Repository navigation
Expand file tree
/
Copy pathstdplus.ks
More file actions
190 lines (154 loc) · 4.58 KB
/
Copy pathstdplus.ks
File metadata and controls
190 lines (154 loc) · 4.58 KB
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
180
181
182
183
184
185
186
187
188
189
190
module:
use std.fmt.write;
use std.StringBuilder;
const UInt32 = Int32;
const CharPlus = (
module:
# Unicode Control Characters
# https://www.compart.com/en/unicode/category/Cc
# First and last C0 Control characters
const NUL :: Char = (@eval Char.from_code(0));
const US :: Char = (@eval Char.from_code(31));
# First and last C1 Control characters
const DEL :: Char = (@eval Char.from_code(127));
const APC :: Char = (@eval Char.from_code(159));
const is_control = (c :: &Char) -> Bool => (
let c = Char.code(c^);
# C0 control codes
(c >= (@eval Char.code(NUL)) and c <= (@eval Char.code(US))) or
# C1 control codes
(c >= (@eval Char.code(DEL)) and c <= (@eval Char.code(APC)))
);
const parse = [T] (c :: Char) -> T => (
String.parse[T](&StringPlus.of_char(c) |> as_str)
);
);
const Option = (
module:
use std.Option.*;
# https://github.com/rust-lang/rust/pull/94317 :(
const inspect_none = [T] (opt :: Option[T], f :: () -> ()) -> Option[T] => match opt with (
| :Some x => :Some x
| :None => (
f();
:None
)
);
const map_or_else = [T, U] (opt :: Option[T], default :: () -> U, f :: T -> U) -> U => (
match opt with (
| :Some x => f(x)
| :None => default()
)
);
const is_some_and = [T] (opt :: Option[T], f :: T -> Bool) -> Bool => (
match opt with (
| :Some x => f(x)
| :None => false
)
);
const expect = [T] (opt :: Option[T], msg :: &str) -> T => match opt with (
| :Some x => x
| :None => panic(&format!("unwrapped :None: \(msg)") |> as_str)
);
);
const StringPlus = (
module:
const of_char = (c :: Char) => (Char as ToString).to_string(c);
const repeat = (self :: &str, times :: UInt32) -> String => (
if times == 0 then (
String.from_str("")
) else (
let mut new_str = StringBuilder.new();
for _ in 0..times do (
&mut new_str |> StringBuilder.add_str(self);
);
new_str |> StringBuilder.into_string
)
);
const is_empty = (s :: &str) => String.length(s) == 0;
const index_of_pred = (s :: &str, predicate :: Char -> Bool) -> Int32 => with_return (
for { i, c_at_i } in String.iteri(s) do (
if predicate(c_at_i) then (
return i;
);
);
-1
);
const rev = (s :: &str) -> String => with_return (
let mut new = StringBuilder.new();
for i in 0..String.length(s) do (
write!(&mut new, "\(String.at(s, String.length(s) - i - 1) |> of_char)");
);
new |> StringBuilder.into_string
)
);
const BoolPlus = (
module:
# i just want to pattern match on booleans
const t = newtype (
| :True
| :False
);
const to_t = (b :: Bool) -> t => if b then :True else :False;
const then_some = [T] (b :: Bool, t :: T) -> Option.t[T] => (
if b then (
:Some t
) else (
:None
)
);
);
const List = (
module:
use std.collections.ArrayList.*;
const last = [T] (list :: &t[T]) -> Option.t[type (&T)] => (
let len = length(list);
if len == 0 then (
:None
) else (
:Some (list |> at(len - 1))
)
);
const last_mut = [T] (list :: &mut t[T]) -> Option.t[type (&mut T)] => (
let len = length(&list^);
if len == 0 then (
:None
) else (
:Some (list |> at_mut(len - 1))
)
);
const iteri = [T] (
a :: &ArrayList.t[T]
) -> std.iter.Iterable[type { UInt32, type (&T) }] => (
let mut i = 0;
{
.iter = f => (
iter(a).iter(item => (
let ret = f({i, item});
i += 1;
ret
))
),
}
);
const is_empty = [T] (list :: &t[T]) -> Bool => length(list) == 0;
const find_map = [T, U] (list :: &t[T], f :: type (&T) -> Option.t[U]) -> Option.t[U] => with_return (
for item in iter(list) do (
if f(item) is :Some u then (
return :Some u
);
);
:None
);
);
const Tup = (
module:
const fst = [T] (tup :: {T, T}) -> T => tup.0;
);
const Default = [Self] newtype {
.default :: () -> Self,
};
const default = [T] () -> T => (T as Default).default();
impl Bool as Default = {
.default = () => true,
};