Lazy piping ES6 generators
If you familiar with languages like Elixir you should now about lazy steams. Such abstraction gives you ability to modify enless steams without calculating results on each step. For example
Today I was learning about JS generators and I was surprised that there is no such powerfull functionality in JS. Let’s try to fix it and provide some kind of lazy piping on ES6. It will be a little hard for me as I’m nore Ruby developer but any way.
As we love TDD let’s prepare our enviroment. I will use jasmine-node for running tests.
Create file main-spec.js
(or something like it, it’s just a draft). Spec suffix used to say jasmine-node
to use for testing.
and run it
Nice start. To automize this process let’s add post save hook for vim that will start tests after each file saving.
Now we need write tests for our fibonacci
generator. For our aims there is no any difference what kind of generator to use but just use simple counter is too boring.
As now our tests don’t work we need to provide implementation for fibonacci
generator. Let’s try with such example
It’s a little dirty variant but it’s very simple and passes out tests. To modify our sequence we need some function. We can use anonymous function but…
Ok, seems we have all to formulate requirements for our idea. Let’s call wrapper for generators LazyGen and define scpes for it.
So, as chaining it’s about states we need some object to store it. LazyGen will just return such object and will be not more that syntax sugar.
Now we need wrapper for our generator.
There we will store our generator. For providing ability to chain we need to return this
on each LazyObject method call. We wil l use same techinq as in previous post Currently jasmine-node will return failure with next explanation:
Message:
TypeError: LazyGen(...).map is not a function
So, to fix we need to provide map
function. We know, taht it should return self and store modificators.
Message:
TypeError: subject.next is not a function
Now we need to realize next
function. Now it seems pretty easy
With such realisation we have ability to chain modifiers in such way
Write test for it
Now we need to implement full Array interface to provide user ability using this as plain array. I will not realize all this methods here (it’s just a metter of time), but will realize take
method that retrives passed count of values from generator.
So, start from test:
and implementation:
Pretty easy. What’s next? Next we need to realize exception throwing, ability to not only map elements but filters and so on, currenlty I’m thinking about storing all passed filters in cortege {:indexNumber, :operation, :function}
and then process it, but it will be later. Now I just realized what I want and keep it so for a while.
P.S. All work on this topic is stored on github. P.P.S. Instead of using vim callback you can just pass –autotest argument into jasmine-node