Hm, I have two predicates and sequence. I would like to select items starting with an element that satisfies predicate1, up to an element that specifies predicate2.
I'm a bit of a newbie when it comes to working subsequences with specter
@roklenarcic you would need https://github.com/nathanmarz/specter/issues/236 to do that
you could make your own version of continuous-subseqs with the generalized behavior, it's not very complicated
Fortunately this isn't nested in some other selector so I'll just use drop-while, take-while
Hi,
is there any way in Specter to have a something like a mapcat
??
For example I’d like to denormalize a map like this:
{:a 1 :b ["a" "b"]}
into
{:a 1 :b "a"}
{:a 1 :b "b"}
same as
(mapcat (fn [{:keys [b] :as m}]
(map #(assoc m :b %) b))
[{:a 1 :b ["a" "b"]} {:a 2 :b ["c" "d"]}])
;;=> '({:a 1, :b "a"} {:a 1, :b "b"} {:a 2, :b "c"} {:a 2, :b "d"})
But I havent found a way to concatenate the result and remove
the extra ’() in one step.
(transform [ALL]
(fn [{:keys [b] :as m}]
(map #(assoc m :b %) b))
[{:a 1 :b ["a" "b"]} {:a 2 :b ["c" "d"]}])
;;=> [({:a 1, :b "a"} {:a 1, :b "b"})
;; ({:a 2, :b "c"} {:a 2, :b "d"})]
any idea?
@bruno.bonacci you can do something like:
(transform ALL
(fn [[m b]] (assoc m :b b))
(select [ALL VAL :b ALL] data))
having two ALL's in a select can act like mapcat
ok, I understand what you are doing here. The select
is creating a projection with the info and the transform
is producing the final maps.
interesting approach, thanks.
with a modified view
you could do it in a single select
just by passing the collected vals into the view fn
got it, thanks
Sorry, view
doesn’t seem to be affected by the collection of values
(select [ALL VAL (view pr-str)] (range 5)) => [[0 "0"] [1 "1"] [2 "2"] [3 "3"] [4 "4"]]
i was expecting that the view
function would receive also the collected values
here it appears it only receives the value in the navigation path
that's correct, that's why I said with a modified view
(defrichnav cview [afn]
(select* [this vals structure next-fn]
(next-fn vals (apply afn (conj vals structure))))
(transform* [this vals structure next-fn]
(next-fn vals (apply afn (conj vals structure)))))
👍 Thanks