Skip to content

Instantly share code, notes, and snippets.

@fmnoise
Created May 27, 2017 12:33
Show Gist options
  • Save fmnoise/9fcc77cc65779f079078a2ab4d084943 to your computer and use it in GitHub Desktop.
Save fmnoise/9fcc77cc65779f079078a2ab4d084943 to your computer and use it in GitHub Desktop.
Moore machine using atom
(defn make-transitions [& states]
(let [statemap
(zipmap
(map str states)
(rest (conj (vec states) (first states))))]
(fn [state] (statemap (str state)))))
(def moore-machine (atom :stopped))
(def transitions (make-transitions :starting :started :stopping :stopped))
(swap! moore-machine transitions) ;; :starting
(swap! moore-machine transitions) ;; :started
(swap! moore-machine transitions) ;; :stopping
(swap! moore-machine transitions) ;; :stopped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment