Clojure中功能状态更新的模式/习语

mik*_*era 2 design-patterns functional-programming clojure

我决定尝试在Clojure中编写一个模拟程序(作为概念证明),其中:

  • 模拟的整个状态存储在单个不可变数据结构中
  • 状态更新表示为将状态映射到下一状态的函数

这意味着我可以编写更新函数,如:

(defn example-update-function [old-state]
  (let [state (atom old-state)]
    (swap! state some-other-update-function-1)
    (if (some-condition @state)
      (swap! state some-conditional-update-function))
    (swap! state some-other-update-function-2)
    (reset! state (some-function @state some-other-param))
    @state))
Run Code Online (Sandbox Code Playgroud)

这种方法似乎有效但下面给出了两个值得关注的原因:

  • 我不得不使用一个可变原子来管理中间状态......看起来不是很有用!
  • 代码看起来有点难看,所有的swap!s和@state dereferences到处都是.

有更好/更优雅的方法吗?

Lau*_*tit 5

你可以像这样写:

  (defn change-when 
    "If (test val) is truethy, returns (fun val), else returns val"
    [val test fun] (if (test val) (fun val) val))

  (defn example-update-function [old-state]
    (-> old-state
        some-other-update-function-1
        (change-when some-condition some-conditional-update-function)
        some-other-update-function-2
        (some-function some-other-param)
        identity))

可能是在monad之路的中途......