Fork me on GitHub
#specter
<
2018-05-25
>
nathanmarz00:05:31

@huthayfa.ainqawi it'll be easier to help you if you give an example of the input and output you want

huthayfa02:05:54

@nathanmarz input {"properties": { "assets": {"target-key"}}} output: [properties assets target-key]

huthayfa02:05:30

input ({"properties": { "assets": {"target-key"}}}, target-key) output: [properties assets target-key]

nathanmarz02:05:27

@huthayfa.ainqawi that input isn't well formed

huthayfa03:05:27

input ({"properties": { "assets": {"target-key" "value"}}}, target-key) nested map

fmn12:05:34

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

nathanmarz13:05:00

@funyako.funyao156 there is no lazy computation with specter

nathanmarz13:05:34

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

fmn13:05:11

@nathanmarz so the select-first behave similar to (first (filter pred ...)) in terms of "lazy like" ? Thanks!

nathanmarz14:05:07

@funyako.funyao156 it works via early termination

nathanmarz14:05:41

it uses reduced/`reduced?` under the hood

fmn14:05:32

@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.

nathanmarz14:05:01

that code never gets to the nil->val navigator since keyword? filters everything out

nathanmarz14:05:21

you can just do (or (select-first [ALL keyword?] [1 2 3]) :foo/bar)

fmn14:05:08

Right, silly me. Sorry I ask alot! Thanks again!

nathanmarz14:05:24

no problem, that's what this channel is for

nathanmarz14:05:44

@huthayfa.ainqawi

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"]