diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-03-23 10:58:52 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-03-23 10:58:52 +0000 |
commit | 27fec2149c765de657d45382af91c361eb3c953c (patch) | |
tree | 2736c4e1320c7f6b144071b2a11e10df598982ce | |
parent | 6af3f431609b2c530ae3f8598ac16815e26748d2 (diff) | |
download | gleam_stdlib-27fec2149c765de657d45382af91c361eb3c953c.tar.gz gleam_stdlib-27fec2149c765de657d45382af91c361eb3c953c.zip |
stdlib additions
-rw-r--r-- | gen/list.erl | 31 | ||||
-rw-r--r-- | gen/tuple.erl | 37 | ||||
-rw-r--r-- | src/list.gleam | 35 | ||||
-rw-r--r-- | src/tuple.gleam | 50 |
4 files changed, 151 insertions, 2 deletions
diff --git a/gen/list.erl b/gen/list.erl index a5df23e..1567b98 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, filter/2, map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, foldl/3, foldr/3]). +-export([length/1, reverse/1, is_empty/1, has_member/2, head/1, tail/1, filter/2, map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, foldl/3, foldr/3, find/2]). length(A) -> erlang:length(A). @@ -264,3 +264,32 @@ foldr(List, Acc, Fun) -> foldr_test() -> expect:equal(foldr([1, 2, 3], [], fun(X, Acc) -> [X | Acc] end), [1, 2, 3]). -endif. + +find(Haystack, F) -> + case Haystack of + [] -> + {error, not_found}; + + [X | Rest] -> + case F(X) of + {ok, X1} -> + {ok, X1}; + + _ -> + find(Rest, F) + end + end. + +-ifdef(TEST). +find_test() -> + F = fun(X) -> case X of + 2 -> + {ok, 4}; + + _ -> + {error, not_found} + end end, + expect:equal(find([1, 2, 3], F), {ok, 4}), + expect:equal(find([1, 3, 2], F), {ok, 4}), + expect:equal(find([1, 3], F), {error, not_found}). +-endif. diff --git a/gen/tuple.erl b/gen/tuple.erl index 270bb29..ec38901 100644 --- a/gen/tuple.erl +++ b/gen/tuple.erl @@ -2,7 +2,16 @@ -compile(no_auto_import). -include_lib("eunit/include/eunit.hrl"). --export([first/1, second/1]). +-export([new/2, first/1, second/1, swap/1, fetch/2]). + +new(A, B) -> + {A, B}. + +-ifdef(TEST). +new_test() -> + expect:equal(new(1, 2), {1, 2}), + expect:equal(new(2, <<"3">>), {2, <<"3">>}). +-endif. first(Tup) -> {A, _} = Tup, @@ -21,3 +30,29 @@ second(Tup) -> second_test() -> expect:equal(second({1, 2}), 2). -endif. + +swap(Tup) -> + {A, B} = Tup, + {B, A}. + +-ifdef(TEST). +swap_test() -> + expect:equal(swap({1, <<"2">>}), {<<"2">>, 1}). +-endif. + +fetch(Haystack, Needle) -> + list:find(Haystack, fun(Tuple) -> case first(Tuple) =:= Needle of + true -> + {ok, second(Tuple)}; + + false -> + {error, []} + end end). + +-ifdef(TEST). +fetch_test() -> + Proplist = [{0, <<"1">>}, {1, <<"2">>}], + expect:equal(fetch(Proplist, 0), {ok, <<"1">>}), + expect:equal(fetch(Proplist, 1), {ok, <<"2">>}), + expect:is_error(fetch(Proplist, 2)). +-endif. diff --git a/src/list.gleam b/src/list.gleam index 6687acf..12b7cad 100644 --- a/src/list.gleam +++ b/src/list.gleam @@ -3,6 +3,9 @@ import expect pub enum Empty = | Empty +pub enum NotFound = + | NotFound + // Using the Erlang C BIF implementation. // pub external fn length(List(a)) -> Int = "erlang" "length" @@ -279,3 +282,35 @@ test foldr { |> foldr(_, [], fn(x, acc) { [x | acc] }) |> expect:equal(_, [1, 2, 3]) } + +pub fn find(haystack, f) { + case haystack { + | [] -> Error(NotFound) + | [x | rest] -> + case f(x) { + | Ok(x) -> Ok(x) + | _ -> find(rest, f) + } + } +} + +test find { + let f = fn(x) { + case x { + | 2 -> Ok(4) + | _ -> Error(NotFound) + } + } + + [1, 2, 3] + |> find(_, f) + |> expect:equal(_, Ok(4)) + + [1, 3, 2] + |> find(_, f) + |> expect:equal(_, Ok(4)) + + [1, 3] + |> find(_, f) + |> expect:equal(_, Error(NotFound)) +} diff --git a/src/tuple.gleam b/src/tuple.gleam index d437a9f..5da1810 100644 --- a/src/tuple.gleam +++ b/src/tuple.gleam @@ -1,4 +1,18 @@ import expect +import result +import list + +pub fn new(a, b) { + {a, b} +} + +test new { + new(1, 2) + |> expect:equal(_, {1, 2}) + + new(2, "3") + |> expect:equal(_, {2, "3"}) +} pub fn first(tup) { let {a, _} = tup @@ -21,3 +35,39 @@ test second { |> second |> expect:equal(_, 2) } + +pub fn swap(tup) { + let {a, b} = tup + {b, a} +} + +test swap { + {1, "2"} + |> swap + |> expect:equal(_, {"2", 1}) +} + +pub fn fetch(haystack, needle) { + list:find(haystack, fn(tuple) { + case first(tuple) == needle { + | True -> Ok(second(tuple)) + | False -> Error([]) + } + }) +} + +test fetch { + let proplist = [{0, "1"}, {1, "2"}] + + proplist + |> fetch(_, 0) + |> expect:equal(_, Ok("1")) + + proplist + |> fetch(_, 1) + |> expect:equal(_, Ok("2")) + + proplist + |> fetch(_, 2) + |> expect:is_error +} |