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 :marry "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"))
())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With