@@ -321,6 +321,19 @@ const mapped = foo.map(x => x * x);
321
321
var mapped = foo .map (function (x ) { return x * x; });
322
322
```
323
323
324
+ All Immutable.js collections are [ Iterable] [ Iterators ] , which allows them to be
325
+ used anywhere an Iterable is expected, such as when spreading into an Array.
326
+
327
+ <!-- runkit:activate -->
328
+ ``` js
329
+ const { List } = require (' immutable' )
330
+ const aList = List ([ 1 , 2 , 3 ])
331
+ const anArray = [ 0 , ... aList, 4 , 5 ] // [ 0, 1, 2, 3, 4, 5 ]
332
+ ```
333
+
334
+ Note: A Collection is always iterated in the same order, however that order may
335
+ not always be well defined, as is the case for the ` Map ` and ` Set ` .
336
+
324
337
[ Iterators ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol
325
338
[ Arrow Functions ] : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
326
339
[ Classes ] : http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes
@@ -504,50 +517,58 @@ Lazy Seq
504
517
--------
505
518
506
519
` Seq ` describes a lazy operation, allowing them to efficiently chain
507
- use of all the sequence methods (such as ` map ` and ` filter ` ).
520
+ use of all the higher-order collection methods (such as ` map ` and ` filter ` )
521
+ by not creating intermediate collections.
508
522
509
523
** Seq is immutable** — Once a Seq is created, it cannot be
510
524
changed, appended to, rearranged or otherwise modified. Instead, any mutative
511
- method called on a Seq will return a new Seq.
525
+ method called on a ` Seq ` will return a new ` Seq ` .
512
526
513
- ** Seq is lazy** — Seq does as little work as necessary to respond to any
514
- method call.
527
+ ** Seq is lazy** — ` Seq ` does as little work as necessary to respond to any
528
+ method call. Values are often created during iteration, including implicit
529
+ iteration when reducing or converting to a concrete data structure such as
530
+ a ` List ` or JavaScript ` Array ` .
515
531
516
- For example, the following does not perform any work, because the resulting
517
- Seq is never used :
532
+ For example, the following performs no work, because the resulting
533
+ ` Seq ` 's values are never iterated :
518
534
519
535
``` js
520
536
const { Seq } = require (' immutable' )
521
537
const oddSquares = Seq ([ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ])
522
- .filter (x => x % 2 )
538
+ .filter (x => x % 2 !== 0 )
523
539
.map (x => x * x)
524
540
```
525
541
526
- Once the Seq is used, it performs only the work necessary. In this
527
- example, no intermediate arrays are ever created, filter is called three times,
528
- and map is only called once:
542
+ Once the ` Seq ` is used, it performs only the work necessary. In this
543
+ example, no intermediate arrays are ever created, filter is called three
544
+ times, and map is only called once:
529
545
530
546
``` js
531
- console . log ( oddSquares .get (1 ) ); // 9
547
+ oddSquares .get (1 ); // 9
532
548
```
533
549
534
- Any collection can be converted to a lazy Seq with ` .toSeq ()` .
550
+ Any collection can be converted to a lazy Seq with ` Seq ()` .
535
551
536
552
<!-- runkit:activate -->
537
553
``` js
538
554
const { Map } = require (' immutable' )
539
- const seq = Map ({ a: 1 , b: 2 , c: 3 }).toSeq ()
555
+ const map = Map ({ a: 1 , b: 2 , c: 3 }
556
+ const lazySeq = Seq (map)
540
557
` ` `
541
558
542
- Seq allows for the efficient chaining of sequence operations, especially when
543
- converting to a different concrete type (such as to a JS object) :
559
+ ` Seq` allows for the efficient chaining of operations, allowing for the
560
+ expression of logic that can otherwise be very tedious :
544
561
545
562
` ` ` js
546
- seq .flip ().map (key => key .toUpperCase ()).flip ().toObject ();
547
- // { A: 1, B: 2, C: 3 }
563
+ lazySeq
564
+ .flip ()
565
+ .map (key => key .toUpperCase ())
566
+ .flip ()
567
+ // Seq { A: 1, B: 1, C: 1 }
548
568
` ` `
549
569
550
- As well as expressing logic that would otherwise seem memory-limited:
570
+ As well as expressing logic that would otherwise seem memory or time
571
+ limited, for example ` Range ` is a special kind of Lazy sequence.
551
572
552
573
<!-- runkit:activate -->
553
574
` ` ` js
@@ -557,13 +578,10 @@ Range(1, Infinity)
557
578
.map (n => - n)
558
579
.filter (n => n % 2 === 0 )
559
580
.take (2 )
560
- .reduce ((r , n ) => r * n, 1 );
581
+ .reduce ((r , n ) => r * n, 1 )
561
582
// 1006008
562
583
` ` `
563
584
564
- Note: A Collection is always iterated in the same order, however that order may
565
- not always be well defined, as is the case for the ` Map ` .
566
-
567
585
568
586
Documentation
569
587
-------------
0 commit comments