From edcad450618ebb0a1831be69a6513d1cb2e5f2e7 Mon Sep 17 00:00:00 2001 From: Christopher O'Donnell Date: Fri, 10 Jun 2016 22:53:31 -0400 Subject: [PATCH] Finish draft of for all of the all caps navigators. --- List-of-Navigators.md | 149 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 147 insertions(+), 2 deletions(-) diff --git a/List-of-Navigators.md b/List-of-Navigators.md index 2906cd6..19b612a 100644 --- a/List-of-Navigators.md +++ b/List-of-Navigators.md @@ -4,7 +4,7 @@ ### ALL -The `ALL` navigator navigates to every element in a collection. If the collection is a map, it will navigate to each key-value pair `[key value]`. The resulting elements will be reconstructed as a vector. +`ALL` navigates to every element in a collection. If the collection is a map, it will navigate to each key-value pair `[key value]`. The resulting elements will be reconstructed as a vector. (Or maybe this is because of `select`?) ```clojure => (select [ALL] [0 1 2 3]) @@ -13,4 +13,149 @@ The `ALL` navigator navigates to every element in a collection. If the collectio [0 1 2 3] => (select [ALL] {:a :b, :c :d, :e :f}) [[:a :b] [:c :d] [:e :f]] -``` \ No newline at end of file +``` + +### ATOM + +`ATOM` navigates to the value of an atom. + +```clojure +=> (def a (atom 0)) +=> (select-one [ATOM] a) +0 +=> (swap! a inc) +=> (select-one [ATOM] a) +1 +``` + +### BEGINNING + +`BEGINNING` navigates to the empty subsequence before the beginning of a collection. Useful with `transform` to add values onto the beginning of a sequence. Returns a lazy sequence. + +```clojure +=> (transform [BEGINNING] (fn [_] '(0 1)) (range 2 7)) +(0 1 2 3 4 5 6) +=> (transform [BEGINNING] (fn [_] [0 1]) (range 2 7)) +(0 1 2 3 4 5 6) +=> (transform [BEGINNING] (fn [_] {0 1}) (range 2 7)) +([0 1] 2 3 4 5 6) +=> (transform [BEGINNING] (fn [_] {:foo :baz}) {:foo :bar}) +([:foo :baz] [:foo :bar]) +``` + + +### END + +`END` navigates to the empty subsequence after the end of a collection. Useful with `transform` to add values onto the end of a sequence. Returns a lazy sequence. + +```clojure +=> (transform [END] (fn [_] '(5 6)) (range 5)) +(0 1 2 3 4 5 6) +=> (transform [END] (fn [_] [5 6]) (range 5)) +(0 1 2 3 4 5 6) +=> (transform [END] (fn [_] {5 6}) (range 5)) +(0 1 2 3 4 [5 6]) +=> (transform [END] (fn [_] {:foo :baz}) {:foo :bar}) +([:foo :bar] [:foo :baz]) +``` + +### FIRST + +`FIRST` navigates to the first element of a collection. If the collection is a map, returns the key-value pair `[key value]`. If the collection is empty, navigation stops. + +```clojure +=> (select-one [FIRST] (range 5)) +0 +=> (select-one [FIRST] (sorted-map 0 :a 1 :b)) +[0 :a] +=> (select-one [FIRST] (sorted-set 0 1 2 3)) +0 +=> (select-one [FIRST] '()) +nil +=> (select [FIRST] '()) +nil +``` + +### LAST + +`LAST` navigates to the last element of a collection. If the collection is a map, returns the key-value pair `[key value]`. If the collection is empty, navigation stops. + +```clojure +=> (select-one [LAST] (range 5)) +4 +=> (select-one [LAST] (sorted-map 0 :a 1 :b)) +[1 :b] +=> (select-one [LAST] (sorted-set 0 1 2 3)) +3 +=> (select-one [LAST] '()) +nil +=> (select [LAST] '()) +nil +``` + +### MAP-VALS + +`MAP-VALS` navigates to every value in a map. `MAP-VALS` is more efficient than `[ALL LAST]`. Note that `MAP-VALS` returns a lazy seq. + +```clojure +=> (select [MAP-VALS] {:a :b, :c :d}) +(:b :d) +=> (select [MAP-VALS MAP-VALS] {:a {:b :c} :d {:e :f}}) +(:c :f) +``` + +### NIL->LIST + +`NIL->LIST` navigates to the empty list `'()` if the value is nil. Otherwise it stays at the current value. + +```clojure +=> (select-one [NIL->LIST] nil) +() +=> (select-one [NIL->LIST] :foo) +:foo +``` + +### NIL->SET + +`NIL->SET` navigates to the empty set `#{}` if the value is nil. Otherwise it stays at the current value. + +```clojure +=> (select-one [NIL->LIST] nil) +#{} +=> (select-one [NIL->LIST] :foo) +:foo +``` + +### NIL->VECTOR + +`NIL->VECTOR` navigates to the empty vector `[]` if the value is nil. Otherwise it stays at the current value. + +```clojure +=> (select-one [NIL->LIST] nil) +[] +=> (select-one [NIL->LIST] :foo) +:foo +``` + +### STAY + +`STAY` stays in place. It is the no-op navigator. + +```clojure +=> (select-one [STAY] :foo) +:foo +``` + +### STOP + +`STOP` stops navigation. For selection, returns nil. For transformation, returns the structure unchanged. + +```clojure +=> (select-one [STOP] :foo) +nil +=> (select [ALL STOP] (range 5)) +[] +=> (transform [ALL STOP] inc (range 5)) +(0 1 2 3 4) +``` +