从'do`中间返回值(Clojure)

Rya*_*sio 1 architecture functional-programming clojure

我有一个在游戏中运行的I/O函数列表,但是需要从函数中间的某个函数中收集值. do

(defn setup-steps [game-state]
  (do (io/clear-screen)
      (print-welcome-message)
      (initial-setup) ;; value to be collected
      (io/clear-screen)
      (io/print-board game-state)))
Run Code Online (Sandbox Code Playgroud)

是否有一种聪明的方法可以从中间的某个位置返回值do

在线下,我使用返回值setup-steps来更新原子,如下所示:

(defn game-loop [game]
  (while (:game-in-progress? @game)

     ;; Here is where I am using the results
    (->> (s-io/setup-steps @game) (state/updater game))

    (while (:game-in-progress? @game)
      (->> (m-io/turn-steps @game) (state/updater game)))
    (->> (eg-io/end-game-steps @game) (state/updater game)))
  (eg-io/exit-game))
Run Code Online (Sandbox Code Playgroud)

哪里

(defn updater
  "Updates the game state.
  This is the only place where the game atom is modified."
  [game update-params]
  (swap! game merge update-params))
Run Code Online (Sandbox Code Playgroud)

我相信你可以为此编写一个宏,但我还没有真正理解宏.

也许我正在以错误的方式思考这个问题......是否更倾向于swap!内部setup-steps

cla*_*taq 7

你有什么理由不能在结果中分配结果let并在函数结束时返回它吗?

(defn setup-steps [game-state]
  (io/clear-screen)
  (print-welcome-message)
  (let [v (initial-setup)] ;; value to be collected
    (io/clear-screen)
    (io/print-board game-state)
    v))
Run Code Online (Sandbox Code Playgroud)

编辑:摆脱doRyan Asensio提到的冗余.