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

Hey JavaScript builders and practical programming lovers.

In a earlier article, I used to be speaking in regards to the practical programming library FxTs, which has been applied with the ES6 iterator protocol.

However you may 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 isn’t any higher approach to perceive one thing than to implement it your self.

Does everybody agree with me?

Then, let’s create a practical 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 because of 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 reminiscent of Map, Set, and Array have already got their very own iterator, to allow them to be looped and we don’t must implement them by ourselves. However, plain objects don’t have iterators, so you may’t use a for .. of assertion.

for .. of loop instance

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

It seems to be sophisticated, however truly not in case 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 methodology ought to return an object having subsequent() a technique that returns both worth: someValue, accomplished: false or accomplished: true . The subsequent methodology makes use of accomplished 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. Observe that you simply can’t use a for .. of assertion until it’s a well-formed iterator.

It’s really easy, isn’t it? There’s a fair simpler approach to make a well-formed iterator utilizing a generator operate.

To make a generator operate you need 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().

It is a key method with that we are able to create lazy map, filter and cut back.

Now that you’re aware of iterators, so let’s begin by implementing map, filter and cut back. These capabilities are the essential capabilities in practical programming as a result of many 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 nearly similar to map.

Lastly, cut back operate. For the reason that objective of cut back operate is to make a price, we don’t must implement lazy cut 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 the way to use pipe and curry operate.

Observe right here is that our curry operate can solely do 1 currying. It’s a must 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 occasions, however to facilitate operate composition with the capabilities having greater than 2 arguments reminiscent of reudce, one time currying is best.

Now that we’ve created a curry operate, let’s apply it to map, filter, take and cut back. Our full model of FP library needs to be like under.


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

Let’s check 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 may see, because of lazy analysis, we had been capable of rule out pointless operations. Nonetheless, lazy analysis is just not at all times a silver bullet in all circumstances. Non-lazy is quicker in case you merely iterate via an array to create a brand new one.

Performance test2 : non-lazy vs lazy

efficiency test2: non-lazy vs lazy

Subsequently, if you must take care of necessary efficiency points, you need to select the proper analysis methodology quite than simply selecting the lazy capabilities at all times.

On this article, we checked out the way to create a practical library from scratch utilizing iterators and mills. If you’re fascinated by a practical library utilizing lazy analysis, it’s endorsed to review the FxTs Github code.

Thanks for studying!

More Posts