Skip to content

Instantly share code, notes, and snippets.

@micha
Last active July 25, 2016 00:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micha/cc7aab2fc2cb5757ef0fcce6e0bdbd5e to your computer and use it in GitHub Desktop.
Save micha/cc7aab2fc2cb5757ef0fcce6e0bdbd5e to your computer and use it in GitHub Desktop.
(ns foop2.bind
(:refer-clojure :exclude [binding bound-fn])
(:require [javelin.core :as j]))
(def ^:dynamic *bindings* #{})
(defmacro binding [bindings & body]
(let [syms (set (map first (partition 2 bindings)))]
(clojure.core/binding [*bindings* (into *bindings* syms)]
(j/macroexpand-all* &env `(clojure.core/binding [~@bindings] ~@body)))))
(defmacro bound-fn [args & body]
(let [bind (comp flatten (partial map list))
syms (take (count *bindings*) (repeatedly gensym))]
`(let [~@(bind syms *bindings*)]
(fn [~@args] (binding [~@(bind *bindings* syms)] ~@body)))))
(ns+ foop2.core
(:page "index.html")
(:require
[foop2.bind :as bind]
[hoplon.core :refer :all]
[javelin.core :refer :all]))
(def ^:dynamic *state* nil)
(body
(bind/binding [*state* (cell 100)]
(div :click (bind/bound-fn [_] (swap! *state* inc)) *state*))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment