Skip to content

Instantly share code, notes, and snippets.

@vbedegi
Last active April 29, 2019 08:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vbedegi/b7292af981020d128d48dd2218468746 to your computer and use it in GitHub Desktop.
Save vbedegi/b7292af981020d128d48dd2218468746 to your computer and use it in GitHub Desktop.
elm in cljs
; util
(defn tag-dispatch [dispatch tag]
(fn [msg]
(let [tagged (if (vector? tag)
(conj tag msg)
[tag msg])]
(dispatch tagged))))
; counter
(defn init-counter [value]
value)
(defn render-counter [model dispatch]
[:div (str model)
[:button {:on-click #(dispatch :inc)} "INC!"]
[:button {:on-click #(dispatch :dec)} "DEC!"]])
(defn update-counter [model msg]
(match msg
:inc
(inc model)
:dec
(dec model)))
; container
(defn init-container [top bottom]
{:top (init-counter top)
:bottom (init-counter bottom)})
(defn render-container [model dispatch]
[:div
(render-counter (:top model) (tag-dispatch dispatch :top))
(render-counter (:bottom model) (tag-dispatch dispatch :bottom))
[:button {:on-click #(dispatch :reset) "reset!"}]])
(defn update-container [model msg]
(match msg
:reset
(init-container 0 0)
[:top msg]
(assoc model :top (update-counter (:top model) msg))
[:bottom msg]
(assoc model :bottom (update-counter (:bottom model) msg))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment