Re: Processing an array

2016-04-26 Thread Olivier Scalbert
Thanks to all of you for your interesting answers ! Olivier -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your

Re: Processing an array

2016-04-25 Thread Derek Troy-West
Hey Olivier, You can shorten the body of your function with nil-punning and in lining next-time (rather than explicitly letting it), e.g: (if (empty? events) timed-events (let [current (first events) next-time (+ (:duration current) time)] (recur

Re: Processing an array

2016-04-24 Thread Paulus Esterhazy
Here's how I would do it: ``` (defn to-timed-events [events] (->> events (reduce (fn [[acc-events acc-time] {:keys [duration], :as event}] [(conj acc-events (assoc event :time acc-time)) (+ acc-time duration)]) [[] 0]) first)) ```

Re: Processing an array

2016-04-24 Thread 676c7473
Hello Olivier, perhaps 'reductions' would be a good fit here: (let [times (reductions + 0 (map :duration events))] (into [] (map #(assoc %1 :time %2) events times))) Best, -- David -- You received this message because you are subscribed to the Google Groups "Clojure" group. To

Processing an array

2016-04-24 Thread Olivier Scalbert
Hi all, I am a Clojure beginner And I have an advice to ask ! I have an array of events: (def events [ {:id "1" :duration 100} {:id "2" :duration 200} {:id "3" :duration 300} ]) I wanted to transform this array into: [ {:id 1, :duration 100, :time 0} ; event 1 starts at 0 {:id