specter

Latest version: 1.1.3
frenata 2018-03-02T16:02:14.000510Z

If I want to select some substructure out of a nested map, rather than just the leaves, what's the best way to do that with specter? IE, I have {:foo 1 :bar {:baz 2 :bat 3}}, and I want to select out {:bar {:baz 2}}. New to the library and just poking around. Thought subselect or filterer might work, but at least the way I'm using them they aren't quite right.

nathanmarz 2018-03-02T16:18:57.000630Z

@andrew354 it depends on the use case, but generally the most straightforward approach is to remove what you don't want

nathanmarz 2018-03-02T16:19:01.000511Z

(setval (multi-path :foo [:bar :bat]) NONE data)

frenata 2018-03-02T16:20:41.000451Z

Ah, so naming the paths of things you want to get rid of.

frenata 2018-03-02T16:21:02.000879Z

Unfortunately I think in my actual use-case, I want to keep a fairly small part of the structure, but I'll see if this approach will work.

nathanmarz 2018-03-02T16:24:03.000594Z

@andrew354 another approach that explicitly selects the keys you want:

nathanmarz 2018-03-02T16:24:10.000586Z

(defdynamicnav viewed [path viewnav]
  (transformed path #(select-any viewnav %)))

(select-any [(submap [:bar]) (viewed :bar (submap [:baz]))] data)

frenata 2018-03-02T16:25:28.000267Z

Interesting. I may try both approaches and see what is most clear.

nathanmarz 2018-03-02T16:25:39.000531Z

could make a helper that takes in a list of keyword paths and produces the navigation

nathanmarz 2018-03-02T16:26:00.000488Z

e.g. (select-any (keep-keyword-paths [:bar :baz]) data)

schmee 2018-03-02T16:28:59.000147Z

I think that viewed with a bit of polish could go into specter core 🙂