From 2fdb81823e06852cbe8d256d39435192c1678980 Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Mon, 9 Oct 2017 12:47:01 -0700 Subject: [PATCH 1/2] Add continue-then-stay example. --- Using-Specter-Recursively.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Using-Specter-Recursively.md b/Using-Specter-Recursively.md index e52ed0a..b3678e0 100644 --- a/Using-Specter-Recursively.md +++ b/Using-Specter-Recursively.md @@ -210,6 +210,30 @@ Here are some other examples of using Specter recursively with `recursive-path`. {:a {:aaa 4, :b {:c {:aaa 3}, :aaa 2}}} ``` +## Recursively navigate to every map in a map of maps + +You have a deeply nested map of maps: + +```clojure +(def data + {:a {:b {:c 1} :d 2} + :e {:f 3 :g 4}}) +``` + +You want to recursively navigate this data structure and add a key-value pair to every map at every level. This example, `MAP-NODES`, shows you how to do so: + +```clojure +=> (def MAP-NODES + (recursive-path [] p + (if-path map? + (continue-then-stay MAP-VALS p)))) + +=> (setval [MAP-NODES :X] 0 data) +{:a {:b {:c 1, :X 0}, :d 2, :X 0}, :e {:f 3, :g 4, :X 0}, :X 0} +``` + +`MAP-NODES` illustrates how to combine recursive paths with `continue-then-stay`, which navigates to the provided path and then to the current element. This should also point to how you might use recursive paths with `stay-then-continue`, which navigates to the current element and then to the provided path. + ## Find the "index route" of a value within a data structure This example comes from [a Stack Overflow question](https://stackoverflow.com/questions/45764946/how-to-find-indexes-in-deeply-nested-data-structurevectors-and-lists-in-clojur). From bad2cb5265d54f838cfc7383f2cbcdcd593a48ee Mon Sep 17 00:00:00 2001 From: Michael Fogleman Date: Mon, 9 Oct 2017 12:56:28 -0700 Subject: [PATCH 2/2] Link to navigators across docs. --- Using-Specter-Recursively.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Using-Specter-Recursively.md b/Using-Specter-Recursively.md index b3678e0..2f9ed31 100644 --- a/Using-Specter-Recursively.md +++ b/Using-Specter-Recursively.md @@ -232,7 +232,7 @@ You want to recursively navigate this data structure and add a key-value pair to {:a {:b {:c 1, :X 0}, :d 2, :X 0}, :e {:f 3, :g 4, :X 0}, :X 0} ``` -`MAP-NODES` illustrates how to combine recursive paths with `continue-then-stay`, which navigates to the provided path and then to the current element. This should also point to how you might use recursive paths with `stay-then-continue`, which navigates to the current element and then to the provided path. +`MAP-NODES` illustrates how to combine recursive paths with `[continue-then-stay](https://github.com/nathanmarz/specter/wiki/List-of-Navigators#continue-then-stay)`, which navigates to the provided path and then to the current element. This should also point to how you might use recursive paths with `[stay-then-continue](https://github.com/nathanmarz/specter/wiki/List-of-Navigators#stay-then-continue)`, which navigates to the current element and then to the provided path. ## Find the "index route" of a value within a data structure @@ -244,9 +244,9 @@ This example comes from [a Stack Overflow question](https://stackoverflow.com/qu (if-path sequential? [INDEXED-VALS (if-path [LAST (pred= v)] - FIRST - [(collect-one FIRST) LAST p])])) - ret (select-first walker data)] + FIRST + [(collect-one FIRST) LAST p])])) + ret (select-first walker data)] (if (or (vector? ret) (nil? ret)) ret [ret]))) #'playground.specter/find-index-route => (find-index-route :my-key '(1 2 :my-key))