Fork me on GitHub
#specter
<
2017-06-14
>
tcoupland09:06:34

looking for a 'stateful collector', for merging a sequence of values with a value within a nested structure

tcoupland09:06:44

something like this:

(use 'com.rpl.specter)

(def o {:a {:x 1}
        :b {:x 1}
        :c {:x 1}})

(def i (range 1 10))

(defcollector putseqval
  [a]
  (collect-val 
   [this structure]
   (let [v (first @a)]
     (swap! a rest)
     v)))

(transform
 [MAP-VALS MAP-VALS (putseqval (atom i))] 
 +
 o);; => {:a {:x 2}, :b {:x 3}, :c {:x 4}}

tcoupland09:06:06

anybody have any better ideas? 🙂

nathanmarz14:06:51

(setval (subselect MAP-VALS MAP-VALS) [2 3 4] o)

tcoupland14:06:58

can i work that into the transform?

tcoupland14:06:57

i want to execute a function (+) on the value in the datastructure (o) and values from the sequence (i)

nathanmarz14:06:45

transform on the subselect will be on the sequence of values

nathanmarz14:06:51

so you can combine the two sequences easily

nathanmarz14:06:46

(transform (subselect MAP-VALS MAP-VALS) (fn [s] (map + s [2 3 4])) o)

tcoupland14:06:46

clearly i do not grok subselect 🙂

tcoupland14:06:53

thank you nathan 🙇

nathanmarz14:06:58

i recommend toying around with the size of transformed sequence to fully understand its behavior

tcoupland15:06:25

do collect and subselect play nicely together?

tcoupland15:06:39

getting an arity exception in terminal*

tcoupland15:06:12

for my function i also need the map keys, trying to collect them on the way

tcoupland15:06:17

(transform [(subselect MAP-VALS (collect FIRST FIRST) MAP-VALS)]
           (fn [fnd] (prn (map (fn [[k v] r] (str k v r)) fnd  i))) 
           o)

tcoupland15:06:34

Wrong number of args (2) passed to:
   specter/fn--38572/fn/reify--38574/fn--38579

                  AFn.java:  429  clojure.lang.AFn/throwArity
                  AFn.java:   36  clojure.lang.AFn/invoke
                  AFn.java:  156  clojure.lang.AFn/applyToHelper
                  AFn.java:  144  clojure.lang.AFn/applyTo
                  core.clj:  657  clojure.core/apply
                  core.clj:  652  clojure.core/apply
                 impl.cljc:  406  com.rpl.specter.impl$terminal_STAR_/invokeStatic
                 impl.cljc:  403  com.rpl.specter.impl$terminal_STAR_/invoke
                 impl.cljc:  413  com.rpl.specter.impl$compiled_transform_STAR_$fn__36780/invoke
              specter.cljc:  673  com.rpl.specter$reify__38443$next_fn__38449/invoke

tcoupland15:06:01

the prn in the transform function get's fired and looks right, but then the exception happens

tcoupland16:06:24

hm, works in a select, but not in the transform

nathanmarz16:06:38

@tcoupland don't use value collection in subselect

nathanmarz16:06:15

i'll put a note about that in its documentation