added setval and transform
This commit is contained in:
parent
9060210d73
commit
6b5f02afbb
1 changed files with 87 additions and 9 deletions
|
|
@ -23,16 +23,18 @@
|
|||
:color "blue"}]}
|
||||
:latacara {:prime [{:doors 11
|
||||
:color "clear"}]
|
||||
:prime-zero [{:doors 0
|
||||
:color "red"}
|
||||
{:doors 0
|
||||
:color "green"}]}}})
|
||||
:prime-three [{:doors 3
|
||||
:color "red"}
|
||||
{:doors 3
|
||||
:color "green"}]}}})
|
||||
|
||||
(def car-inventory-ford-focus
|
||||
{:car {:ford {:focus [{:doors 2
|
||||
(def car-inventory-ford
|
||||
{:cars {:ford {:focus [{:doors 2
|
||||
:color "red"}
|
||||
{:doors 2
|
||||
:color "blue"}]}}})
|
||||
:color "blue"}]
|
||||
:expedition [{:doors 4
|
||||
:color "red"}]}}})
|
||||
|
||||
;; The hope of this exercise is to get you more familiar with specter and eidolon
|
||||
|
||||
|
|
@ -70,6 +72,12 @@
|
|||
(= [{:doors 2 :color "red"} {:doors 2 :color "blue"} {:doors 4 :color "red"} {:doors 4 :color "black"}]
|
||||
(sr/select [__ __ sr/MAP-VALS __] car-inventory))
|
||||
|
||||
|
||||
;; You can also pass a function to the end of a select to filter results
|
||||
;; Let's dig down into the maps values for all of the cars and then just get the results of the cars with 4 doors
|
||||
(= [{:doors 4, :color "red"} {:doors 4, :color "black"} {:doors 4, :color "tan"} {:doors 4, :color "blue"} {:doors 4, :color "blue"}]
|
||||
(sr/select [:cars ___ ___ sr/ALL #(= (:doors %) 4)] car-inventory))
|
||||
|
||||
;; Great, so we can now traverse a map, going either by the absolute path or using some generalized special functions to get where we need to go
|
||||
;; So what is Eidolon?
|
||||
;; Eidolon is a latacora built specter library - Let's check it out
|
||||
|
|
@ -78,10 +86,80 @@
|
|||
(= [:doors :color :doors :color :doors :color :doors :color :focus :expedition]
|
||||
(sr/select [:cars :ford e/TREE-KEYS] __))
|
||||
|
||||
;; what if instead of TREE-KEYS we wanted the TREE-LEAVES?
|
||||
;; What if instead of TREE-KEYS we wanted the TREE-LEAVES?
|
||||
(= [2 "red" 2 "blue" 4 "red" 4 "black"]
|
||||
(sr/select [:cars :ford ___] car-inventory))
|
||||
|
||||
;; Another eidolon feature is collecting a value and continuing down to the items
|
||||
;; this will return both the collected value and the items in their own vector
|
||||
(= [[:focus {:doors 2, :color "red"}] [:focus {:doors 2, :color "blue"}] [:expedition {:doors 4, :color "red"}] [:expedition {:doors 4, :color "black"}]]
|
||||
(sr/select [:cars __ e/INDEXED __] car-inventory))
|
||||
|
||||
;; TODO figure out a good use for INDEXED-SEQ :)
|
||||
|
||||
;; Setval and transform
|
||||
;; So part of specter is collecting data, but another aspect is changing that data
|
||||
;; Let's scope these examples down a tad with car-inventory-ford-focus
|
||||
|
||||
;; imagine that someone came and painted all of your ford focuses orange.
|
||||
(= {:cars {:ford {:focus [{:doors 2, :color "orange"} {:doors 2, :color "orange"}], :expedition [{:doors 4, :color "red"} {:doors 4, :color "black"}]}}}
|
||||
(sr/setval [:cars :ford :focus sr/ALL __] __ car-inventory-ford))
|
||||
|
||||
;; The new orange ford focuses sell like hot cakes! now that we have sold 2 lets remove them from our inventory
|
||||
(= {:cars {:ford {:expedition [{:doors 4, :color "red"} {:doors 4, :color "black"}]}}}
|
||||
(sr/setval [:cars __ __] sr/NONE car-inventory-ford))
|
||||
|
||||
;; setval takes a map and returns a map.
|
||||
;; Let's use a thread to remove both of our sold ford focuses and change the color of the expedition to orange to try to sell that faster as well
|
||||
(= {:cars {:ford {:expedition [{:doors 4, :color "orange"}]}}}
|
||||
(___ car-inventory-ford
|
||||
(sr/setval [:cars :ford :focus] ___)
|
||||
(sr/setval [:cars :ford :expedition sr/ALL :color] __)))
|
||||
|
||||
;; Transform also takes a map and returns a map
|
||||
;; The format of transform is (sr/transform path function map)
|
||||
;; Generally I use transform when I need to do more complex functions to decide what to change things to
|
||||
|
||||
;; Let's change any blue ford focus from out car-inventory-ford map to orange, our new favorite color
|
||||
(= {:cars {:ford {:focus [{:doors 2, :color "red"} {:doors 2, :color "orange"}], :expedition [{:doors 4, :color "red"}]}}}
|
||||
(___ [:cars :ford :focus sr/ALL :color]
|
||||
#(if (= % "blue")
|
||||
"orange"
|
||||
%)
|
||||
__))
|
||||
|
||||
;; This is another way to right the same thing, but with a less anonymous function
|
||||
;; (sr/transform [:cars :ford :focus sr/ALL :color]
|
||||
;; (fn [color]
|
||||
;; (if (= color "blue")
|
||||
;; "orange"
|
||||
;; color))
|
||||
;; car-inventory-ford)
|
||||
|
||||
;; For this final example let's scope back up to car-inventory, just for fun :)
|
||||
|
||||
;; Let's say that the government has outlawed all cars with an even amount of doors. To be good to the people, let's just give them an extra one
|
||||
;; latacara is ahead of the curve on this one, so we will specifically exclude them from this transform
|
||||
;; We are going to use eidolon's INDEXED function to collect the name of the brand, and then continue to dig down into the door count. From there, we can check if its even using an even? function and if its true increase the count by one
|
||||
(= {:cars {:ford {:focus [{:doors 3, :color "red"} {:doors 3, :color "blue"}], :expedition [{:doors 5, :color "red"} {:doors 5, :color "black"}]}, :toyota {:camry [{:doors 3, :color "red"} {:doors 5, :color "tan"}], :rav4 [{:doors 5, :color "blue"} {:doors 5, :color "blue"}]}, :latacara {:prime [{:doors 11, :color "clear"}], :prime-three [{:doors 3, :color "red"} {:doors 3, :color "green"}]}}}
|
||||
(sr/transform [:cars ___ ___ sr/ALL :doors]
|
||||
(fn [brand doors]
|
||||
(if (not= brand:latacara)
|
||||
(if (___ doors)
|
||||
(___ doors)
|
||||
doors)
|
||||
doors))
|
||||
car-inventory))
|
||||
|
||||
;; Here is another way we could go about it in a kind of clever way
|
||||
;; (ignoring the requirement to exclude latacara)
|
||||
|
||||
;; (sr/transform [e/TREE-LEAVES int?]
|
||||
;; #(if (even? %)
|
||||
;; (inc %)
|
||||
;; %) car-inventory)
|
||||
|
||||
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue