-
Notifications
You must be signed in to change notification settings - Fork 0
/
skip-take.spl.js
55 lines (45 loc) · 1.41 KB
/
skip-take.spl.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
const GF = require('../index');
//fixed length array samples
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
console.log('fixed length array sample')
console.log('skip', x.skip(2));
console.log('take', x.take(5));
console.log('skip-take', x.skip(3).take(4));
console.log('dropWhile', x.dropWhile(i => i < 2));
console.log('takeWhile', x.takeWhile(i => i < 2));
//fixed length generator samples
function* simple(N) {
for (let i = 0; i < N; i++)yield i;
}
console.log('fixed length generator samples')
console.log('skip', simple(10).skip(2));
console.log('take', simple(10).take(5));
console.log('skip-take', simple(10).skip(3).take(4));
//fibonacci infinite length loop generator
function* fibonacci() {
let a = 0, b = 1;
while (true) {
yield b;
c = a + b;
a = b;
b = c;
}
}
console.log('fibonacci infinite length loop generator');
console.log('skip-take', fibonacci().iskip(3).itake(4).toArray());
console.log('skip-take', fibonacci().iskip(3).take(4));
//prime infinite length loop generator
function is_prime(m) {
for (let j = 2; j <= Math.sqrt(m); j++)
if (m % j == 0) return false;
return true;
}
function* prime() {
yield 2;
for (let i = 3; ; i++)
if (is_prime(i))
yield i;
}
console.log('prime infinite length loop generator');
console.log('skip-take', prime().iskip(3).itake(4).toArray());
console.log('skip-take', prime().iskip(3).take(4));