将螺纹last与循环结合在clojure中

发布于 2025-02-11 03:27:49 字数 843 浏览 5 评论 0原文

我编写小型纸牌游戏,我希望我的代码非常明确,因此在高水平上清楚地表明,有很多回合可以玩。我的第一个实现是:

(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚你发端 2025-02-18 03:27:49

正如尤金(Eugene)所建议的那样,您想保持简单。以下是我通常使用的结构,

(defn play-game
  []
  (let [game-init (->>
                    (myio/initialize-cards-and-players)
                    (shuffle-and-share-cards myio/myshuffle)
                    (announce))]
    (loop [round 1
           game  game-init]
      (if (< 4 round)
        game ; return result
        (let [game-next (play-round-with-game game round)]
          (recur (inc round) game-next))))))

我喜欢使用表单为每个值提供一个明确的名称。它有助于避免混淆,尤其是对于代码的新读者而言。如果(INC圆形)并不那么简单,则我还将做一个明确的rount-next actible,并在recur中使用它。

另外,我喜欢返回条件是在循环的每次迭代中检查的第一件事。这通常将recur作为循环中的最后一个语句。

As Eugene suggested, you want to keep it simple. The following is the structure I normally use

(defn play-game
  []
  (let [game-init (->>
                    (myio/initialize-cards-and-players)
                    (shuffle-and-share-cards myio/myshuffle)
                    (announce))]
    (loop [round 1
           game  game-init]
      (if (< 4 round)
        game ; return result
        (let [game-next (play-round-with-game game round)]
          (recur (inc round) game-next))))))

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 explicit round-next variable and use that in the recur.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文