Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Created May 30, 2012 02:34
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save puffnfresh/2833130 to your computer and use it in GitHub Desktop.
Save puffnfresh/2833130 to your computer and use it in GitHub Desktop.
(def users [{:name "Brian" :age 22} {:name "Ben" :age 19}])
;; Takes a "path", returns a function that takes an "object" and
;; returns a "costate"
(defn lens [p]
(fn [o]
{:get (get-in o p)
:set #(assoc-in o p %1)}))
(def myAgeLens (lens [0 :age]))
(def usersCoState (myAgeLens users))
(println (:get usersCoState))
;; 22
(println ((:set usersCoState) 23))
;; [{:name Brian, :age 23} {:name Ben, :age 19}]
;; Laws
(println (= (:get (myAgeLens ((:set usersCoState) 23))) 23))
(println (= ((:set usersCoState) (:get usersCoState)) users))
(println (= ((:set (myAgeLens ((:set usersCoState) 23))) 24) ((:set usersCoState) 24)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment