Mergesort in Clojure using Post Conditionals
Whilst currently reading through The Joy of Clojure book I was introduced to the concept of pre and post-conditionals, similar to another language I have heard about called Eiffel. To experiment with this feature I decided to create a simple merge-sort algorithm implementation which provided the post invariant that its returned values were sorted by the provided predicate.
I really liked the manner in which I could use the ‘partition’ function to succinctly map the task of splitting on each co-located pair of values within a collection in code. We are able to then put this implementation into practice by optionally providing a predicate (else defaulting to ascending order).