将螺纹last与循环结合在clojure中
我编写小型纸牌游戏,我希望我的代码非常明确,因此在高水平上清楚地表明,有很多回合可以玩。我的第一个实现是:
(defn play-game []
(->
(myio/initialize-cards-and-players)
(shuffle-and-share-cards myio/myshuffle)
;(announce)
(play-rounds)
)
)
我希望比赛回合更加明确并表明,这是顶级循环:
(defn play-game []
(->>
(myio/initialize-cards-and-players)
(shuffle-and-share-cards myio/myshuffle)
(announce)
(loop [round 1]
(play-round-with-game round)
(if (<= round 4)
(recur (+ round 1))
)
)
; (play-round-with-game 2)
; (play-round-with-game 3)
; (play-round-with-game 4)
)
)
但是我以某种方式无法获得“游戏”对象,每个函数都会返回到循环中。有没有螺纹的好方法来处理此操作?
还是有一种方法可以使用减少而不是循环?
I write small card game and i want my code to be very explicit and therefore make it clear on a high level, that there are rounds to play. My first implementation was:
(defn play-game []
(->
(myio/initialize-cards-and-players)
(shuffle-and-share-cards myio/myshuffle)
;(announce)
(play-rounds)
)
)
I want the play rounds to be more explicit and show, that it is a loop on top-level:
(defn play-game []
(->>
(myio/initialize-cards-and-players)
(shuffle-and-share-cards myio/myshuffle)
(announce)
(loop [round 1]
(play-round-with-game round)
(if (<= round 4)
(recur (+ round 1))
)
)
; (play-round-with-game 2)
; (play-round-with-game 3)
; (play-round-with-game 4)
)
)
But i somehow cannot get the "game" object, that is returned by every function into the loop. Is there a good way to handle this with thread-last?
Or is there a way to use reduce instead of the loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如尤金(Eugene)所建议的那样,您想保持简单。以下是我通常使用的结构,
我喜欢使用
让
表单为每个值提供一个明确的名称。它有助于避免混淆,尤其是对于代码的新读者而言。如果(INC圆形)
并不那么简单,则我还将做一个明确的rount-next
actible,并在recur
中使用它。另外,我喜欢返回条件是在循环的每次迭代中检查的第一件事。这通常将
recur
作为循环中的最后一个语句。As Eugene suggested, you want to keep it simple. The following is the structure I normally use
I like to use
let
forms to give each value an explicit name. It helps to avoid confusion, especially for new readers of the code. If the(inc round)
was not so simple, I would also make an explicitround-next
variable and use that in therecur
.Also, I like the return condition to be the first thing checked in each iteration of the loop. This normally leaves the
recur
as the last statement in the loop.