From d55c816533367eea9763ca0fd6ccaa0f99c640f5 Mon Sep 17 00:00:00 2001 From: darkf Date: Tue, 17 Mar 2015 00:54:48 -0700 Subject: [PATCH] improve std/ --- mods/std/list.lamb | 4 +++- mods/std/str.lamb | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 mods/std/str.lamb diff --git a/mods/std/list.lamb b/mods/std/list.lamb index 37a0967..4206024 100644 --- a/mods/std/list.lamb +++ b/mods/std/list.lamb @@ -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) -> diff --git a/mods/std/str.lamb b/mods/std/str.lamb new file mode 100644 index 0000000..e4252e6 --- /dev/null +++ b/mods/std/str.lamb @@ -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). \ No newline at end of file