(def sample ["mytitle1" "2015-02-01"
"mytitle"" "2015-03-12"
"" "2015-03-28"
"mytitle3" "2015-01-12"])
;; Turns the map above into a map, skip this if you already have a map
(def working-set
(-> sample
(partition 2) ;; Gives you [["mytitle1" "2015-02-01] ....]
(group-by first))) ;; Gives you a map of title->list of dates
(-> (fn [acc k v] (assoc acc {:title k, :events v}))
(reduce-kv {} working-set))
kl. 11:07:58 UTC+1 fredag 12. desember 2014 skrev Mathias Picker følgende:
>
> Hi all,
>
> a short question from a newbie. I have a data structure like:
>
> "mytitle1"; "2015-02-01"
> "mytitle2"; "2015-03-12"
> "";2015-03-28"
> "mytitle3; "2015-01-12"
>
> and want something like this
>
> [{:title "mytitle1" :events [{:date "2015-02-01}]}
> {:title "mytitle2" :events [{:date "2015-03-12"} {:date "2015-03-28"}]}
> {:title "mytitle3" :events [{:date "2015-01-12"}]}]
>
> I can recurse through the csv manually, but I have a dim feeling this
> might be a job for reduce, but can't wrap my head around it...
>
> Can anyone give me a hint if and how this could be done? Or just tell me
> that I'm mistaken?
>
> If I'm wrong about reduce, anything which uses part of the standard lib to
> make this a more elegant solution would be interesting.
>
>
> Thanks,
>
> Mathias
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.