Playing with Dates20 Aug 2014 clojure
Build small self contained units of code (possibly pure) and combine them to make them do more complex stuff.
It fit nicely into what I was doing when playing with dates and generating histograms.
I had to get to a certain date in the past to start building my histogram. Libraries like moment.js make it pretty trivial:
This will give you a date 2 months ago from now.
Although this works fine and I am reasonably happy with it, I find it somewhat limiting. Using Clojure (and clj-time specifically), you could do this something like:
which is equivalent to saying
Other than being easier on eyes (matter of opinion I’ve been told), we have Clojure’s composability at work here.
months are two functions, and not literals which have a set meaning. You can write your own functions to further extend this.
Lets say I want to clamp the time to midnight (which basically means setting hours, minutes and seconds to 0). You could write a function that just does that:
Basically, just pick the year, month and day part out of the supplied time value and construct a new
date-midnight instance out of it. I can then use this function as a part of my compositional chain:
which now gives you time at midnight, 2 months ago.