diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-03-05 19:11:55 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-03-05 19:47:09 +0000 |
commit | d74af16e6a527f46730c3d35554ef55677c9caf4 (patch) | |
tree | abc22ce196abb2b466421f1197a72b14185b3494 | |
parent | ea5e146cf5ffd26a1c77a6b7e3160d98fc503c3c (diff) | |
download | gleam_stdlib-d74af16e6a527f46730c3d35554ef55677c9caf4.tar.gz gleam_stdlib-d74af16e6a527f46730c3d35554ef55677c9caf4.zip |
Compile more of stdlib
-rw-r--r-- | gen/list.erl | 37 | ||||
-rw-r--r-- | src/list.gleam | 55 |
2 files changed, 64 insertions, 28 deletions
diff --git a/gen/list.erl b/gen/list.erl index ae1ef24..2d87a6a 100644 --- a/gen/list.erl +++ b/gen/list.erl @@ -1,7 +1,7 @@ -module(list). -include_lib("eunit/include/eunit.hrl"). --export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, map/2, new/0, foldl/3, foldr/3]). +-export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, map/2, do_traverse/3, traverse/2, new/0, foldl/3, foldr/3]). length(A) -> erlang:length(A). @@ -107,6 +107,41 @@ map_test() -> end([0, 4, 5, 7, 3])). -endif. +do_traverse(List, Fun, Acc) -> + case List of + [] -> + {ok, reverse(Acc)}; + + [X | Xs] -> + case Fun(X) of + {ok, Y} -> + do_traverse(Xs, Fun, [Y | Acc]); + + {error, Error} -> + {error, Error} + end + end. + +traverse(List, Fun) -> + do_traverse(List, Fun, []). + +-ifdef(TEST). +traverse_test() -> + Fun = fun(X) -> case X =:= 6 orelse X =:= 5 of + true -> + {ok, X * 2}; + + false -> + {error, X} + end end, + _ = fun(Capture1) -> + expect:equal(Capture1, {ok, [10, 12, 10, 12]}) + end(fun(Capture1) -> traverse(Capture1, Fun) end([5, 6, 5, 6])), + fun(Capture1) -> + expect:equal(Capture1, {error, 7}) + end(fun(Capture1) -> traverse(Capture1, Fun) end([6, 5, 7, 3])). +-endif. + new() -> []. diff --git a/src/list.gleam b/src/list.gleam index 338ee3c..871c980 100644 --- a/src/list.gleam +++ b/src/list.gleam @@ -134,37 +134,38 @@ test map { |> expect:equal(_, [0, 8, 10, 14, 6]) } -// pub fn do_traverse(list, fun, acc) { -// case list { -// | [] -> Ok(reverse(acc)) -// | [x | xs] -> -// case fun(x) { -// | Ok(y) -> do_traverse(xs, fun, [y | acc]) -// | Error(error) -> Error(error) -// } -// } -// } +pub fn do_traverse(list, fun, acc) { + case list { + | [] -> Ok(reverse(acc)) + | [x | xs] -> + case fun(x) { + | Ok(y) -> do_traverse(xs, fun, [y | acc]) + | Error(error) -> Error(error) + } + } +} -// pub fn traverse(list, fun) { -// do_traverse(list, fun, []) -// } +pub fn traverse(list, fun) { + do_traverse(list, fun, []) +} -// test traverse { -// let fun = fn(x) { -// case x < 6 { -// | True -> Ok(x * 2) -// | False -> Error(x) -// } -// } +test traverse { + let fun = fn(x) { + case x == 6 || x == 5 || x == 4 { + | True -> Ok(x * 2) + | False -> Error(x) + } + } -// let _ = [0, 4, 5, 6, 3] -// |> traverse(_, fun) -// |> expect:equal(_, Ok([0, 8, 10, 12, 6])) + let _ = [5, 6, 5, 6] + |> traverse(_, fun) + |> expect:equal(_, Ok([10, 12, 10, 12])) + + [4, 6, 5, 7, 3] + |> traverse(_, fun) + |> expect:equal(_, Error(7)) +} -// [0, 4, 5, 7, 3] -// |> traverse(_, fun) -// |> expect:equal(_, Error(7)) -// } // pub fn drop(list, n) { // case n <= 0 { |