Partition With Game of Thrones Pugs
Clojure’s partition and partition-all functions are very useful. However, I have been bitten a few times using partition when I really wanted partition-all. So to help myself and all of you to remember it, I have made some diagrams with pugs from the Game of Thrones
In code, partition takes a collection and returns a lazy sequence of lists, each containing n items.
To demonstrate this with pugs, we will partition 5 pugs into groups of twos.
This partition will give you two groups of two pugs.
Notice, (and here is the important part), the last pug is missing. The Joffrey pug is not included because partition will not include items that do not make a complete partition. In this case, because there is no group of 2 pugs for the Joffrey pug to be in, it gets dropped.
This is the thing that has bitten me in the past.
A common use for wanting to partition things is to control the number of things that you process at one time. An example of this is sending only 500 items to be processed in a batch job at one time. If you have a few thousand items to be processed, partitioning them is a good way of chuncking. However, if you have an arbitrary number of items, you most certainly want to process them all and not drop any. This is where you should use partition-all instead.
Partition-all chunks the items as well, but also includes any leftovers. Demonstrating again with pugs.
This partition-all will give you three groups of pugs.
This time pug Joffrey is not left out!
Remember, think carefully before using partition. Don’t leave a pug out.
By the way, I can’t wait till the next season of Game of Thrones. Until then ..