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 = 0, list = _ } \ list
|
||||||
| { n = _, list = [x, ...xs] } \ drop (n - 1) xs;
|
| { 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
|
zipWith = f l1 l2 \ l1
|
||||||
| [] \ []
|
| [] \ []
|
||||||
| [x, ...xs] \ (l2
|
| [x, ...xs] \ (l2
|
||||||
| [] \ []
|
| [] \ []
|
||||||
| [y, ...ys] \ [f x y, ...zipWith f xs ys]);
|
| [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 : Bool \ Bool \ Bool
|
||||||
and = x y \ x
|
and = x y \ x
|
||||||
| False \ False
|
| False \ False
|
||||||
| True \ (y | False \ False | True \ True);
|
| True \ y;
|
||||||
|
|
||||||
# or : Bool \ Bool \ Bool
|
# or : Bool \ Bool \ Bool
|
||||||
or = x y \ x
|
or = x y \ x
|
||||||
| True \ True
|
| True \ True
|
||||||
| False \ (y | True \ True | False \ False);
|
| False \ y;
|
||||||
|
|
||||||
# not = Bool \ Bool
|
# not : Bool \ Bool
|
||||||
not = x \ x
|
not = x \ x
|
||||||
| True \ False
|
| True \ False
|
||||||
| False \ True;
|
| False \ True;
|
||||||
|
|
||||||
# isSome = maybe \ ...
|
# isSome : Maybe a \ Bool
|
||||||
# isNone = maybe \ ...
|
isSome = a \ a
|
||||||
# unwrapOr = default maybe \ ...
|
| Some _ \ True
|
||||||
|
| _ \ False;
|
||||||
|
|
||||||
# range = start end \ ... # [start, start+1, ..., end-1]
|
# isNone : Maybe a \ Bool
|
||||||
# sum = list \ ...
|
isNone = a \ a
|
||||||
# any = f list \ ...
|
| Some _ \ False
|
||||||
# all = f list \ ...
|
| _ \ 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];
|
list = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
add1 = x \ x + 1;
|
add1 = x \ x + 1;
|
||||||
|
|
||||||
mapped = map add1 list;
|
mapped = map add1 list;
|
||||||
|
|
||||||
isEven = x \ x % 2 == 0;
|
isEven = x \ x % 2 == 0;
|
||||||
|
|
||||||
sumList = list \ fold add 0 list;
|
sumList = list \ fold add 0 list;
|
||||||
|
|
||||||
filtered = filter isEven mapped;
|
filtered = filter isEven mapped;
|
||||||
|
|
||||||
summed = sumList mapped;
|
summed = sumList mapped;
|
||||||
|
listLen = len list;
|
||||||
l = len list;
|
|
||||||
|
|
||||||
reversed = reverse list;
|
reversed = reverse list;
|
||||||
|
|
||||||
take3 = take 3 list;
|
take3 = take 3 list;
|
||||||
|
|
||||||
drop3 = drop 3 list;
|
drop3 = drop 3 list;
|
||||||
|
|
||||||
list2 = [6, 7, 8];
|
list2 = [6, 7, 8];
|
||||||
|
|
||||||
zipped = zipWith add list list2;
|
zipped = zipWith add list list2;
|
||||||
|
|
||||||
anded = and True False;
|
anded = and True False;
|
||||||
|
|
||||||
ored = or True False;
|
ored = or True False;
|
||||||
|
|
||||||
notted = not 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,
|
filtered = filtered,
|
||||||
summed = summed,
|
summed = summed,
|
||||||
l = l,
|
listLen = listLen,
|
||||||
reversed = reversed,
|
reversed = reversed,
|
||||||
take3 = take3,
|
take3 = take3,
|
||||||
drop3 = drop3,
|
drop3 = drop3,
|
||||||
zipped = zipped,
|
zipped = zipped,
|
||||||
anded = anded,
|
anded = anded,
|
||||||
ored = ored,
|
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