@huthayfa.ainqawi it'll be easier to help you if you give an example of the input and output you want
@nathanmarz input {"properties": { "assets": {"target-key"}}} output: [properties assets target-key]
input ({"properties": { "assets": {"target-key"}}}, target-key) output: [properties assets target-key]
@huthayfa.ainqawi that input isn't well formed
input ({"properties": { "assets": {"target-key" "value"}}}, target-key) nested map
Hi, how do you navigate data structures with specter lazily? What I'm trying to do is select the first item with matching predicate like this:
(->> very-long-vector
(transform [ALL] my-fn)
(select-first [ALL my-pred?]))
I believe doing that won't be very efficient since specter will "realize" each step@funyako.funyao156 there is no lazy computation with specter
but if you do (select-first [ALL (view my-fn) my-pred?] very-long-vector)
it will stop traversing the vector as soon as my-pred?
is satisfied
@nathanmarz so the select-first
behave similar to (first (filter pred ...))
in terms of "lazy like" ? Thanks!
@funyako.funyao156 it works via early termination
it uses reduced
/`reduced?` under the hood
@nathanmarz Thanks alot for answering me! Last one, sorry if I ask a lot. How do you return default value using select-first
if no matching element found, for example:
(select-first [ALL keyword? (nil->val :foo/bar)] [1 2 3])
That still return a nil.that code never gets to the nil->val
navigator since keyword?
filters everything out
you can just do (or (select-first [ALL keyword?] [1 2 3]) :foo/bar)
Right, silly me. Sorry I ask alot! Thanks again!
no problem, that's what this channel is for
user=> (def MAP-VALS+ (path ALL (collect-one FIRST) LAST))
#'user/MAP-VALS+
user=> (select-any [MAP-VALS+ MAP-VALS+ MAP-KEYS] {"properties" { "assets" {"target-key" "value"}}})
["properties" "assets" "target-key"]