-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerators.js
More file actions
141 lines (124 loc) · 4.45 KB
/
Copy pathGenerators.js
File metadata and controls
141 lines (124 loc) · 4.45 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
"use strict";
//------------------------------------------------------------------------------
// Generator Function, Iterator Protocol
// Support for generators, a special case of Iterators containing a generator
// function, where the control flow can be paused and resumed, in order to
// produce sequence of values (either finite or infinite).
// http://es6-features.org/#GeneratorFunctionIteratorProtocol
//------------------------------------------------------------------------------
let fibonacci = {
*[Symbol.iterator]() {
let pre = 0, cur = 1
for (;;) {
[ pre, cur ] = [ cur, pre + cur ]
yield cur
}
}
}
for (let n of fibonacci) {
if (n > 1000)
break
console.log(n)
}
//------------------------------------------------------------------------------
// Generator Function, Direct Use
// Support for generator functions, a special variant of functions where the
// control flow can be paused and resumed, in order to produce sequence of
// values (either finite or infinite).
// http://es6-features.org/#GeneratorFunctionDirectUse
//------------------------------------------------------------------------------
function* range (start, end, step) {
while (start < end) {
yield start
start += step
}
}
for (let i of range(0, 10, 2)) {
console.log(i) // 0, 2, 4, 6, 8
}
//------------------------------------------------------------------------------
// Generator Matching
// Support for generator functions, i.e., functions where the control flow can
// be paused and resumed, in order to produce and spread sequence of values
// (either finite or infinite).
// http://es6-features.org/#GeneratorMatching
//------------------------------------------------------------------------------
let fibonacci = function* (numbers) {
let pre = 0, cur = 1
while (numbers-- > 0) {
[ pre, cur ] = [ cur, pre + cur ]
yield cur
}
}
for (let n of fibonacci(1000))
console.log(n)
let numbers = [ ...fibonacci(1000) ]
let [ n1, n2, n3, ...others ] = fibonacci(1000)
//------------------------------------------------------------------------------
// Generator Control-Flow
// Support for generators, a special case of Iterators where the control flow
// can be paused and resumed, in order to support asynchronous programming in
// the style of "co-routines" in combination with Promises (see below).
// [Notice: the generic async function usually is provided by a reusable
// library and given here just for better understanding. See co or Bluebird's
// coroutine in practice.]
// http://es6-features.org/#GeneratorControlFlow
//------------------------------------------------------------------------------
// async is a keyword now...
// generic asynchronous control-flow driver
function fasync (proc, ...params) {
var iterator = proc(...params)
return new Promise((resolve, reject) => {
let loop = (value) => {
let result
try {
result = iterator.next(value)
}
catch (err) {
reject(err)
}
if (result.done)
resolve(result.value)
else if ( typeof result.value === "object"
&& typeof result.value.then === "function")
result.value.then((value) => {
loop(value)
}, (err) => {
reject(err)
})
else
loop(result.value)
}
loop()
})
}
// application-specific asynchronous builder
function makeAsync (text, after) {
return new Promise((resolve, reject) => {
setTimeout(() => resolve(text), after)
})
}
// application-specific asynchronous procedure
fasync(function* (greeting) {
let foo = yield makeAsync("foo", 300)
let bar = yield makeAsync("bar", 200)
let baz = yield makeAsync("baz", 100)
return `${greeting} ${foo} ${bar} ${baz}`
}, "Hello").then((msg) => {
console.log("RESULT:", msg) // "Hello foo bar baz"
})
//------------------------------------------------------------------------------
// Generator Methods
// Support for generator methods, i.e., methods in classes and on objects, based on generator functions.
// http://es6-features.org/#GeneratorMethods
//------------------------------------------------------------------------------
class Clz {
* bar () {
// …
}
}
let Obj = {
* foo () {
// …
}
}