-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs
179 lines (160 loc) · 3.65 KB
/
funcs
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# puts $2 elements into $1.
fn buf {
gg = ();
while(! ~ $#gg $2) gg = ($gg g);
# While the length of gg is not $2, add one g to gg.
$1 = $gg
}
# think lisp, really
fn add {
buf a $1
buf b $2
# Create buffers of lengths $1 and $2, store them in a and b.
result = ($a $b)
# Concatenate the buffers.
R = $#result
# Print the length of the buffers.
}
fn sub {
buf tot $1
# Create a buffer in tot.
count = $2
# Store the amount we subtract by.
* = $tot
shift $count
# Set the positional parameters to the buffer, and then pop off count elements.
R = $#*
}
fn mult {
# multiply $1 by $2.
tot = 0
count = ()
while(! ~ $#count $2) {
buf a $tot
# Create a 'buffer', a, to hold gs of length $tot.
buf b $1
# Do the same for $1.
c = ($a $b)
# Add them together.
tot = $#c
count = ($count m)
# Increment the counter by one. No ++ here.
}
R = $tot
}
fn div {
# : is valid notation for division, and is acknowledged by ISO 80000-2.
# We can use it because it's not reserved for noop like in Unix shells.
# divide $1 by $2.
count = ()
dividend = $1
divisor = $2
buf tmp $dividend
# Create a buffer of the length of the dividend.
* = $tmp tmp = ()
# Set $* to the buffer, and unset the temporary buffer.
while (@ shift $divisor >[2]/dev/null) {
shift $divisor
count = ($count m)
}
# @ denotes a subshell, which the shift runs in.
# We check if it succeeds via the while loop, and supress errors.
# It runs inside a subshell to account for a bug in the reimplementation of rc this was written in.
# The subshell ensures the same behaviour across implementations.
R = $#count
}
fn mod {
# divide $1 by $2.
dividend = $1
divisor = $2
buf tmp $dividend
# Create a buffer of the length of the dividend.
* = $tmp tmp = ()
# Set $* to the buffer, and unset the temporary buffer.
while (@ shift $divisor >[2]/dev/null) {
shift $divisor
}
# @ denotes a subshell, which the shift runs in.
# We check if it succeeds via the while loop, and supress errors.
# It runs inside a subshell to account for a bug in the reimplementation of rc this was written in.
# The subshell ensures the same behaviour across implementations.
R = $#*
}
fn fold {
# Take an array, iterate through each element.
op = $1
tot = $2
shift 2
for (i in $*) {
$op $tot $i
tot = $R
}
R = $tot
}
fn map {
op = $1
shift 1
tot = ()
for (i) {
$op $i
tot = ($tot $R)
}
R = $tot
}
fn invert {
# Invert an array.
R = ()
for (i) R = ($i $R)
}
fn dedup {
# Deduplicate an array.
seen = ()
for (i) ~ $i $seen || seen = ($seen $i)
R = $seen
}
fn filter {
op = $1
shift 1
tmp = ()
for (i) {
$op $i
if (~ $R true) tmp = ($tmp $i)
}
R = $tmp
}
fn first {
R = $1
}
fn last {
for(R)_
}
# Some odd, annoying to categorise functions.
fn r {
# Immediately output the returned value.
$*
echo $R
}
fn list {
# Alternate syntax for defining a list.
op = $1
shift 1
$op = $*
}
fn set {
# Set the contents of $1 to the $R of $2.
res = $1
shift 1
$* $$res
$res = $R
}
# A noop operation. Always sets status to 0.
fn _{}
# Replacements for /bin/false and /bin/true. Works the same, no path lookup!
fn false {!}
fn true {}
fn match {
# Match $1 against $2. Effectively ~, but it sets $R.
if (~ $1 $2) R = true
if not R = false
}
fn unset { for (i) $i = () }