-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstd.fkc
148 lines (135 loc) · 4.08 KB
/
std.fkc
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
cong map = fk(arr, f) {
cong iter = fk(arr, accumulated) {
if (len(arr) == 0) {
-> accumulated;
} else {
-> iter(rest(arr), push(accumulated, f(front(arr))));
}
}
if (!isInteger(arr)) {
-> newError("Error: `map` expects an array made of integers.");
} else {
-> iter(arr, []);
}
}
cong reduce = fk(arr, initial, f) {
cong iter = fk(arr, result) {
if (len(arr) == 0) {
-> result;
} else {
-> iter(rest(arr), f(result, front(arr)));
}
}
if (!isInteger(arr)) {
-> newError("Error: `reduce` expects an array made of integers.");
} else {
-> iter(arr, initial);
}
}
cong sum = fk(arr) {
if (!isInteger(arr)) {
-> newError("Error: `sum` expects an array made of integers.");
} else {
-> reduce(arr, 0, fk(initial, el) { -> initial + el; });
}
}
cong max = fk(arr) {
if(!isInteger(arr)) {
-> newError("Error: `max` expects an array made of integers.");
} else {
-> reduce(arr, 0, fk(initial, el) { -> if (el > initial) { el; } else { initial; } });
}
}
cong min = fk(arr) {
if(!isInteger(arr)) {
-> newError("Error: `min` expects an array made of integers.");
} else {
-> reduce(arr, 0, fk(initial, el) { -> if (el < initial) { el; } else { initial; } });
}
}
cong merge = fk(left, right, target) {
cong merged = [];
if (target == 0) {
cong mergeHelper = fk(acc, l, r) {
if (len(l) == 0) {
-> push(acc, r);
}
if (len(r) == 0) {
-> push(acc, l);
}
if (front(l) > front(r)) {
-> mergeHelper(push(acc, front(l)), rest(l), r);
} else{
-> mergeHelper(push(acc, front(r)), l, rest(r));
}
}
-> mergeHelper(merged, left, right);
} else {
cong mergeHelper = fk(acc, l, r) {
if (len(l) == 0) {
-> push(acc, r);
}
if (len(r) == 0) {
-> push(acc, l);
}
if (front(l) < front(r)) {
-> mergeHelper(push(acc, front(l)), rest(l), r);
} else{
-> mergeHelper(push(acc, front(r)), l, rest(r));
}
}
-> mergeHelper(merged, left, right);
}
}
cong sort = fk(arr, target) {
if(!isInteger(target)) {
-> newError("Error: `sort` expects a target of 0(desc) or 1(asc).");
}
if(target * (target - 1) != 0) {
-> newError("Error: `sort` expects a target of 0(desc) or 1(asc).");
}
if (!isInteger(arr)) {
-> newError("Error: `sort` expects an array made of integers.");
} else {
if (len(arr) < 2) {
-> arr;
} else {
cong middle = len(arr) / 2;
cong left = sort(subArray(arr, 0, middle), target);
cong right = sort(subArray(arr, middle, len(arr)), target);
-> merge(left, right, target);
}
}
}
cong reverse = fk(arr) {
if (!isInteger(arr)) {
-> newError("Error: `reverse` expects an array made of integers.");
} else {
cong iter = fk(arr, il, ir) {
if (il < ir) {
swap(arr, il, ir);
iter(arr, il + 1, ir - 1);
} else {
-> arr;
}
}
cong rIdx = len(arr) - 1;
-> iter(arr, 0, rIdx);
}
}
cong swap = fk(arr, il, ir) {
if (!isInteger(arr)) {
-> newError("Error: `swap` expects an array made of integers.");
} else {
cong length = len(arr);
if (il * (length - il) < 0) {
-> newError("Error: `swap` expects a valid index.");
}
if (ir * (length - ir) < 0) {
-> newError("Error: `swap` expects a valid index.");
}
cong tmp = arr[il];
set arr[il] = arr[ir];
set arr[ir] = tmp;
}
}