specter

Latest version: 1.1.3
roklenarcic 2018-09-06T12:55:12.000100Z

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.

roklenarcic 2018-09-06T12:55:51.000100Z

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

nathanmarz 2018-09-06T13:06:21.000100Z

@roklenarcic you would need https://github.com/nathanmarz/specter/issues/236 to do that

nathanmarz 2018-09-06T13:07:22.000100Z

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

roklenarcic 2018-09-06T13:08:07.000100Z

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

2018-09-06T15:26:16.000100Z

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

2018-09-06T15:26:23.000100Z

any idea?

nathanmarz 2018-09-06T17:22:58.000100Z

@bruno.bonacci you can do something like:

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

nathanmarz 2018-09-06T17:23:55.000100Z

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

2018-09-06T17:26:41.000100Z

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.

2018-09-06T17:27:19.000100Z

interesting approach, thanks.

nathanmarz 2018-09-06T17:27:55.000100Z

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

nathanmarz 2018-09-06T17:29:11.000100Z

just by passing the collected vals into the view fn

2018-09-06T17:30:27.000100Z

got it, thanks

2018-09-06T17:43:57.000100Z

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

2018-09-06T17:44:36.000100Z

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

2018-09-06T17:45:22.000100Z

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

2018-09-06T17:46:08.000100Z

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

nathanmarz 2018-09-06T17:50:47.000100Z

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

nathanmarz 2018-09-06T17:54:47.000100Z

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

2018-09-06T17:55:49.000100Z

👍 Thanks