Saturday, May 30, 2015

Destructuring in Clojure and ES6

"I do not like ‘ But yet;’ it does allay
The good precedence. Fie upon ‘ But yet!’

‘But yet' is as a gaoler to bring forth
Some monstrous malefactor.
"
-- Shakespeare, Antony and Cleopatra
Act II, Scene V, Lines 50 - 53

Intro


Sometimes your cup is over flowing and all you want is a sip.  This is where destructuring comes into play.

Say you have the following data structure.


If you only needed the Hobbit's name and family you could either drill into the data structure to obtain them or you could communicate to future readers of your code (which most likely will be you) that you only need name and family from hobbit by destructuring hobbit in the function which consume it.

JavaScript


In ECMAScript 6 we could do the following:



In the JavaScript above we see that we can destructure Bilbo to get just the name and family (which is renamed to parents) using: let {name, family:parents} = bilbo;

We see also that we can set up a function which will expect data mapping which include a name and family and then use those in its code.

let hobbies = ({name, pastTime:hobbies}) => name + ' enjoys ' + hobbies.join(', ');

The nice thing about using a data map is that we can reuse the method above with different data mappings and as long as they have a mapping for a name and family we are good.  This is what Rich Hickey was talking about in Simple Made Easy.  By using data mappings as our data transfer objects we uncomplect our code and allow for more flexibility.

Clojure


In Clojure destructuring would look like this:



Like the JavaScript code above we see we can rename while destructuring.

(let [{name :name
parents :family} bilbo]
(is (= "Bilbo" name))
(is (= ["Baggins" "Took"] parents)))

We see also that we can define a function which takes a data map in which we expect a keyword of name and another of past-time.

(defn hobbies [{name :name hobbies :past-time}]
(str name " enjoys "
(clojure.string/join ", " hobbies)))

Again we see from the created of Clojure, Rich Hickey, how using a data map in our interface allows us to reuse this function on a differently structured data map as long as it has the keywords of name and past-time.

Extro


There we have it, the ability to sip with destructuring.

Monday, May 25, 2015

Living with Variadic Functions in Clojure and JavaScript

"No wonder, my lord – one lion may, when many asses do."
-- Shakespeare, A Midsummer Night's Dream
Act V, Scene I, Line 152

Splat


Often in programming (and life) you do not get exactly what you want.  Say we have a simple function which adds numbers together.  How should the input be structure?  Should we require the caller to place all the values into some type of collection?  Why can't the caller pass in the values one at a time into the function?

Splatting in JavaScript allows a function to accept a variable number of arguments, this is called a variadic function.  A variadic function allows for a variable arity, which is a fancy term for what is often called arguments.

Say we want the following:


We would like to be able to call our summer function with a variable number of arguments but we want to be able to work with the arguments like they were in a collection.  This is were splatting comes into play.



In the plain old JavaScript example we see the following:

Array.prototype.slice.call(arguments, summer.length)

This is how to do a splat in plain old JavaScript.  We are taking the argument object then we slice off the number of arguments which the function normally takes using Function.lenght, in the case of summer the Function.length value is 0.  By doing all of this we now have an array with all of the arguments passed into the summer function.  We can now call Lodash's reduce with the arguments to obtain the sum.

In the ES6 example we see we can use the new splat operation ... (also called spread) to get an array of the arguments passed in.  This is a lot easier than using the slice, arguments, and Function.length pattern that we see in the plain old JavaScript code.

Apply


How would we do something like this in Clojure?



We see in Clojure we can destruct the parameters using the & to obtain the rest of the parameters, in the case of summer & values will give us everything the function is called with.  We can then apply this collection against the + function using apply.  Apply allows use to pass in a collection as the last argument to a function which does not take collections.

In Clojure we do not need a summer function since that is exactly how the + function works.

Sunday, May 17, 2015

Intro to Higher Order Functions

"Plucking the grass to know where sits the wind"
-- Shakespeare, The Merchant of Venice
Act I, Scene I, Line 19

Intro


Step on up.  Come one, come all. See the amazing function which produces other functions.  See the marvelous function which consumes other functions.  See things which you will tell your grandkids about.

Functions Producing Functions


Our first sight is the amazing function which produces other functions.

function A produces function B


In JavaScript and Clojure we would have the following:



We see in repl.js that we are having a return b which returns the string 'Bilbo'.  Likewise in repl_es6.js we see the first lambda return another lambda which returns the string 'Bilbo'.  In repl.clj we see the function a returns the function b which returns the string "Bilbo".  Three examples of a function producing another function.

Functions Consuming Functions


Our next sight is a function which consumes another function.

function A consumes function B


In JavaScript and Clojure we would have the following:



We see in repl.js that we are having function a consume function b which when invoked returns the string 'Bilbo'.  Likewise in repl_es6.js we see the first lambda consumes the lambda passed in which when invoked returns the string 'Bilbo'.  In repl.clj we see the function a consumes the function b which when invoked returns the string "Bilbo".  Three examples of a function consuming another function.

Pluck


The next sight we see Pluck taking apart a dwarf.



In JavaScript and Clojure we would have the following:



We see in repl.js and repl_es6.js we can use lodash's Pluck function extract the name property from our dwarves.  Likewise in repl.clj we can use the Map function with the :name keyword to extract the name value form our dwarves.

Reduce


This last sight to see is a function which will Reduce its input to a single value.



In JavaScript and Clojure we would have the following:



In repl.js and repl_es6.js we can use lodash's Reduce / Foldl function to sum up the values of 1, 2, 3, and 4 to get the value of 10, we can leave out the seed value and still get 10, or we can use a seed value of 100 to get 110.  Likewise in repl.clj we can use the Reduce function to sum up the values of 1, 2, 3, and 4 to get again 10, we can also not give a seed value or given it a different value just as before.

Fin


Thank you and watch your step on the way out.