In applying the Fusion technique, you can only use it if all the functions have the same shape of arguments and the same shape of return. Here’s our example back there
You can see that our mapper functions have the same shape — both accept a number and they both return the same shape. A sum and a product.
That’s the Fusion technique. For us to “fuse” the functions or compose them, we have to follow a rule. A rule that in order for us to fuse or compose our functions they should have the same function shape. On our example, the
multiplyBy2 both have the same shape and that’s why we were able to take advantage of composition.
But, what if there’s an additional requirement? Let’s say we need to filter our result by only getting the numbers below 10 and get the total of all the numbers?
Okay, I hear you. We will add
Array.prototype.filter() to remove other items since we only need the items that are below 10 and an
Array.prototype.reduce() to get the total of all the numbers. That’s actually correct!
But, this approach suffers also from the fact that on each chain layer, it will create a new copy of the data and iterate on each item again to apply the function.
Maybe you are now starting to wonder, is it possible to combine
Array.prototype.reduce() into a single call to avoid creating intermediary copies of data on each layer?
The answer is YES and that’s where Transduction will come! That is our goal, to put those
Array.prototype.reduce() into a single call.
Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.
Functional programming(FP) is a programming paradigm where we think of code as a function. How fun is that everything is a function. Functional programming is not a new concept but it is gaining its popularity, Even Object-oriented programming languages are trying to have functional concepts. For eg: Lambda functions in Java.
<p>Other then the syntactical differences. The main difference is the way the this keyword behaves? In an arrow function, the this keyword remains the same throughout the life-cycle of the function and is always bound to the value of this in the...