Skip to content

Commit 08e8f60

Browse files
committed
add some examples
1 parent da144a8 commit 08e8f60

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

examples/001-alphabet.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { pipe, map, range } from '..';
2+
3+
const alphabet = pipe(
4+
range(0, 25),
5+
map((x: number) => String.fromCharCode(x + 65)),
6+
Array.from
7+
);
8+
9+
alphabet(); // [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ]

examples/002-random-numbers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { pipe, generate, take } from '..';
2+
3+
const randomNumberGenerator = pipe(generate(Math.random), take(3), Array.from);
4+
5+
randomNumberGenerator();
6+
// [ 0.973565686837264, 0.7129324120429745, 0.10387686881346947 ]

examples/003-fibonacci.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { pipe, generate, take } from '..';
2+
3+
const fibonacci = pipe(
4+
generate(
5+
(function() {
6+
let x = 1;
7+
let y = 1;
8+
9+
return () => {
10+
let previous = x;
11+
x = y;
12+
y += previous;
13+
return previous;
14+
};
15+
})()
16+
),
17+
take(10),
18+
Array.from
19+
);
20+
21+
fibonacci(); // [ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 ]

src/generator.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ it('should be possible to create a fibonacci iterator', () => {
2929
take(10),
3030
Array.from
3131
);
32-
expect(fibonacci(10)).toEqual([1, 1, 2, 3, 5, 8, 13, 21, 34, 55]);
32+
expect(fibonacci()).toEqual([1, 1, 2, 3, 5, 8, 13, 21, 34, 55]);
3333
});

0 commit comments

Comments
 (0)