improve std/

This commit is contained in:
darkf 2015-03-17 00:54:48 -07:00
parent 7179a6d818
commit d55c816533
2 changed files with 26 additions and 1 deletions

View File

@ -13,11 +13,13 @@ map(f, []) -> [].
map(f, x::xs) -> f(x) :: map(f, xs).
-- list folds
foldl(f, v, "") -> v.
foldl(f, v, []) -> v.
foldl(f, v, x::xs) -> do
foldl(f, f(v, x), xs)
end.
foldr(f, v, "") -> v.
foldr(f, v, []) -> v.
foldr(f, v, x::xs) -> do
f(x, foldr(f, v, xs))
@ -26,7 +28,7 @@ end.
sum(lst) -> foldl(\x,y -> x + y, 0, lst).
product(lst) -> foldl(\x,y -> x * y, 1, lst).
reverse(lst) -> foldl(\x,xs -> x :: xs, [], lst).
length(lst) -> foldl(\_,y -> 1 + y, 0, lst).
length(lst) -> foldl(\y,_ -> 1 + y, 0, lst).
filter(f, []) -> [].
filter(f, x::xs) ->

23
mods/std/str.lamb Normal file
View File

@ -0,0 +1,23 @@
import("std/op").
takeWhileS(f, "") -> "".
takeWhileS(f, x::xs) -> do
if f(x) == true then x :: takeWhileS(f, xs)
else ""
end.
takeUntilS(f, xs) -> takeWhileS(\x -> op\not(f(x)), xs).
dropWhileS(f, "") -> "".
dropWhileS(f, x::xs) -> do
if f(x) == true then dropWhileS(f, xs)
else x :: xs
end.
dropS(0, x) -> x.
dropS(n, "") -> "".
dropS(n, _::xs) -> dropS(n-1, xs).
takeS(0, _) -> "".
takeS(n, "") -> "".
takeS(n, x::xs) -> x :: takeS(n-1, xs).