Fork me on GitHub
#specter
<
2018-09-06
>
roklenarcic12:09:12

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.

roklenarcic12:09:51

I'm a bit of a newbie when it comes to working subsequences with specter

nathanmarz13:09:22

you could make your own version of continuous-subseqs with the generalized behavior, it's not very complicated

roklenarcic13:09:07

Fortunately this isn't nested in some other selector so I'll just use drop-while, take-while

bruno.bonacci15:09:16

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

nathanmarz17:09:58

@bruno.bonacci you can do something like:

(transform ALL
  (fn [[m b]] (assoc m :b b))
  (select [ALL VAL :b ALL] data))

nathanmarz17:09:55

having two ALL's in a select can act like mapcat

bruno.bonacci17:09:41

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.

bruno.bonacci17:09:19

interesting approach, thanks.

nathanmarz17:09:55

with a modified view you could do it in a single select

nathanmarz17:09:11

just by passing the collected vals into the view fn

bruno.bonacci17:09:27

got it, thanks

bruno.bonacci17:09:57

Sorry, view doesn’t seem to be affected by the collection of values

bruno.bonacci17:09:36

(select [ALL VAL (view pr-str)] (range 5)) => [[0 "0"] [1 "1"] [2 "2"] [3 "3"] [4 "4"]]

bruno.bonacci17:09:22

i was expecting that the view function would receive also the collected values

bruno.bonacci17:09:08

here it appears it only receives the value in the navigation path

nathanmarz17:09:47

that's correct, that's why I said with a modified view

nathanmarz17:09:47

(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)))))