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