Create Clojure Map, advanced methods, PART 1

As I promised in the first general introduction about clojure maps I am going to show way methods to create maps.

Let’s start exploring the function that return maps.

hash-map

How you may guess by yourself hash-map takes as input an even number of argument and return a map.

array-map

Here it is something more exotic.

Let’s start introducing what an ArrayMap is. An ArrayMap is an array of key, val, key, val, etc… that does implement the whole map interface.

Because the implementation it will keep the order of the key val, however it is only suitable for very small maps.

ArrayMap is guaranteed to keep the order only when unmodified.

Those are not extremely popular structure, but they exists.

zipmap

zipmap is a very handy function, you may compare this function to the zip of your jacket, you have a sequence of little tooth from one side, another sequence of little tooth from the other side and running the zipper you map a teeth to the other.

The function takes two sequence as input, a sequence of keys and a sequence of values and return a map.

The map is as long as the shorter of the two inputs.

sorted-map

Here something very interesting.

You can think about SortedMap as a mix between HashMap and ArrayMap, with the best from the two worlds.

SortedMap keep the order of the keys, but at the same time is implemented as a tree so it is fast to look up values or modify the map.

However there are some point to keep in mind.

The order that a sorted-map keeps is the order given by the function compare this means that the keys of your map must be comparable somehow.

It intuitive to define the order between a sequence of Number, either Int, Float, etc… first the smaller then the bigger. It is still easy to define the order between strings, let’s follow the alphabet. But what is the order between a string and a number ? Which come first ?

An implication of this is that you cannot build a sorted-map which has some integer and some string as keys.

Now that we know the details that SortedMaps are based on we can move on something more practical.

Of course you can still merge, assoc and dissoc SortedMap and they will always keep their order.

SortedMap are very useful but are limited by what the function compare can takes as input, to solve this problem we can use

sorted-map-by

SortedMapBy are a generalizazion of SortedMap.

sorted-map-by takes as input also a function, a comparator, that it is used to determinate the order between the keys in your map.

A comparator is a function that takes two inputs and return > 0 if the second input is smaller than the first one < 0 otherwise or 0 if the inputs are equals.

You need to be careful writing your own comparator because if two different keys are compared equal you will loose a value from your map.

Of course if you pass compare as function in sorted-map-by you will end up with a simple SortedMap.

End

This is all I get for now, next time I will talk about other functions that return maps: bean, frequencies, group-by and clojure.set/index

Next Chapter: bean, frequencies, group-by, clojure.set/index

If you have any questions or suggestions please let me know, I am extremely interested in your opinion.

Also, if you have some topic you wish to explore more deeply feel free to require it in the comments below. (Eg. Anyone here is interested in the use of transiet ?)

I am available for freelance work, I am specialized in IoT and distributed fault tolerant systems, if you are interested in working with me you can get in touch here: simone [at] mweb [dot] biz