-
Notifications
You must be signed in to change notification settings - Fork 1
/
lang.proton
103 lines (79 loc) · 1.94 KB
/
lang.proton
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
import default
import time
sleep = time.sleep
!!ordinal = num => {
string = str(num)
num %= 100
if num % 10 == 1 and num != 11 string += 'st'
else if num % 10 == 2 and num != 12 string += 'nd'
else if num % 10 == 3 and num != 13 string += 'rd'
else string += 'th'
return string
}
!!map = (function, array) => [function(value) for value : array]
!!filter = (function, array) => [value for value : array if function(value)]
!!reduce = (function, array) => {
list = list(array)
if !list return 0
while (len(list) > 1) {
list[0, 1] = [function(list[0], list[1])]
}
return list[0]
}
!!digits = (base, number) => {
result = []
trunc = number % 1
number = int(number)
while number {
result.insert(0, number % base)
number //= base
}
if result result[-1] += trunc
return result
}
fallthrough = value => {
print(value)
return value
}
pyprint = print
print = *arguments => {
pyprint(*map(str, arguments))
}
select = *indices => array => {
result = []
for index : indices result.append(array[index])
if len(indices) == 1 return result[0]
return type(array)(result)
}
!!cartesianproduct = (left, right) => {
result = []
for l : left for r : right result.append((l, r))
return result
}
!!cartesianpower = (power, array) => {
if power == 0 return []
if power == 1 return array
if power == 2 return cartesianproduct(array, array)
result = []
for a : array for k : cartesianpower(power - 1, array) {result.append((a,) + k)}
return result
}
!!prefixes = array => [array[to i + 1] for i : range(len(array))]
!!suffixes = array => [array[i to] for i : range(len(array))]
pyrange = range + list
range = *a => {
if len(a) == 0 return -1
if len(a) == 1 {
a = a[0]
if a is list return max(a) - min(a)
else return pyrange(a)
}
return pyrange(*a)
}
identity = x => x
first = select(0)
last = select(-1)
!!head = (x, y) => y[to x]
!!tail = (x, y) => y[len(y) - x to]
pop = x => x[to -1]
dequeue = x => x[1 to]