You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
118 lines
2.5 KiB
Plaintext
118 lines
2.5 KiB
Plaintext
# TODO delete
|
|
double = a \ a * 2;
|
|
|
|
# map : (a \ b) \ List a \ List b
|
|
map = f list \ list
|
|
| [] \ []
|
|
| [x, ...xs] \ [f x, ...map f xs];
|
|
|
|
# mapWithIndex : (a \ Number \ b) \ List a \ List b
|
|
mapWithIndex = f list \
|
|
init = { i = 0, result = [] };
|
|
f2 = acc x \ { i = acc.i + 1, result = [...acc.result, f x acc.i] };
|
|
(fold f2 init list).result;
|
|
|
|
# filter : (a \ Bool) \ List a \ List a
|
|
filter = f list \ list
|
|
| [] \ []
|
|
| [x, ...xs] \ (f x
|
|
| True \ [x, ...filter f xs]
|
|
| False \ filter f xs);
|
|
|
|
# fold : (b \ a \ b) \ b \ List a \ b
|
|
fold = f acc list \ list
|
|
| [] \ acc
|
|
| [x, ...xs] \ fold f (f acc x) xs;
|
|
|
|
# reverse : List a \ List a
|
|
reverse = list \ list
|
|
| [] \ []
|
|
| [x, ...xs] \ (reverse xs) & [x];
|
|
|
|
# take : Int \ List a \ List a
|
|
take = n list \ { n = n, list = list }
|
|
| { n = _, list = [] } \ []
|
|
| { n = 0, list = _ } \ []
|
|
| { n = _, list = [x, ...xs] } \ [x, ...take (n - 1) xs];
|
|
|
|
# drop : Int \ List a \ List a
|
|
drop = n list \ { n = n, list = list }
|
|
| { n = _, list = [] } \ []
|
|
| { n = 0, list = _ } \ list
|
|
| { n = _, list = [x, ...xs] } \ drop (n - 1) xs;
|
|
|
|
# zipWith : (a \ b \ c) \ List a \ List b \ List c
|
|
zipWith = f l1 l2 \ l1
|
|
| [] \ []
|
|
| [x, ...xs] \ (l2
|
|
| [] \ []
|
|
| [y, ...ys] \ [f x y, ...zipWith f xs ys]);
|
|
|
|
# contains : a \ List a \ Bool
|
|
contains = x list \ list
|
|
| [] \ False
|
|
| [y, ...ys] \ (x == y
|
|
| True \ True
|
|
| False \ contains x ys);
|
|
|
|
# find : (a \ Bool) \ List a \ Maybe a
|
|
find = f list \ list
|
|
| [] \ None
|
|
| [x, ...xs] \ (f x
|
|
| True \ Some x
|
|
| False \ find f xs);
|
|
|
|
# flatten : List (List a) \ List a
|
|
flatten = lists \ fold cat [] lists;
|
|
|
|
# and : Bool \ Bool \ Bool
|
|
and = x y \ x
|
|
| False \ False
|
|
| True \ y;
|
|
|
|
# or : Bool \ Bool \ Bool
|
|
or = x y \ x
|
|
| True \ True
|
|
| False \ y;
|
|
|
|
# not : Bool \ Bool
|
|
not = x \ x
|
|
| True \ False
|
|
| False \ True;
|
|
|
|
# isSome : Maybe a \ Bool
|
|
isSome = a \ a
|
|
| Some _ \ True
|
|
| _ \ False;
|
|
|
|
# isNone : Maybe a \ Bool
|
|
isNone = a \ a
|
|
| Some _ \ False
|
|
| _ \ True;
|
|
|
|
# unwrapOr : a \ Maybe a \ a
|
|
unwrapOr = default x \ x
|
|
| Some val \ val
|
|
| _ \ default;
|
|
|
|
# if : Bool \ a \ a \ a
|
|
if = test a b \ test
|
|
| True \ a
|
|
| _ \ b;
|
|
|
|
# range : Int \ Int \ List Int
|
|
# [start, start+1, ..., end-1]
|
|
range = start end \ start >= end
|
|
| True \ []
|
|
| False \ [start, ...range (start + 1) end];
|
|
|
|
# sum : List Int \ Int
|
|
# TODO sum floats too? wait for typeclasses?
|
|
sum = list \ fold add 0 list;
|
|
|
|
# any : (a \ Bool) \ List a \ Bool
|
|
any = f list \ fold (acc x \ or acc (f x)) False list;
|
|
|
|
# all : (a \ Bool) \ List a \ Bool
|
|
all = f list \ fold (acc x \ and acc (f x)) True list;
|