1 | An [iterable] is a sequence of values.<br>
|
2 | :package: [NPM](https://www.npmjs.com/package/extra-iterable),
|
3 | :smiley_cat: [GitHub](https://github.com/orgs/nodef/packages?repo_name=extra-iterable),
|
4 | :running: [RunKit](https://npm.runkit.com/extra-iterable),
|
5 | :vhs: [Asciinema](https://asciinema.org/a/339719),
|
6 | :moon: [Minified](https://www.npmjs.com/package/extra-iterable.min),
|
7 | :scroll: [Files](https://unpkg.com/extra-iterable/),
|
8 | :newspaper: [JSDoc](https://nodef.github.io/extra-iterable/),
|
9 | :blue_book: [Wiki](https://github.com/nodef/extra-iterable/wiki/).
|
10 |
|
11 | Assumption here is that an iterable can only be iterated over once. Methods
|
12 | which require multiple iterations preserve old values in a backup array using
|
13 | [many]. Many methods accept both compare and map functions, and in some cases
|
14 | using **only** a map function enables *faster comparision* (like [unique]).
|
15 | I borrowed a lot of ideas from Haskell, Elm, Python, Basic, Lodash, and other
|
16 | NPM packages. These are mentioned in references of each method.
|
17 |
|
18 | Methods as separate packages:
|
19 |
|
20 | - `@extra-iterable/swap`: use [rollup] to bundle this es module.
|
21 | - `@extra-iterable/swap.min`: use in browser ([browserify], [uglify-js]).
|
22 |
|
23 | > Stability: Experimental.
|
24 |
|
25 | <br>
|
26 |
|
27 | ```javascript
|
28 | const iterable = require("extra-iterable");
|
29 | // import * as iterable from "extra-iterable";
|
30 | // import * as iterable from "https://unpkg.com/extra-iterable@2.5.10/index.mjs"; (deno)
|
31 |
|
32 | var x = [2, 4, 6, 8];
|
33 | iterable.get(x, 1);
|
34 | // 4
|
35 |
|
36 | var x = [1, 2, 3, 4];
|
37 | [...iterable.swap(x, 0, 1)];
|
38 | // [ 2, 1, 3, 4 ]
|
39 |
|
40 | var x = [1, 2, 3];
|
41 | [...iterable.cycle(x, 0, 4)];
|
42 | // [1, 2, 3, 1]
|
43 |
|
44 | var x = [1, 2, 3, 4];
|
45 | iterable.reduce(x, (acc, v) => acc+v);
|
46 | // 10
|
47 | ```
|
48 |
|
49 | <br>
|
50 | <br>
|
51 |
|
52 |
|
53 | ## Index
|
54 |
|
55 | | Method | Action |
|
56 | | --------------------- | ---------------------------------------------- |
|
57 | | [is] | Checks if value is iterable. |
|
58 | | [get] | Gets value at index. |
|
59 | | [set] | Sets value at index. |
|
60 | | [swap] | Exchanges two values. |
|
61 | | [index] | Gets zero-based index. |
|
62 | | [indexRange] | Gets index range of part of iterable. |
|
63 | | [isEmpty] | Checks is iterable is empty. |
|
64 | | [size] | Counts the number of values. |
|
65 | | |
|
66 | | [entries] | Lists all index-value pairs. |
|
67 | | [iterator] | Gives iterator for iterable. |
|
68 | | [many] | Converts a once iterable to many. |
|
69 | | [from] | Converts iterator to iterable. |
|
70 | | |
|
71 | | [take] | Keeps first n values only. |
|
72 | | [drop] | Discards first n values only. |
|
73 | | [head] | Gets first value. |
|
74 | | [left] | Gets values from left. |
|
75 | | [concat] | Appends values from iterables. |
|
76 | | [push] | Adds values to the end. |
|
77 | | [copy] | Copies part of iterable to another. |
|
78 | | [fill] | Fills with given value. |
|
79 | | [slice] | Gets part of an iterable. |
|
80 | | [splice] | Removes or replaces existing values. |
|
81 | | |
|
82 | | [chunk] | Breaks iterable into chunks of given size. |
|
83 | | [cycle] | Gives values that cycle through an iterable. |
|
84 | | [repeat] | Repeats an iterable given times. |
|
85 | | [reverse] | Reverses the values. |
|
86 | | [rotate] | Rotates values in iterable. |
|
87 | | [interleave] | Merges values from iterables. |
|
88 | | [merge] | Merges values from sorted iterables. |
|
89 | | [flat] | Flattens nested iterable to given depth. |
|
90 | | |
|
91 | | [min] | Finds smallest entry. |
|
92 | | [max] | Finds largest entry. |
|
93 | | [range] | Finds smallest and largest entries. |
|
94 | | [count] | Counts values which satisfy a test. |
|
95 | | [partition] | Segregates values by test result. |
|
96 | | [cut] | Breaks iterable when test passes. |
|
97 | | [split] | Breaks iterable considering test as separator. |
|
98 | | [group] | Keeps similar values together and in order. |
|
99 | | [join] | Joins values together. |
|
100 | | |
|
101 | | [map] | Updates values based on map function. |
|
102 | | [filter] | Keeps the values which pass a test. |
|
103 | | [reduce] | Reduces values to a single value. |
|
104 | | [accumulate] | Produces accumulating values. |
|
105 | | [cartesianProduct] | Lists cartesian product of iterables. |
|
106 | | [zip] | Combines values from iterables. |
|
107 | | |
|
108 | | [unique] | Removes duplicate values. |
|
109 | | [union] | Gives values present in any iterable. |
|
110 | | [intersection] | Gives values present in both iterables. |
|
111 | | [difference] | Gives values not present in another iterable. |
|
112 | | [symmetricDifference] | Gives values not present in both iterables. |
|
113 | | [isUnique] | Checks if there are no duplicate values. |
|
114 | | [isDisjoint] | Checks if iterables have no value in common. |
|
115 | | |
|
116 | | [hasValue] | Checks if iterable has a value. |
|
117 | | [hasPrefix] | Checks if iterable starts with a prefix. |
|
118 | | [hasSuffix] | Checks if iterable ends with a suffix. |
|
119 | | [hasInfix] | Checks if iterable contains an infix. |
|
120 | | [hasSubsequence] | Checks if iterable has a subsequence. |
|
121 | | |
|
122 | | [isEqual] | Checks if two iterables are equal. |
|
123 | | [compare] | Compares two iterables. |
|
124 | | [search] | Finds index of first value passing a test. |
|
125 | | [scanWhile] | Scans from left, while a test passes. |
|
126 | | [find] | Finds first value passing a test. |
|
127 | | [forEach] | Calls a function for each value. |
|
128 | | [some] | Checks if any value satisfies a test. |
|
129 | | [every] | Checks if all values satisfy a test. |
|
130 |
|
131 | <br>
|
132 | <br>
|
133 |
|
134 | [![](https://img.youtube.com/vi/qgxPbqDskyw/maxresdefault.jpg)](https://www.youtube.com/watch?v=qgxPbqDskyw)
|
135 |
|
136 | [browserify]: https://www.npmjs.com/package/browserify
|
137 | [rollup]: https://www.npmjs.com/package/rollup
|
138 | [uglify-js]: https://www.npmjs.com/package/uglify-js
|
139 | [iterable]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
|
140 | [:ledger:]: https://unpkg.com/extra-iterable/
|
141 | [is]: https://github.com/nodef/extra-iterable/wiki/is
|
142 | [get]: https://github.com/nodef/extra-iterable/wiki/get
|
143 | [set]: https://github.com/nodef/extra-iterable/wiki/set
|
144 | [swap]: https://github.com/nodef/extra-iterable/wiki/swap
|
145 | [head]: https://github.com/nodef/extra-iterable/wiki/head
|
146 | [index]: https://github.com/nodef/extra-iterable/wiki/index
|
147 | [indexRange]: https://github.com/nodef/extra-iterable/wiki/indexRange
|
148 | [size]: https://github.com/nodef/extra-iterable/wiki/size
|
149 | [entries]: https://github.com/nodef/extra-iterable/wiki/entries
|
150 | [iterator]: https://github.com/nodef/extra-iterable/wiki/iterator
|
151 | [many]: https://github.com/nodef/extra-iterable/wiki/many
|
152 | [from]: https://github.com/nodef/extra-iterable/wiki/from
|
153 | [push]: https://github.com/nodef/extra-iterable/wiki/push
|
154 | [fill]: https://github.com/nodef/extra-iterable/wiki/fill
|
155 | [copy]: https://github.com/nodef/extra-iterable/wiki/copy
|
156 | [concat]: https://github.com/nodef/extra-iterable/wiki/concat
|
157 | [left]: https://github.com/nodef/extra-iterable/wiki/left
|
158 | [slice]: https://github.com/nodef/extra-iterable/wiki/slice
|
159 | [splice]: https://github.com/nodef/extra-iterable/wiki/splice
|
160 | [flat]: https://github.com/nodef/extra-iterable/wiki/flat
|
161 | [cut]: https://github.com/nodef/extra-iterable/wiki/cut
|
162 | [chunk]: https://github.com/nodef/extra-iterable/wiki/chunk
|
163 | [cycle]: https://github.com/nodef/extra-iterable/wiki/cycle
|
164 | [repeat]: https://github.com/nodef/extra-iterable/wiki/repeat
|
165 | [reverse]: https://github.com/nodef/extra-iterable/wiki/reverse
|
166 | [rotate]: https://github.com/nodef/extra-iterable/wiki/rotate
|
167 | [interleave]: https://github.com/nodef/extra-iterable/wiki/interleave
|
168 | [merge]: https://github.com/nodef/extra-iterable/wiki/merge
|
169 | [min]: https://github.com/nodef/extra-iterable/wiki/min
|
170 | [max]: https://github.com/nodef/extra-iterable/wiki/max
|
171 | [range]: https://github.com/nodef/extra-iterable/wiki/range
|
172 | [map]: https://github.com/nodef/extra-iterable/wiki/map
|
173 | [reduce]: https://github.com/nodef/extra-iterable/wiki/reduce
|
174 | [filter]: https://github.com/nodef/extra-iterable/wiki/filter
|
175 | [take]: https://github.com/nodef/extra-iterable/wiki/take
|
176 | [drop]: https://github.com/nodef/extra-iterable/wiki/drop
|
177 | [count]: https://github.com/nodef/extra-iterable/wiki/count
|
178 | [partition]: https://github.com/nodef/extra-iterable/wiki/partition
|
179 | [group]: https://github.com/nodef/extra-iterable/wiki/group
|
180 | [split]: https://github.com/nodef/extra-iterable/wiki/split
|
181 | [join]: https://github.com/nodef/extra-iterable/wiki/join
|
182 | [cartesianProduct]: https://github.com/nodef/extra-iterable/wiki/cartesianProduct
|
183 | [zip]: https://github.com/nodef/extra-iterable/wiki/zip
|
184 | [unique]: https://github.com/nodef/extra-iterable/wiki/unique
|
185 | [union]: https://github.com/nodef/extra-iterable/wiki/union
|
186 | [intersection]: https://github.com/nodef/extra-iterable/wiki/intersection
|
187 | [difference]: https://github.com/nodef/extra-iterable/wiki/difference
|
188 | [isUnique]: https://github.com/nodef/extra-iterable/wiki/isUnique
|
189 | [isDisjoint]: https://github.com/nodef/extra-iterable/wiki/isDisjoint
|
190 | [hasValue]: https://github.com/nodef/extra-iterable/wiki/hasValue
|
191 | [hasPrefix]: https://github.com/nodef/extra-iterable/wiki/hasPrefix
|
192 | [hasInfix]: https://github.com/nodef/extra-iterable/wiki/hasInfix
|
193 | [hasSuffix]: https://github.com/nodef/extra-iterable/wiki/hasSuffix
|
194 | [hasSubsequence]: https://github.com/nodef/extra-iterable/wiki/hasSubsequence
|
195 | [isEqual]: https://github.com/nodef/extra-iterable/wiki/isEqual
|
196 | [compare]: https://github.com/nodef/extra-iterable/wiki/compare
|
197 | [search]: https://github.com/nodef/extra-iterable/wiki/search
|
198 | [find]: https://github.com/nodef/extra-iterable/wiki/find
|
199 | [some]: https://github.com/nodef/extra-iterable/wiki/some
|
200 | [every]: https://github.com/nodef/extra-iterable/wiki/every
|
201 | [forEach]: https://github.com/nodef/extra-iterable/wiki/forEach
|
202 | [isEmpty]: https://github.com/nodef/extra-iterable/wiki/isEmpty
|
203 | [accumulate]: https://github.com/nodef/extra-iterable/wiki/accumulate
|
204 | [symmetricDifference]: https://github.com/nodef/extra-iterable/wiki/symmetricDifference
|
205 | [scanWhile]: https://github.com/nodef/extra-iterable/wiki/scanWhile
|