Functional Programming

 - introduction to functional programming
 - function properties and methods

 - functions that return functions

 - callbacks

 - closures

 - immediately Invoked function expressions

 - self-defining functions
 - recursive functions

 - currying

Inheritance vs Functional Programming

The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.

https://medium.com/@cscalfani/goodbye-object-oriented-programming-a59cda4c0e53#.7nx9e59dn

What is functional programming ?!

In JavaScript, functions are first-class objects, which means that they behave the
same way as every other value. So they can have their own properties and methods,
as well as accepting other functions as parameters and being returned by other
functions. This makes them a very flexible tool to work with in JavaScript, and there
are a variety of techniques and patterns that can be used to make code cleaner.

Functions should not alter the underlying data they deal with ― they should simply
return a different value, rather than change the value itself. This is known as non-
destructive data transformation.
However ...

 

Functions should, as much as possible, only perform a single operation. They can
then be used as building blocks to create other functions that represent more complex
abstractions and extend the functionality further.

Functions that return functions

As functions are first-class objects is that they can accept a function as an argument
as well as return another function.

This is the essence of functional programming: it allows generic higher-order
functions to be used to return more specific functions based on particular parameters.

The fact that functions are first-class objects means that they can have properties
and methods themselves. For example, all functions have a length property that
returns the number of parameters the function has.

Call and Apply Methods


The call() method allows a function to be called by an object that is provided as
the first argument. Inside the body of the function, the keyword this is used to refer
to the object the function is called on.

Call and Apply Methods


The apply method works in the same way, except the arguments of the function
are provided as an array, even if there is only one argument.

This can be useful if the data you’re using as an argument is already in the form of
an array.
These two methods allow generalized functions to be written that are not tied to
specific objects by being methods of that object, giving more flexibility over the
usage of the function.

Custom Properties

There is nothing to stop from you adding your own properties to functions. For
example, you could add a description property to a function that describes what
it does.

A useful feature of this is that it provides result caching, or memoization.

If a function takes some time to compute a return value, we can save the result in
a cache property. Then if the same argument is used again later, we can return the
value from the cache, rather than having to compute the result again. For example,
say that squaring a number was an expensive computational operation that took a
long time. We could rewrite the square() function so that it saved each result in a
cache object that is a property of the function:

Callbacks

 

Event-driven Asynchronous Programming

Remember, though, that JavaScript is still single-threaded, so only one task can
happen at once. If an event takes little time to happen, it may still have to wait until
other parts of the program have executed before the callback occurs.
For example,
let’s see what happens if we set the waiting time to be zero seconds

Generalized Functions


Callbacks can be used to build more generalized functions. Instead of having lots
of specific functions, one function can be written that accepts a callback.

Closures

A closure is a reference to a free variable that was created inside the scope of another
function, but is then kept alive and used in another part of the program. They’re
one of JavaScript’s most powerful features, but they can be difficult to get your head
round initially.

Function Scope

A closure is a reference to a free variable that was created inside the scope of another
function, but is then kept alive and used in another part of the program. They’re
one of JavaScript’s most powerful features, but they can be difficult to get your head
round initially.

Immediately Invoked Function Expressions

An Immediately Invoked Function Expression (or IIFE, pronounced “iffy”) is a
function that, as the name suggests, is invoked as soon as it’s defined. This is easily
achieved by placing parentheses at the end of the function definition (remember
that we use parentheses to invoke a function). The function also has to be made into an expression, which is done by placing the whole declaration inside paren-
theses, as can be seen in this example:

Temporary Variables

There is no way to remove a variable from a scope once it’s been declared. If a
variable is only required temporarily, it may cause confusion if it’s still available
later in the code. Even worse, the name of the variable may clash with another piece
of code (an external JavaScript library, for example) resulting in errors. Placing any code that uses the temporary variable inside an IIFE will ensure that it’s only

available while the IIFE is invoked, and then it will disappear. The example that follows uses an IIFE to swap the value of two global variables, a and b.

Mimicking Block Scope

In most other languages, a variable has scope inside a code block―that’s what’s
known as block scope. But this does not happen in JavaScript; variables only have
a limited scope inside functions. This means that when a temporary variable is
created inside an if block or a for loop, the variable will still be available outside
of that block:

 

Functions that Define and Rewrite Themselves

The dynamic nature of JavaScript means that a function is able to not only call itself,
but define itself, and even redefine itself. This is done by assigning a variable to an
anonymous function that has the same name as the function.

 

Properties Will Be Lost
If any properties have previously been set on the function, these will be lost when
the function redefines itself. In the previous example, we can set a test property
and see that it no longer exists after the function has been invoked and redefined:

 

Init-Time Branching

This technique can be used with the feature detection that we discussed in the last
chapter to create functions that rewrite themselves, known as init-time branching.
This enables the functions to work more effectively in the browser, and avoid
checking for features every time they’re invoked.

 

Recursive Functions

A recursive function is one that invokes itself until a certain condition is met. It is
a useful tool to use when iterative processes are involved.

 

Currying ( no not the food curry )

Currying is a process that involves the partial application of functions. It’s named
after the logician Haskell Curry just like the programming
language Haskell is. His work on a paper by Moses Schönfinkel lead to the develop-
ment of this programming technique.

 

A function is said to be curried when not all arguments have been supplied to the
function, so it returns another function that retains the arguments given and expects
the remaining arguments. Here is a multiplier function that expects two arguments
and multiplies those numbers together.

deck

By Robert Pop