Skip to content

Instantly share code, notes, and snippets.

@cgrand
Created April 9, 2014 10:33
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 cgrand/10252639 to your computer and use it in GitHub Desktop.
Save cgrand/10252639 to your computer and use it in GitHub Desktop.
;; bifocal is the lens library which I may spin off the enliven codebase
;; a lens is the generalization of associative access/update
;; fetch/putback are the pendant of get/assoc
;; symbols, strings, numbers, keywords, regex are lenses on their own
=> (fetch {:a 1 :b 2} :a)
1
=> (putback {:a 1 :b 2} :a 6)
{:a 6, :b 2}
=> (update {:a 1 :b 2} :a inc)
{:a 2, :b 2}
;; lenses can be compounded:
=> (fetch {:a {:b 2} :c 3} (compound :a :b))
2
;; fetch and putback implicitely call #'lens on their lens argument and
;; #'lens converts sequential collections of lenses into compound lenses
=> (fetch {:a {:b 2} :c 3} [:a :b])
2
;; lenses can be wrapped around functions:
=> (def inc-count (af inc :count))
#'enliven.core.lenses/inc-count
=> (inc-count {:count 41})
{:count 42}
;; af stands for autofocus, you can manually focus:
=> (def inc-count (focus :count inc :count))
#'enliven.core.lenses/inc-count
=> (inc-count {:count 41})
{:count 42}
;; focused fns can take additional args
=> ((focus :c + :a :b) {:a 1 :b 2})
{:c 3, :a 1, :b 2}
=> ((af + :acc :x) {:acc 3 :x 6})
{:x 6, :acc 9}
;; things to come: automatic simplification when composing focused fns
;; so that
(comp (af inc :c) (focus :c + :a :b)) becomes (focus c (comp inc +) :a :b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment