Functional JavaScript, some concepts
Disclaimer: This is my first post in English. I know that my English is not very good so feel free to correct me.
Functional programming is actually one of the mainstream topic in the world of software development. JavaScript as a flexible lenguaje, allow us to use it in a functional way. In this post I will try to cover some of the basic concepts of functional programming and how to apply them in JavaScript.
- Functions are "first-class" objects
- Functions can return functions (Higher-order functions)
- Lexical closures
- Functions are "pure"
- Safe recursion
- No mutating state
Functions are "first-class" objects
A functional programming language must facilitate the use and creation of first-class. The first-class means that something has a value. For example:
var age = 27;
A first-class function is a function that can go anywhere that any other value can go. In JavaScript is easy to define functions as variables.
var sum = function (a, b) { return a + b; }
Nothing here is special, but an important feature of being first-class is the ability to pass functions as an argument.
var sum = function (a, b) { return a + b; }
var exec = function (fn, a, b) { return fn(a, b); }
exec(sum, 4, 6); // 10
Functions can return functions (Higher order functions)
As we see, functions that take functions as arguments and returns other functions are called Higher order functions.
var always = function (value) {
return function() {
return VALUE;
}
}
This is useful for illustrating closures. First, a closure will capture a single value (reference) and repeatedly return the same value.
var f = always(function(){});
f() === f(); // true
Lexical Closures
Closures and scope are one of the main concepts to understand JavaScript.
A Closure is when an inner function is able to have access to the scope of the parent function even if the parent function has been returned.
function myFunc() {
var message = 'Hello';
function getMessage() {
console.log(message);
}
getMessage();
}
myFunc
function creates a local variable message
and a nested function getMessage
. getMessage
is a Closure because he has the ability to access to the variables of the parent function.
You can learn more about Closures here.
Functions are pure
A pure function is a function that for an input returns the same output without modifying anything out of it's scope. Their purity makes them:
- Testable
- Portable
- Memoizable
- Parallelizable
var double = function (value) {
return value * 2;
}
var initialValue = 30;
double(initialValue); // 60
initialValue; // still 30
More about functional programming
Functional programming languages also worry about safe recursion and immutability. Here are some notes about it:
Safe recursion
Although you can call a function recursively, it's not compiled with tail-recursion optimizations so you have to be careful to avoid stack overflow errors.
NO mutating state
JavaScript is not immutable by default but you can write code that doesn't mutate variables. Pure functions are another way to say NO mutating state.
This is just some of the basics of functional programming with JavaScript. I will try to cover more in the future.