-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
93 lines (73 loc) · 2.27 KB
/
index.ts
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
import { IO } from "./source/IO";
import { Maybe, Just, Nothing } from "./source/Maybe";
import { Task } from "./source/Task";
import * as Terminal from "./source/terminal";
function chunk<T>(items: T[], chunkSize: number, initial: T[][] = []): T[][] {
const slices = items.length / chunkSize;
if (slices === 0) return initial;
if (slices === 1) return initial.concat([items]);
const head = items.slice(0, chunkSize);
const tail = items.slice(chunkSize);
return chunk(tail, chunkSize, initial.concat([head]));
}
function map<A, B>(items: A[], transform: (value: A) => B): B[] {
return reduce<A, B[]>(
items,
(current, value) => current.concat(transform(value)),
[],
);
}
function every<A>(items: A[], predicate: (value: A) => boolean): boolean {
if (items.length < 1) return true;
return reduce(
items,
(previousPredicate, item) => previousPredicate && predicate(item),
true,
);
}
function some<T>(items: T[], predicate: (value: T) => boolean): boolean {
if (items.length < 1) return false;
return reduce(
items,
(previousPredicate, item) => previousPredicate || predicate(item),
false,
);
}
function flat<T>(items: T[][]): T[] {
return reduce<T[], T[]>(items, (current, item) => current.concat(item), []);
}
function flatMap<T, B>(items: T[][], transform: (value: T) => B): B[] {
return map(flat(items), transform);
}
function filter<T>(items: T[], predicate: (value: T) => boolean): T[] {
return reduce(
items,
(current, value) => (predicate(value) ? current.concat(value) : current),
[] as T[],
);
}
function reduce<T, B>(
items: T[],
transform: (current: B, value: T) => B,
initial: B,
): B {
if (items.length < 1) return initial;
const next = transform(initial, items[0]);
return reduce(items.slice(1, items.length), transform, next);
}
const toUpperCase = (str: string): Maybe<string> => Just(str.toUpperCase())
const safeTerminalPuts = (str: Maybe<string>): IO<void> => {
switch (str._tag_) {
case "Just":
return Terminal.puts(str.get());
case "Nothing":
return Terminal.puts("`str` is empty!");
}
}
Terminal.getInput("Qual o seu nome?\n")
.map(toUpperCase)
.map(safeTerminalPuts)
.fork(
(error) => Terminal.puts(error.message),
(result) => Terminal.puts("Fim!")
)