Take Or Leave Functional Programming
I appreciate orthogonality in programming. If you name a method in your app “out,” I might expect its pair-opposite, “in” to appear. Even more interesting, I enjoy seeing the implementation of “in” referencing “out” in some way (or visa versa).
While reading docs for various “functional” JavaScript libraries, I noticed
most have a take
method. But I can’t find a leave
method anywhere.
So, here’s an introduction to leave
, what it does, why you need it, and
why it balances take
.
In a classic JavaScript scenario, the n00b functional programmer reuses the
uber-useful function sum
in a map
instead of add
because he wants to
bind a couple of other parameters.
The poor n00b expected an array [4, 5]
but instead created all kinds of errors
with ["41,2", "61,2"]
and can’t figure out why.
take
, in JavaScript, works like Array.prototype.slice
but always starting at
the 0
index of an array. So, list.take( x )
should have the same output as
list.slice( 0, x )
.
We can use leave
to “guard” the parameters passed from map
. leave
will only
leave one spot open to the signature of sum
.
For fun, click the “repl” link below and change the code to ...leave( 2 )...
. Why did that happen?
Try it in the repl