Mark Needham

Thoughts on Software Development

Clojure: Destructuring group-by’s output

with 2 comments

One of my favourite features of Clojure is that it allows you to destructure a data structure into values that are a bit easier to work with.

I often find myself referring to Jay Fields’ article which contains several examples showing the syntax and is a good starting point.

One recent use of destructuring I had was where I was working with a vector containing events like this:

user> (def events [{:name "e1" :timestamp 123} {:name "e2" :timestamp 456} {:name "e3" :timestamp 789}])

I wanted to split the events in two – those containing events with a timestamp greater than 123 and those less than or equal to 123.

After remembering that the function I wanted was group-by and not partition-by (I always make that mistake!) I had the following:

user> (group-by #(> (->> % :timestamp) 123) events)
{false [{:name "e1", :timestamp 123}], true [{:name "e2", :timestamp 456} {:name "e3", :timestamp 789}]}

I wanted to get 2 vectors that I could pass to the web page and this is fairly easy with destructuring:

user> (let [{upcoming true past false} (group-by #(> (->> % :timestamp) 123) events)] 
       (println upcoming) (println past))
[{:name e2, :timestamp 456} {:name e3, :timestamp 789}]
[{:name e1, :timestamp 123}]
nil

Simple!

Be Sociable, Share!

Written by Mark Needham

May 31st, 2014 at 12:03 am

Posted in Clojure

Tagged with

  • http://jonathanwinandy.fr/ Jonathan Winandy

    Hi ! Their is a

  • Logan

    If your grouping function returns :upcoming/:past rather than true/false, the map returned from group-by has more intuitive keys and be destructured more naturally as {:keys [upcoming past]}