Mark Needham

Thoughts on Software Development

Clojure: Converting an array/set into a hash map

with 3 comments

When I was implementing the Elo Rating algorithm a few weeks ago one thing I needed to do was come up with a base ranking for each team.

I started out with a set of teams that looked like this:

(def teams #{ "Man Utd" "Man City" "Arsenal" "Chelsea"})

and I wanted to transform that into a map from the team to their ranking e.g.

Man Utd -> {:points 1200}
Man City -> {:points 1200}
Arsenal -> {:points 1200}
Chelsea -> {:points 1200}

I had read the documentation of array-map, a function which can be used to transform a collection of pairs into a map, and it seemed like it might do the trick.

I started out by building an array of pairs using mapcat:

> (mapcat (fn [x] [x {:points 1200}]) teams)
("Chelsea" {:points 1200} "Man City" {:points 1200} "Arsenal" {:points 1200} "Man Utd" {:points 1200})

array-map constructs a map from pairs of values e.g.

> (array-map "Chelsea" {:points 1200} "Man City" {:points 1200} "Arsenal" {:points 1200} "Man Utd" {:points 1200})
("Chelsea" {:points 1200} "Man City" {:points 1200} "Arsenal" {:points 1200} "Man Utd" {:points 1200})

Since we have a collection of pairs rather than individual pairs we need to use the apply function as well:

> (apply array-map ["Chelsea" {:points 1200} "Man City" {:points 1200} "Arsenal" {:points 1200} "Man Utd" {:points 1200}])
{"Chelsea" {:points 1200}, "Man City" {:points 1200}, "Arsenal" {:points 1200}, "Man Utd" {:points 1200}}

And if we put it all together we end up with the following:

> (apply array-map (mapcat (fn [x] [x {:points 1200}]) teams))
{"Man Utd"  {:points 1200}, "Man City" {:points 1200}, "Arsenal"  {:points 1200}, "Chelsea"  {:points 1200}}

It works but the function we pass to mapcat feels a bit clunky. Since we just need to create a collection of team/ranking pairs we can use the vector and repeat functions to build that up instead:

> (mapcat vector teams (repeat {:points 1200}))
("Chelsea" {:points 1200} "Man City" {:points 1200} "Arsenal" {:points 1200} "Man Utd" {:points 1200})

And if we put the apply array-map code back in we still get the desired result:

> (apply array-map (mapcat vector teams (repeat {:points 1200})))
{"Chelsea" {:points 1200}, "Man City" {:points 1200}, "Arsenal" {:points 1200}, "Man Utd" {:points 1200}}

Alternatively we could use assoc like this:

> (apply assoc {} (mapcat vector teams (repeat {:points 1200})))
{"Man Utd" {:points 1200}, "Arsenal" {:points 1200}, "Man City" {:points 1200}, "Chelsea" {:points 1200}}

I also came across the into function which seemed useful but took in a collection of vectors:

> (into {} [["Chelsea" {:points 1200}] ["Man City" {:points 1200}] ["Arsenal" {:points 1200}] ["Man Utd" {:points 1200}] ])

We therefore need to change the code to use map instead of mapcat:

> (into {} (map vector teams (repeat {:points 1200})))
{"Chelsea" {:points 1200}, "Man City" {:points 1200}, "Arsenal" {:points 1200}, "Man Utd" {:points 1200}}

However, my favourite version so far uses the zipmap function like so:

> (zipmap teams (repeat {:points 1200}))
{"Man Utd" {:points 1200}, "Arsenal" {:points 1200}, "Man City" {:points 1200}, "Chelsea" {:points 1200}}

I’m sure there are other ways to do this as well so if you know any let me know in the comments.

Be Sociable, Share!

Written by Mark Needham

September 20th, 2013 at 9:13 pm

Posted in Clojure

Tagged with

  • Udayakumar Rayala

    Although I think zipmap function is a neat way of doing this, you can also use reduce with assoc like this:

    (reduce #(assoc %1 %2 {:points 1200}) {} teams)

  • http://www.markhneedham.com/blog Mark Needham

    @udayakumarrayala:disqus Very nice Mr Uday! That works very nicely as well.

  • Pouria Mellati

    I think you’d agree with me that the scala version below is way better:

    teams.map{_ -> Points(1200)}.toMap