Created
August 27, 2013 15:58
-
-
Save swannodette/6355478 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns cljs-play.dates.core | |
(:require [goog.date.Date] | |
[goog.date.Interval] | |
[clojure.browser.repl :as repl])) | |
(repl/connect "http://localhost:9000/repl") | |
(defn date [dstr] | |
(let [[m d y] (map js/parseInt (.split dstr "/"))] | |
(goog.date.Date. y m d))) | |
(defn pad [xs n] | |
(reduce | |
(fn [v _] | |
(let [last (peek v)] | |
(conj v | |
{:group (:group last) | |
:date (doto (.clone (:date last)) | |
(.add (goog.date.Interval. 0 0 1))) | |
:value 0}))) | |
xs | |
(range n))) | |
(defn add-defaults [data] | |
(let [grouped (group-by :key data) | |
maxn (->> grouped vals (map count) (apply max))] | |
(->> (reduce | |
(fn [m [k v]] | |
(let [v (into [] (sort v))] | |
(if (= (count v) maxn) | |
(assoc m k v) | |
(assoc m k (pad v (- maxn (count v))))))) | |
{} grouped) | |
vals | |
(apply concat) | |
(sort-by :key)))) | |
(def data [{:key "Group1" :date (date "01/01/12") :value 200} | |
{:key "Group2" :date (date "01/01/12") :value 100} | |
{:key "Group1" :date (date "01/01/13") :value 400}]) | |
(comment | |
(add-defaults data) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment