Skip to content

Commit addff95

Browse files
committed
Add more to Seq documentation and mirror to README section
Closes immutable-js#218
1 parent d6352e4 commit addff95

File tree

2 files changed

+65
-35
lines changed

2 files changed

+65
-35
lines changed

README.md

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,19 @@ const mapped = foo.map(x => x * x);
321321
var mapped = foo.map(function (x) { return x * x; });
322322
```
323323

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+
324337
[Iterators]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol
325338
[Arrow Functions]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
326339
[Classes]: http://wiki.ecmascript.org/doku.php?id=strawman:maximally_minimal_classes
@@ -504,50 +517,58 @@ Lazy Seq
504517
--------
505518

506519
`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.
508522

509523
**Seq is immutable** — Once a Seq is created, it cannot be
510524
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`.
512526

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`.
515531

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:
518534

519535
```js
520536
const { Seq } = require('immutable')
521537
const oddSquares = Seq([ 1, 2, 3, 4, 5, 6, 7, 8 ])
522-
.filter(x => x % 2)
538+
.filter(x => x % 2 !== 0)
523539
.map(x => x * x)
524540
```
525541

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:
529545

530546
```js
531-
console.log(oddSquares.get(1)); // 9
547+
oddSquares.get(1); // 9
532548
```
533549

534-
Any collection can be converted to a lazy Seq with `.toSeq()`.
550+
Any collection can be converted to a lazy Seq with `Seq()`.
535551

536552
<!-- runkit:activate -->
537553
```js
538554
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)
540557
```
541558
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:
544561
545562
```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 }
548568
```
549569
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.
551572
552573
<!-- runkit:activate -->
553574
```js
@@ -557,13 +578,10 @@ Range(1, Infinity)
557578
.map(n => -n)
558579
.filter(n => n % 2 === 0)
559580
.take(2)
560-
.reduce((r, n) => r * n, 1);
581+
.reduce((r, n) => r * n, 1)
561582
// 1006008
562583
```
563584
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-
567585
568586
Documentation
569587
-------------

type-definitions/Immutable.d.ts

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,20 +2769,21 @@ declare module Immutable {
27692769
}
27702770

27712771
/**
2772-
* Represents a sequence of values, but may not be backed by a concrete data
2773-
* structure.
2772+
* `Seq` describes a lazy operation, allowing them to efficiently chain
2773+
* use of all the higher-order collection methods (such as `map` and `filter`)
2774+
* by not creating intermediate collections.
27742775
*
27752776
* **Seq is immutable** — Once a Seq is created, it cannot be
27762777
* changed, appended to, rearranged or otherwise modified. Instead, any
27772778
* mutative method called on a `Seq` will return a new `Seq`.
27782779
*
2779-
* **Seq is lazy** — Seq does as little work as necessary to respond to any
2780+
* **Seq is lazy** — `Seq` does as little work as necessary to respond to any
27802781
* method call. Values are often created during iteration, including implicit
27812782
* iteration when reducing or converting to a concrete data structure such as
27822783
* a `List` or JavaScript `Array`.
27832784
*
27842785
* For example, the following performs no work, because the resulting
2785-
* Seq's values are never iterated:
2786+
* `Seq`'s values are never iterated:
27862787
*
27872788
* ```js
27882789
* const { Seq } = require('immutable@4.0.0-rc.7')
@@ -2791,27 +2792,38 @@ declare module Immutable {
27912792
* .map(x => x * x)
27922793
* ```
27932794
*
2794-
* Once the Seq is used, it performs only the work necessary. In this
2795-
* example, no intermediate data structures are ever created, filter is only
2796-
* called three times, and map is only called once:
2795+
* Once the `Seq` is used, it performs only the work necessary. In this
2796+
* example, no intermediate arrays are ever created, filter is called three
2797+
* times, and map is only called once:
27972798
*
2798-
* ```
2799-
* oddSquares.get(1)); // 9
2799+
* ```js
2800+
* oddSquares.get(1); // 9
28002801
* ```
28012802
*
2802-
* Seq allows for the efficient chaining of operations,
2803-
* allowing for the expression of logic that can otherwise be very tedious:
2803+
* Any collection can be converted to a lazy Seq with `Seq()`.
28042804
*
2805+
* <!-- runkit:activate -->
2806+
* ```js
2807+
* const { Map } = require('immutable')
2808+
* const map = Map({ a: 1, b: 2, c: 3 }
2809+
* const lazySeq = Seq(map)
28052810
* ```
2806-
* Seq({ a: 1, b: 1, c: 1})
2811+
*
2812+
* `Seq` allows for the efficient chaining of operations, allowing for the
2813+
* expression of logic that can otherwise be very tedious:
2814+
*
2815+
* ```js
2816+
* lazySeq
28072817
* .flip()
28082818
* .map(key => key.toUpperCase())
28092819
* .flip()
28102820
* // Seq { A: 1, B: 1, C: 1 }
28112821
* ```
28122822
*
2813-
* As well as expressing logic that would otherwise be memory or time limited:
2823+
* As well as expressing logic that would otherwise seem memory or time
2824+
* limited, for example `Range` is a special kind of Lazy sequence.
28142825
*
2826+
* <!-- runkit:activate -->
28152827
* ```js
28162828
* const { Range } = require('immutable@4.0.0-rc.7')
28172829
* Range(1, Infinity)

0 commit comments

Comments
 (0)