Unlock This Lesson
1×
Become a member
to unlock all features
Autoplay

# Exploring Sequences and Range() in Immutable.js

J.S. Leonard
Immutable.js
^3.8.1

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.

### You must be a Member to view code

Access all courses and lessons, track your progress, gain confidence and expertise.

Become a Member
and unlock code for this lesson

### Transcript

00:02 Understanding immutable JS as a Map and List structures will likely take you as far as you want to go with immutable programming.

00:07 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.

00:21 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.

00:35 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.

00:54 Now, let's write an expectation, so expect sequence.get02=0That's correct, because the first item in the range will be 0the 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.

01:26 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.

01:40 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.

01:49 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.

02:05 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.

02:27 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.

02:48 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=0which 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.

03:12 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.

03:35 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.

03:53 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.

04:06 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.

04:22 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.

04:37 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.

04:56 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.

05:19 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.

05:47 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 0to 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.

06:24 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.