Fork me on GitHub
#specter
<
2017-07-20
>
alexyakushev12:07:16

Hello everyone. I want to do the following transformation:

[[:a :b :c] [:d] [:e :f] [:g]] => [[0 1 2] [3] [4 5] [6]]
In other words, I want to enumerate elements in two-levels-deep nested structure. I managed to achieve this with subselect:
(setval (subselect ALL ALL) (iterate inc 0) [[:a :a :a] [:b] [:c :c] [:d]])
Is there a better way than using subselect here? I was looking for something like FLATMAP navigator that is like ALL but splices each sequence into top-level view.

nathanmarz12:07:53

@alexyakushev that's a perfect solution

nathanmarz12:07:46

you could define FLATMAP with something like:

(def FLATMAP
  (recursive-path [] p
    (if-path sequential?
      [ALL p]
      STAY
      )))

alexyakushev12:07:58

Thanks, Nathan! A quick follow-up question. Is there a way to transform a vector so that the transform-fn receives an index of the element? Like map-indexed. All I could Google was https://github.com/nathanmarz/specter/issues/169, but in that case the iterable element was a map, where keys are more easily obtained.

nathanmarz12:07:28

not in specter core

nathanmarz12:07:45

you could define a navigator similar to ALL which navigates to [index value]

alexyakushev12:07:20

Hm, I didn't know it is this easy to define new navigators 🙂 This is excellent, thank you!