-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgenerator-function.js
More file actions
64 lines (63 loc) · 1.42 KB
/
Copy pathgenerator-function.js
File metadata and controls
64 lines (63 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Generator functions can be tested in a number of ways
*
* @template T
* @param {T[]} array
* @yields T
* @example generateValues(["foo", "bar", "baz"]).next()
* // Call .next() immediately to check the first value
* //=> {
* value: "foo",
* done: false
* }
* @example Array.from(generateValues([42, 17]))
* // Consume the iterator into an array, and test that
* //=> [42, 17]
* @example
* // Assign the iterator to a variable and iterate as many steps as needed
* const it = generateValues(["baz", "qux"]);
* it.next();
* it.next();
* it.next();
* //=> {
* value: undefined,
* done: true
* }
*/
export function* generateValues(array) {
for (const x of array) yield x;
}
/**
* Here's old Fibonacci
*
* @param {number[]} start
* @yields {number}
* @example fibonacci().next().value
* //=> 0
* @example
* const f = fibonacci();
* f.next();
* f.next().value
* //=> 1
* @example
* const f = fibonacci();
* f.next();
* f.next();
* f.next().value
* //=> 1
* @example
* const f = fibonacci();
* [...Array(13)].map(() => f.next().value)
* //=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]
* @example
* const f = fibonacci([8, 13]);
* [...Array(13)].map(() => f.next().value)
* //=> [8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584]
*/
export function* fibonacci([last, next] = [0, 1]) {
yield last;
while (true) {
yield next;
[last, next] = [next, next + last];
}
}