forked from denisidoro/navi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coll.sh
84 lines (67 loc) · 1.16 KB
/
coll.sh
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
#!/usr/bin/env bash
coll::new() {
for x in "$@"; do
echo "$x"
done
}
coll::first() {
head -n1
}
coll::rest() {
tail -n +2
}
coll::map() {
local -r fn="$1"
for x in $(cat); do
"$fn" "$x"
done
}
coll::filter() {
local -r pred="$1"
for x in $(cat); do
"$pred" "$x" && echo "$x" || true
done
}
coll::remove() {
local -r pred="$1"
for x in $(cat); do
"$pred" "$x" || echo "$x"
done
}
coll::without_empty_line() {
local -r input="$(cat)"
local -r words="$(echo "$input" | wc -w | xargs)"
if [[ $words > 0 ]]; then
echo "$input"
fi
}
coll::add() {
cat | coll::without_empty_line
for x in "$@"; do
echo "$x"
done
}
coll::reverse() {
str::reverse_lines "$@"
}
coll::set() {
sort -u
}
coll::get() {
local n="$1"
n=$((n+1))
sed "${n}q;d"
}
# TODO: implement tailrec
coll::reduce() {
local -r fn="$1"
local state="$2"
local -r coll="$(cat)"
local -r x="$(echo "$coll" | coll::first)"
if [ -z "$x" ]; then
echo "$state"
else
local -r new_state="$("$fn" "$state" "$x")"
echo "$coll" | coll::rest | coll::reduce "$fn" "$new_state"
fi
}