diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-03-12 18:53:16 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-03-12 18:53:16 +0000 |
commit | 447cd2ba25ac0414ed7604a49484a7590f40d202 (patch) | |
tree | 082e73d0bca06f843cf018de76b0fab329d2ab20 /gen | |
parent | 80a8083b143eaa24218ec5fda16f947658fb823d (diff) | |
download | gleam_stdlib-447cd2ba25ac0414ed7604a49484a7590f40d202.tar.gz gleam_stdlib-447cd2ba25ac0414ed7604a49484a7590f40d202.zip |
list:flatten, list:append, test iodata
Diffstat (limited to 'gen')
-rw-r--r-- | gen/iodata.erl | 16 | ||||
-rw-r--r-- | gen/list.erl | 38 |
2 files changed, 50 insertions, 4 deletions
diff --git a/gen/iodata.erl b/gen/iodata.erl index 9704413..a17f87a 100644 --- a/gen/iodata.erl +++ b/gen/iodata.erl @@ -1,10 +1,11 @@ -module(iodata). -compile(no_auto_import). +-include_lib("eunit/include/eunit.hrl"). -export([prepend/2, append/2, from/1, to_string/1, byte_size/1]). prepend(A, B) -> - gleam__stdlib:iodata_concat(A, B). + gleam__stdlib:iodata_prepend(A, B). append(A, B) -> gleam__stdlib:iodata_append(A, B). @@ -17,3 +18,16 @@ to_string(A) -> byte_size(A) -> erlang:iolist_size(A). + +-ifdef(TEST). +iodata_test() -> + Iodata = (fun(Capture1) -> + prepend(Capture1, <<"H">>) + end)((fun(Capture1) -> + append(Capture1, <<" world!">>) + end)((fun(Capture1) -> + append(Capture1, <<",">>) + end)(from([<<"ello">>])))), + expect:equal(to_string(Iodata), <<"Hello, world!">>), + expect:equal(byte_size(Iodata), 13). +-endif. diff --git a/gen/list.erl b/gen/list.erl index 964dc69..7364f66 100644 --- a/gen/list.erl +++ b/gen/list.erl @@ -2,7 +2,7 @@ -compile(no_auto_import). -include_lib("eunit/include/eunit.hrl"). --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]). +-export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, map/2, do_traverse/3, traverse/2, new/0, append/2, flatten/1, foldl/3, foldr/3]). length(A) -> erlang:length(A). @@ -54,7 +54,7 @@ head(List) -> [] -> {error, empty}; - [X | Xs] -> + [X | _] -> {ok, X} end. @@ -71,7 +71,7 @@ tail(List) -> [] -> {error, empty}; - [X | Xs] -> + [_ | Xs] -> {ok, Xs} end. @@ -151,6 +151,38 @@ new_test() -> (fun(Capture1) -> expect:equal(Capture1, []) end)(new()). -endif. +append(A, B) -> + lists:append(A, B). + +-ifdef(TEST). +append_test() -> + expect:equal(append([1], [2, 3]), [1, 2, 3]). +-endif. + +do_flatten(Lists, Acc) -> + case Lists of + [] -> + Acc; + + [L | Rest] -> + do_flatten(Rest, append(Acc, L)) + end. + +flatten(Lists) -> + do_flatten(Lists, []). + +-ifdef(TEST). +flatten_test() -> + _ = (fun(Capture1) -> expect:equal(Capture1, []) end)(flatten([])), + _ = (fun(Capture1) -> expect:equal(Capture1, []) end)(flatten([[]])), + _ = (fun(Capture1) -> + expect:equal(Capture1, []) + end)(flatten([[], [], []])), + (fun(Capture1) -> + expect:equal(Capture1, [1, 2, 3, 4]) + end)(flatten([[1, 2], [], [3, 4]])). +-endif. + foldl(List, Acc, Fun) -> case List of [] -> |