redplanetlabs / specter

Clojure(Script)'s missing piece
Apache License 2.0
2.52k stars 105 forks source link

Question: Unexpected result of filterer + FIRST #282

Closed oskarkv closed 5 years ago

oskarkv commented 5 years ago

If I do (s/setval [(s/filterer (s/pred= 1)) s/FIRST] s/NONE [1 2 1 3]) I get [1 2 3] but I expected [2 1 3]. What's going on?

nathanmarz commented 5 years ago

filterer navigates you to a sequence. When the replacement sequence is shorter, it removes elements in the original data structure from the end. Put another way, it's indexes in the filterer sequence that correspond to locations in the original data structure.

oskarkv commented 5 years ago

Oh, I see. Is there a better way to do what I wanted, i.e. remove the first element matching some predicate?

nathanmarz commented 5 years ago

There's an idea for "stateful navigators" which would allow something like this to be done very elegantly: https://github.com/nathanmarz/specter/issues/121

For now, I would suggest using zippers. com.rpl.specter.zipper integrates zippers with Specter.

oskarkv commented 5 years ago

Ok! Thank you!