Idiomatic clojure question on sequence transformation
I am learning Clojure, and I need a push in the right direction with this problem I came up with.
I have a sequence of events. Each event includes a 'date'.
(def events
[
[1509 :marry "Catherine of Aragon"]
[1527 :unmarry "Catherine of Aragon"]
[1533 :mar开发者_如何学Pythonry "Anne Boleyn"]
[1536 :unmarry "Anne Boleyn"]
[1536 :marry "Jane Seymour"]
[1537 :unmarry "Jane Seymour"]
[1540 :marry "Anne of Cleves"]
[1540 :unmarry "Anne of Cleves"]
[1540 :marry "Catherine Howard"]
[1542 :unmarry "Catherine Howard"]
[1543 :marry "Catherine Parr"]])
I want to convert this into a lazy timeline, i.e. a sequence containing one vector per year. Starting with the year of the first event, and continuing to infinity.
[[[:marry "Catherine of Aragon"]] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [] [[:unmarry "Catherine of Aragon"]] [] [] [] [] [] [[:marry "Ane Boleyn"]] [] [] [[:unmarry "Anne Boleyn"] [:marry "Jayne Seymour"]] ...]
(def timeline
(let [events-by-year (group-by first events)]
(map #(map next (events-by-year %))
(iterate inc (reduce min (keys events-by-year))))))
Quick test:
=> (take 30 timeline)
(((:marry "Catherine of Aragon")) () () () () () () () () () () () () () () () ()
() ((:unmarry "Catherine of Aragon")) () () () () () ((:marry "Anne Boleyn")) ()
() ((:unmarry "Anne Boleyn") (:marry "Jane Seymour")) ((:unmarry "Jane Seymour"))
())
I would suggest something like:
(defn timeline
([] (timeline (ffirst *events*) *events*))
([time evts]
(let [[now later] (split-with #(= time (first %)) evts)]
(cons (map rest now)
(lazy-seq (timeline (inc time) later))))))
Test:
user> (take 30 (timeline))
(((:marry "Catherine of Aragon")) () () () () () () () () () () () () () () () () ()
((:unmarry "Catherine of Aragon")) () () () () () ((:marry "Anne Boleyn")) () ()
((:unmarry "Anne Boleyn") (:marry "Jane Seymour")) ((:unmarry "Jane Seymour")) ())
I'm assuming that the list of events is infinite as well :)
Updated with improvements, and borrowing a couple of ideas from cgrand (thanks :)
精彩评论