Build a Functional Programming Library Using ES6 Iterator Protocol | by Woolim Park | Feb, 2022

Hey JavaScript builders and useful programming lovers.

In a earlier article, I used to be speaking concerning the useful programming library FxTs, which has been carried out with the ES6 iterator protocol.

However you might need the identical query as me :

What’s ES6 iterator protocol and the way it may be used ?

To reply this query, I’m going to construct our personal FP library as a result of there is no such thing as a higher method to perceive one thing than to implement it your self.

Does everybody agree with me?

Then, let’s create a useful library from scratch. We’ll name it light-fp.js. Don’t be afraid. You’ll be stunned how straightforward it’s than you assume.

Have you ever considered how for .. of can loop Map , Set not solely Array? The reply is due to the iterable protocol, which I’ll clarify now.

If we use the iterator protocol, we are able to make any object iterable over a for .. of assertion. For instance, built-in objects akin to Map, Set, and Array have already got their very own iterator, to allow them to be looped and we don’t have to implement them by ourselves. Alternatively, plain objects don’t have iterators, so you possibly can’t use a for .. of assertion.

for .. of loop instance

To make it attainable to iterate over plain objects, you need to implement customized iterators that respect the iterator protocol.

It appears difficult, however really not should you have a look at it one after the other. The primary situation for making an iterable object is to have the Image.iterator property.

Second, this technique ought to return an object having subsequent() a technique that returns both worth: someValue, achieved: false or achieved: true . The subsequent technique makes use of achieved as a flag to cease iteration within the for .. of assertion.

Third, to make a well-formed iterator like a built-in iterator, subsequent() ought to return the iterator itself. Be aware that you simply can not use a for .. of assertion except it’s a well-formed iterator.

It’s really easy, isn’t it? There may be a good simpler method to make a well-formed iterator utilizing a generator operate.

To make a generator operate it’s best to connect an asterisk(*) in entrance of the operate identify. Then, use yield key phrase for every iteration worth. This yield key phrase stops execution of generator operate, and the worth of the expression following the yield key phrase is returned each time the iterator execute subsequent().

This can be a key approach with that we are able to create lazy map, filter and scale back.

Now that you’re acquainted with iterators, so let’s begin by implementing map, filter and scale back. These capabilities are the fundamental capabilities in useful programming as a result of a lot of the capabilities are derived from these three capabilities.

Let’s take a lookmap first.

The primary is a basic map implementation that we all know and the second is the lazy map. It stops iteration at yield inside a for .. of assertion and executes f(a) operate.

Subsequent is filter. The implementation is sort of similar to map.

Lastly, scale back operate. For the reason that objective of scale back operate is to make a worth, we don’t have to implement lazy scale back.

The take operate receives a quantity as a parameter and is used to extract solely a portion of the array as a lot because the corresponding worth.

Let’s additionally make pipe and curry capabilities that assist operate composition.

Right here is an instance of methods to use pipe and curry operate.

Be aware right here is that our curry operate can solely do 1 currying. It’s important to fill in 1 parameter within the first and all the remainder of the parameters within the second. It’s attainable to make a curry operate currying a number of instances, however to facilitate operate composition with the capabilities having greater than 2 arguments akin to reudce, one time currying is best.

Now that we have now created a curry operate, let’s apply it to map, filter, take and scale back. Our full model of FP library must be like beneath.


Preparation is over. Now it’s time to check what we’ve made.

Let’s take a look at the efficiency of lazy and non-lazy capabilities. These two teams will carry out the identical operate however present utterly totally different performances.

Performance test : non-lazy vs lazy

Performace test1: non-lazy vs lazy

As you possibly can see, due to lazy analysis, we had been capable of rule out pointless operations. Nonetheless, lazy analysis isn’t at all times a silver bullet in all instances. Non-lazy is quicker should you merely iterate by means of an array to create a brand new one.

Performance test2 : non-lazy vs lazy

efficiency test2: non-lazy vs lazy

Subsequently, if you need to cope with necessary efficiency points, it’s best to select the correct analysis technique moderately than simply selecting the lazy capabilities at all times.

On this article, we checked out methods to create a useful library from scratch utilizing iterators and turbines. If you’re involved in a useful library utilizing lazy analysis, it’s endorsed to review the FxTs Github code.

Thanks for studying!

More Posts