Skip to content

Instantly share code, notes, and snippets.

@aengelberg
Last active April 29, 2016 19:06
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aengelberg/c723648035454f32e6374145349a71ba to your computer and use it in GitHub Desktop.
Save aengelberg/c723648035454f32e6374145349a71ba to your computer and use it in GitHub Desktop.
(defn select-view
"Navigates to a sequence that is the result of a select on the
current structure. Has similar functionality of srange and filterer,
but can be used with arbitrary selectors making it vastly more
powerful."
[& path]
(fixed-pathed-path [late path]
(select* [this structure next-fn]
(next-fn (compiled-select late structure)))
(transform* [this structure next-fn]
(let [select-result (compiled-select late structure)
transformed (next-fn select-result)
values-to-insert (atom transformed)]
(compiled-transform late
(fn [_] (let [next-val (first @values-to-insert)]
(swap! values-to-insert rest)
next-val))
structure)))))
;; I couldn't think of any ways to solve the following problems using
;; specter without select-view:
(comment
;; Reverse all even :a values across several maps.
(transform [(select-view ALL :a even?)]
reverse
[{:a 1} {:a 2} {:a 3} {:a 4}])
=>
[{:a 1} {:a 4} {:a 3} {:a 2}]
;; Shuffle the values in several maps.
(transform [ALL (select-view ALL LAST)]
shuffle
[{:a 1 :b 2 :c 3}
{:d :hello :e 'saluton :f "aloha"}])
=>
[{:a 3, :b 2, :c 1} {:d saluton, :e "aloha", :f :hello}]
;; Reverse only the even leafs of a tree.
(defprotocolpath AllNestedInts [])
(extend-protocolpath AllNestedInts
clojure.lang.Seqable [ALL AllNestedInts]
Long STAY)
(transform [(select-view AllNestedInts) (filterer even?)]
reverse
[[[0] 1] [2 3 [4 [5]]]])
=>
[[[4] 1] [2 3 [0 [5]]]]
)
@aengelberg
Copy link
Author

A less stateful version of this would be ideal (my atom approach feels icky) but would need more thought, the kicker is how to stick the transformed values back into whatever structure you started with.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment