Created
May 13, 2011 12:28
-
-
Save mikera/970439 to your computer and use it in GitHub Desktop.
Options for state update in Clojure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; event application with subsequent events | |
;; event :: state -> (state' [event]) | |
(defn update-with-events [state events] | |
(if-let [event (first events)] | |
(let [other-events (rest events) | |
[updated-state new-events] (event game)] | |
(recur updated-state (concat other-events new-events))) | |
state )) | |
(defn update [state event] | |
(update-events state [event])) | |
(defn example-event [original-state] | |
(let [events (atom []) | |
state (atom original-state)] | |
(if (somecondition state) | |
(swap! events conj some-new-event)) | |
(swap! state do-something-to-state) | |
[@state @events])) | |
;; state update with state transition functions | |
;; event: state -> state' | |
(defn update [state event] | |
(event state)) | |
(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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment