A closure is the combination of a function, bundled with its lexical environment. What’s that supposed to mean? How do I create a closure? Well, you probably created closures many times without realizing it! Every time you nest a function inside another function and expose it (you return it or assign it to other functions), you create a closure. The inner function has access to any variables and parameters defined in 3 different scopes (Scope Chain):
In a previous post, I briefly mentioned pure functions when talking about Functional Programming without really digging into their meaning or their use. The time has arrived, and as you’ll see, there’s nothing to be afraid of! Pure functions are functions that:
As I anticipated in the last blog post, today we’ll talk about function composition. It’s a concept I’m very familiar with since it actually belongs to Mathematics! It refers to the action of composing multiple functions together, in order to create a new function. There are of course some conditions that you need to verify first, before trying to compose two or more functions together. In Mathematics you need to make sure that domain and codomain are properly correlated for each couple of the chain, meaning that the inner function’s codomain needs to be included in the outer function’s domain. Otherwise, you might end up trying to analyze the behavior of a function in a point that doesn’t even belong to the function’s domain, probably leading you to a mistake but most certainly making you waste time. There are ways around these issues, like using domain restrictions, but I think that enough for now! After all, the scope is still JavaScript right?!
Last week we talked about higher-order functions and I mentioned some of the most used patterns in functional programming. Today, we’ll focus on what partial application and currying are and how could we benefit by using them!
This blog post is the first of a series of technical posts that will explore some of the core concepts of JavaScript. The subject will shift from very specific cases and patterns to higher-level abstractions and concepts along the way. What I would like to talk about today are higher-order functions, and in particular, why should we use them and how they will make your code more concise, readable, reusable and easy to test.