Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic clojure question on sequence transformation

Tags:

clojure

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"]] ...]
like image 926
GHZ Avatar asked Jan 31 '11 11:01

GHZ


1 Answers

(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"))
 ())
like image 110
cgrand Avatar answered Sep 23 '22 15:09

cgrand