-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathes5-generators-part1.js
156 lines (135 loc) · 4.41 KB
/
es5-generators-part1.js
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Code Wars
// ES5 Generators - Part 1 Problem
// generator(sequencer[, arg1, arg2, ...]) receives a sequencer function to generate the sequence and returns and object with a next() method. When the next() method is invoked, the next value is generated. The method could receive as well optional arguments to be passed to the sequencer function.
function generator(sequencer) {
// capture any additional arguments passed into the generator function
var args = Array.prototype.slice.call(arguments, 1);
// return an object with a next method
return {
next: sequencer.apply(this, args)
}
}
function dummySeq() {
return function() {
return "dummy";
};
}
function factorialSeq() {
var factorial = 0;
var currNum = 1;
return function() {
if (factorial === 0) {
factorial = 1;
} else {
factorial *= currNum;
currNum++;
}
return factorial;
}
}
function fibonacciSeq() {
var currValues = [0, 1];
var temp;
return function() {
temp = currValues[1];
currValues[1] = currValues[0] + currValues[1]
currValues[0] = temp;
return temp;
}
}
function rangeSeq(start, step) {
var current;
return function() {
if (!current) {
current = start;
} else {
current += step;
}
return current;
}
}
function primeSeq() {
var currentPrime = 1;
var isPrime = true;
return function() {
// start incrementing from the current prime number, check each subsequent number until you get to the next prime number, and then store that as the current prime and return it
while (true) {
// increment the number being checked
currentPrime++;
// reset the prime flag
isPrime = true;
// determine if the number being checked is prime
for (var i = 2; i <= Math.sqrt(currentPrime); i++) {
// if not prime then change flag
if (currentPrime % i === 0) {
isPrime = false;
}
}
// if the number being checked is prime then return it
if (isPrime) {
return currentPrime;
}
}
}
}
function partialSumSeq() {
// closure variable for the partial sum so far
var sum = 0;
// closure variable to store the passed in arguments
var args = Array.prototype.slice.call(arguments);
console.log('args:', args);
return function() {
if (args.length > 0) {
sum += args.shift();
return sum;
} else {
throw new Error('No more arguments');
}
}
}
// test
console.log("Test dummy generator");
var seq = generator(dummySeq);
console.log("seq.next():", seq.next());
console.log("seq.next():", seq.next());
console.log("seq.next():", seq.next());
console.log("ES5 Simple Generators");
console.log("Test factorial generator");
var seq = generator(factorialSeq);
console.log('seq.next():', seq.next()); // 0! = 1
console.log('seq.next():', seq.next()); // 1! = 1
console.log('seq.next():', seq.next()); // 2! = 2
console.log('seq.next():', seq.next()); // 3! = 6
console.log('seq.next():', seq.next()); // 4! = 6
console.log("Test Fibonacci generator");
var seq = generator(fibonacciSeq);
console.log('seq.next():', seq.next()); // fib(0) = 1
console.log('seq.next():', seq.next()); // fib(1) = 1
console.log('seq.next():', seq.next()); // fib(2) = 2
console.log('seq.next():', seq.next()); // fib(3) = 3
console.log('seq.next():', seq.next()); // fib(4) = 5
console.log('seq.next():', seq.next()); // fib(5) = 8
console.log('seq.next():', seq.next()); // fib(6) = 13
console.log("Test Range generator");
var seq = generator(rangeSeq, 5, 3); // 5,8,11,14,17
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log("Test Prime Numbers generator");
var seq = generator(primeSeq);
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log("Test partial sum generator");
var seq = generator(partialSumSeq, -1, 4, 2, 5);
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next());
console.log('seq.next():', seq.next()); // End of sequence
// console.log('seq.next():', seq.next()); // Beyond end of sequence throw error