I need to add a item( :NEW
) before a pred ( map?
) just on first match.
exaple: [:foo :bar {} {}]
into [:foo :bar :NEW {} {}]
I'm using mapcat and a atom
to say if it's the first match.
There is a simpler way to do that?
There is BEFORE-ELEM
but can't understand how to compose it.
@souenzzo you can do something like:
(defn index-matching [p]
(fn [s]
(select-first [INDEXED-VALS (selected? LAST (pred p)) FIRST] s)
))
(setval
[(srange-dynamic (index-matching map?) (end-fn [_ i] i))
BEFORE-ELEM]
:NEW
[:foo :bar {} {}]
)
probably want to modify it a little to handle the case where nothing matches the predicate
suppose, you have a transform ALL function which makes substitutions that may themselves need to have substitutions and so reapply the transform on result until no more substitutions involved. Is the obvious soln such as (->> x (iterate xform) (drop-while (comp not test)) first)
still the 'right' way to do this with specter? Or is there an even more idiomatic way?
@jsa-aerial you could always do something like:
(def iterated-path
(recursive-path [test] p
(stay-then-continue
(pred test)
p
)))
(transform (iterated-path #(< % 10)) inc 1)
;; => 10
@nathanmarz that looks very interesting - let me take a hack with that. Thanks!
Hmmmm, actually the test needs the last two values. Basically let x1 and x2 be the last two values, iterate until (= x1 x2).
(def FIXED-POINT-PATH
(recursive-path [] p
[(collect-one STAY)
(multi-path
[DISPENSE STAY]
[(collect-one STAY)
(if-path (collected? [a b] (= a b))
STOP
[DISPENSE p]
)])
]))
(defn f [a] (min (inc a) 10))
(transform [:a FIXED-POINT-PATH] f {:a 1 :b 2})
;; => {:a 10 :b 2}
that's an interesting one
actually can shorten the definition like this:
(def FIXED-POINT-PATH
(recursive-path [] p
[(collect-one STAY)
(multi-path
[DISPENSE STAY]
[(collect-one STAY)
(if-path (collected? [a b] (not= a b))
[DISPENSE p]
)])
]))
I'd probably just write that in regular clojure though
using loop
I think it will be instructive to study these two - even though for the particular case in question the solution actually looks simpler in straight clojure
for sure