小编Rya*_*sio的帖子

为符号`with-redefs`连续返回不同的值(Clojure)

我希望用来with-redefs模拟STDIN的用户输入.

首先,我正在测试不正确的输入,它应该重新询问用户输入.然后,应该给出正确的输入.

有没有办法用于with-redefs将不同的值绑定到给定的符号?

我正在尝试获得此功能:

(with-redefs [read-line (fn [] "HI")
              read-line (fn [] "OK")]
  (do (println (read-line)) ;; -> "HI"
      (println (read-line)))) ;; -> "OK"
Run Code Online (Sandbox Code Playgroud)

unit-testing clojure

5
推荐指数
1
解决办法
134
查看次数

从'do`中间返回值(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 …
Run Code Online (Sandbox Code Playgroud)

architecture functional-programming clojure

1
推荐指数
1
解决办法
149
查看次数