Understanding Immutable.js's Map() and List() structures will likely take you as far as you want to go with immutable programming. They have only small semantic differences between each other and the remaining structures in the Immutable.js family. Sequence, however, has one major difference: it's lazy--which opens a new realm of functional possibilities. Let's write a simple sequence to start.
Understanding immutable JS as a Map and List structures will likely take you as far as you want to go with immutable programming.
They have only small semantic differences between each other, and the remaining structures in immutable JS family. Sequence, however, has one major difference. It's lazy, which opens a new realm of functional possibilities.
Let's write a simple sequence to start. We're going to create a constant range of 1,000 items. Lodash's range will just return an array with 1,000 items.
Now, we're just going to create a sequence. It's pretty easy. It's very familiar what you've seen from List. We're going use the immutable name space .sequence or SEQ of and we're just going pass in the range, just like a list using the rest operator.
Now, let's write an expectation, so expect sequence.get02=0. That's correct, because the first item in the range will be 0, the last one will be 999. Let's see if the last one is 999, we can do sequence.last2=999 and we have a passing test.
All is well. That's cool. It looks just like a list. But let's demonstrate how this is actually lazy. We're going to copy this range again and demonstrate just that.
Now, we're going to create a variable called "number of operations." This number of operations is going to count how many times something's executed.
We're going to create a power of two sequence, meaning, we can build out a sequence that kind of acts like a function, but the way it maps will return a specific value,nd it doesn't return in real time. It returns it only when you ask for it.
We'll write let power of 2 = immutable.SEQ.of and we'll pass in the range. Then, here's where the fun begins. We could start chaining commands to it. Now with this sequence, we can map to it. We'll map to it and we'll pass in a function of map to take the power of 2.
The number is passed in to each map iteration, and we're just going to also increment this number of operations so you can see when these actually take place. Return num X 2. It is going to multiply each of these times 2. Simple enough.
Let's go and see what the expectation says now. Now if this were a regular loop, number of operations that have already executed, right. Let's say, 2=0, which is right, because the sequence is lazy. It hasn't actually done anything yet, because we haven't said we want X number of items.
Let's go and ask for some stuff from the sequence so it'll actually do some calculations. What we do, so go power of 2.take just like any other immutable structure 10, and we'll say 2 array, because 2 array says, OK, now, we get serious. Calculate these out and produce an array that actually has the values, essentially, caching them.
Then, we can take a look of number of operations. Now, it should equal 10 and sure enough it does. That's how sequence is lazy. This allows us to some very interesting things that are just impossible with other loops.
Let's go ahead and try and produce a buffer overflow. It's pretty easy to do with four each. You could essentially go X is less or equal to infinity and it'll all over flow.
But with sequence, we can ask for how much comes back so we'll never actually get a buff overflow. Let me show you how that works. Let power of 2 range = immutable.range 1 to infinity.
Now, range is similar to Lodash's range except now we have an immutable sequence. It's not an array. We have a range containing infinite values in a sequence...kind of neat.
Now, we can expect power of 2 range.size to equal infinity and it passes. That's pretty crazy. That's hard to achieve any other way. Let's get the first 1,000 powers.
We'll say, first 1,000 powers equal to power of 2 range.take 1,000, and then, we'll map each one of these N, which is the number in the range, and return N X 2. That'll essentially give us the power of 2.
Then we can expect first 1,000 powers.size to equal 1,000 and sure enough the test passed. That's pretty awesome. This also allows us to chain methods onto the sequence. If it's not ran in real time and we only ask for things when we need them, we can chain very complicated methods, and then, ask for them later. It's quite a nice way to work.
Let's see how that works, too. Now, we're just going to add the powers of 2. We'll do let add powers of 2 = immuatable.range 0 to infinity. Then, we're going to filter. We're going to chain on there a filter method that's going to filter out all even values. Then, we're going to map that to the power of 2, just like we did before.
Now, let's take those first 1,000 odd powers, so first 1,000 odd powers = add powers of 2.take 1,000 and this should = 3,998. So expect first 1,000 odd powers.we get the last one to = 3,998. And sure enough we have a winner.