Simple variations on state machines in Clojure

unsorted — cgrand, 3 March 2009 @ 15 h 00 min

Given a transition function that takes the current state and an input value as arguments then (reduce transition-fn initial-state input) returns the final state.

If you are interested in intermediate states, you can use clojure.contrib.seq-utils/reductions instead of reduce.

If you want an asynchronous state machine, you can use an agent:

(def agt (agent initial-state))
;then each time you get an input-value:
(send agt transition-fn input-value)

If you add a watch to the agent, you can react to state transitions.

3 Comments »

  1. I like it!
    Alan

    Comment by Alan Thompson — 5 November 2015 @ 1 h 05 min
  2. IWIN 68 2025 ra mắt, tặng code 99K tại iwin68-club.net! Thử bầu cua, xóc đĩa, tài xỉu… và thắng lớn ngay hôm nay!

    Comment by IWIN 68 — 26 January 2025 @ 17 h 53 min
  3. Max88 là một trong những nhà cái trực tuyến nổi bật và được yêu thích tại châu Á. Với sự phát triển không ngừng và sự đổi mới trong dịch vụ, Max 88 đang khẳng định được vị thế của mình trong ngành cá cược trực tuyến.

    Comment by Max88 870 — 15 March 2025 @ 4 h 48 min

RSS feed for comments on this post. TrackBack URI

Leave a comment

(c) 2025 Clojure and me | powered by WordPress with Barecity