寻找一种从具有副作用的函数生成返回值集合的方法,以便我可以将其提供给take-while
.
(defn function-with-side-effects [n]
(if (> n 10) false (do (perform-io n) true)))
(defn call-function-with-side-effects []
(take-while true (? (iterate inc 0) ?)))
更新
这是我在 Jan 的回答后得到的:
(defn function-with-side-effects [n]
(if (> n 10) false (do (println n) true)))
(defn call-function-with-side-effects []
(take-while true? (map function-with-side-effects (iterate inc 0))))
(deftest test-function-with-side-effects
(call-function-with-side-effects))
运行测试不会打印任何内容。使用doall
导致内存不足异常。