even more stdlib
This commit is contained in:
parent
aa7a034368
commit
e22f99b796
2 changed files with 72 additions and 29 deletions
|
|
@ -32,33 +32,77 @@ drop = n list \ { n = n, list = list }
|
|||
| { n = 0, list = _ } \ list
|
||||
| { n = _, list = [x, ...xs] } \ drop (n - 1) xs;
|
||||
|
||||
# zipWith = (a \ b \ c) List a \ List b \ List c
|
||||
# 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 | False \ False | True \ True);
|
||||
| True \ y;
|
||||
|
||||
# or : Bool \ Bool \ Bool
|
||||
or = x y \ x
|
||||
| True \ True
|
||||
| False \ (y | True \ True | False \ False);
|
||||
| False \ y;
|
||||
|
||||
# not = Bool \ Bool
|
||||
# not : Bool \ Bool
|
||||
not = x \ x
|
||||
| True \ False
|
||||
| False \ True;
|
||||
|
||||
# isSome = maybe \ ...
|
||||
# isNone = maybe \ ...
|
||||
# unwrapOr = default maybe \ ...
|
||||
# isSome : Maybe a \ Bool
|
||||
isSome = a \ a
|
||||
| Some _ \ True
|
||||
| _ \ False;
|
||||
|
||||
# range = start end \ ... # [start, start+1, ..., end-1]
|
||||
# sum = list \ ...
|
||||
# any = f list \ ...
|
||||
# all = f list \ ...
|
||||
# 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;
|
||||
|
|
|
|||
35
src/test.cg
35
src/test.cg
|
|
@ -1,44 +1,43 @@
|
|||
# standard library testing
|
||||
|
||||
list = [1, 2, 3, 4, 5];
|
||||
|
||||
add1 = x \ x + 1;
|
||||
|
||||
mapped = map add1 list;
|
||||
|
||||
isEven = x \ x % 2 == 0;
|
||||
|
||||
sumList = list \ fold add 0 list;
|
||||
|
||||
filtered = filter isEven mapped;
|
||||
|
||||
summed = sumList mapped;
|
||||
|
||||
l = len list;
|
||||
|
||||
listLen = len list;
|
||||
reversed = reverse list;
|
||||
|
||||
take3 = take 3 list;
|
||||
|
||||
drop3 = drop 3 list;
|
||||
|
||||
list2 = [6, 7, 8];
|
||||
|
||||
zipped = zipWith add list list2;
|
||||
|
||||
anded = and True False;
|
||||
|
||||
ored = or True False;
|
||||
|
||||
notted = not False;
|
||||
ranged = range 3 14;
|
||||
anyEven = any isEven list;
|
||||
allEven = all isEven list;
|
||||
flattened = flatten [[1, 2, 3], [4, 5, 6]];
|
||||
contains3 = contains 3 list;
|
||||
findEven = find isEven list;
|
||||
|
||||
{
|
||||
filtered = filtered,
|
||||
summed = summed,
|
||||
l = l,
|
||||
listLen = listLen,
|
||||
reversed = reversed,
|
||||
take3 = take3,
|
||||
drop3 = drop3,
|
||||
zipped = zipped,
|
||||
anded = anded,
|
||||
ored = ored,
|
||||
notted = notted
|
||||
notted = notted,
|
||||
ranged = ranged,
|
||||
anyEven = anyEven,
|
||||
allEven = allEven,
|
||||
flattened = flattened,
|
||||
contains3 = contains3,
|
||||
findEven = findEven
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue