Created
April 9, 2014 10:33
-
-
Save cgrand/10252639 to your computer and use it in GitHub Desktop.
This file contains 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
;; 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