diff options
-rw-r--r-- | gen/any.erl | 9 | ||||
-rw-r--r-- | gen/bool.erl | 53 | ||||
-rw-r--r-- | gen/expect.erl | 18 | ||||
-rw-r--r-- | gen/iodata.erl | 18 | ||||
-rw-r--r-- | gen/list.erl | 152 | ||||
-rw-r--r-- | gen/map.erl | 80 | ||||
-rw-r--r-- | gen/order.erl | 126 | ||||
-rw-r--r-- | gen/result.erl | 150 | ||||
-rw-r--r-- | src/bool.gleam | 93 | ||||
-rw-r--r-- | src/list.gleam | 295 | ||||
-rw-r--r-- | src/map.gleam | 236 | ||||
-rw-r--r-- | src/order.gleam | 56 | ||||
-rw-r--r-- | src/result.gleam | 25 |
13 files changed, 952 insertions, 359 deletions
diff --git a/gen/any.erl b/gen/any.erl new file mode 100644 index 0000000..9ea0087 --- /dev/null +++ b/gen/any.erl @@ -0,0 +1,9 @@ +-module(any). + +-export([from/1, unsafeCoerce/1]). + +from(A) -> + gleam__stdlib:identity(A). + +unsafeCoerce(A) -> + gleam__stdlib:identity(A). diff --git a/gen/bool.erl b/gen/bool.erl new file mode 100644 index 0000000..52a9379 --- /dev/null +++ b/gen/bool.erl @@ -0,0 +1,53 @@ +-module(bool). +-include_lib("eunit/include/eunit.hrl"). + +-export([max/2, min/2, to_int/1]). + +max(A, B) -> + case A of + true -> + true; + + false -> + B + end. + +-ifdef(TEST). +max_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, true) end(max(true, true)), + _ = fun(Capture1) -> expect:equal(Capture1, true) end(max(true, false)), + _ = fun(Capture1) -> expect:equal(Capture1, false) end(max(false, false)), + fun(Capture1) -> expect:equal(Capture1, true) end(max(false, true)). +-endif. + +min(A, B) -> + case A of + false -> + false; + + true -> + B + end. + +-ifdef(TEST). +min_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, true) end(min(true, true)), + _ = fun(Capture1) -> expect:equal(Capture1, false) end(min(true, false)), + _ = fun(Capture1) -> expect:equal(Capture1, false) end(min(false, false)), + fun(Capture1) -> expect:equal(Capture1, false) end(min(false, true)). +-endif. + +to_int(Bool) -> + case Bool of + false -> + 0; + + true -> + 1 + end. + +-ifdef(TEST). +to_int_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, 1) end(to_int(true)), + fun(Capture1) -> expect:equal(Capture1, 0) end(to_int(false)). +-endif. diff --git a/gen/expect.erl b/gen/expect.erl new file mode 100644 index 0000000..2152c4f --- /dev/null +++ b/gen/expect.erl @@ -0,0 +1,18 @@ +-module(expect). + +-export([equal/2, not_equal/2, true/1, false/1, fail/0]). + +equal(A, B) -> + gleam__stdlib:expect_equal(A, B). + +not_equal(A, B) -> + gleam__stdlib:expect_not_equal(A, B). + +true(A) -> + gleam__stdlib:expect_true(A). + +false(A) -> + gleam__stdlib:expect_false(A). + +fail() -> + true(false). diff --git a/gen/iodata.erl b/gen/iodata.erl new file mode 100644 index 0000000..0eaaca2 --- /dev/null +++ b/gen/iodata.erl @@ -0,0 +1,18 @@ +-module(iodata). + +-export([prepend/2, append/2, from/1, to_string/1, byte_size/1]). + +prepend(A, B) -> + gleam__stdlib:iodata_prepend(A, B). + +append(A, B) -> + gleam__stdlib:iodata_append(A, B). + +from(A) -> + gleam__stdlib:identity(A). + +to_string(A) -> + erlang:iolist_to_binary(A). + +byte_size(A) -> + erlang:iolist_size(A). diff --git a/gen/list.erl b/gen/list.erl new file mode 100644 index 0000000..ae1ef24 --- /dev/null +++ b/gen/list.erl @@ -0,0 +1,152 @@ +-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]). + +length(A) -> + erlang:length(A). + +-ifdef(TEST). +length_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, 0) end(length([])), + _ = fun(Capture1) -> expect:equal(Capture1, 1) end(length([1])), + _ = fun(Capture1) -> expect:equal(Capture1, 2) end(length([1, 1])), + fun(Capture1) -> expect:equal(Capture1, 3) end(length([1, 1, 1])). +-endif. + +reverse(A) -> + erlang:reverse(A). + +-ifdef(TEST). +reverse_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, 0) end(length([])), + fun(Capture1) -> expect:equal(Capture1, 5) end(length([1, 2, 3, 4, 5])). +-endif. + +is_empty(List) -> + List =:= []. + +-ifdef(TEST). +is_empty_test() -> + _ = expect:true(is_empty([])), + expect:false(is_empty([1])). +-endif. + +has_member(List, Elem) -> + case List of + [] -> + false; + + [Head | Rest] -> + Head =:= Elem orelse has_member(Rest, Elem) + end. + +-ifdef(TEST). +has_member_test() -> + _ = expect:true(has_member([0, 4, 5, 1], 1)), + _ = expect:false(has_member([0, 4, 5, 7], 1)), + expect:false(has_member([], 1)). +-endif. + +head(List) -> + case List of + [] -> + {error, empty}; + + [X | Xs] -> + {ok, X} + end. + +-ifdef(TEST). +head_test() -> + _ = fun(Capture1) -> + expect:equal(Capture1, {ok, 0}) + end(head([0, 4, 5, 7])), + fun(Capture1) -> expect:equal(Capture1, {error, empty}) end(head([])). +-endif. + +tail(List) -> + case List of + [] -> + {error, empty}; + + [X | Xs] -> + {ok, Xs} + end. + +-ifdef(TEST). +tail_test() -> + _ = fun(Capture1) -> + expect:equal(Capture1, {ok, [4, 5, 7]}) + end(tail([0, 4, 5, 7])), + _ = fun(Capture1) -> expect:equal(Capture1, {ok, []}) end(tail([0])), + fun(Capture1) -> expect:equal(Capture1, {error, empty}) end(tail([])). +-endif. + +do_map(List, Fun, Acc) -> + case List of + [] -> + reverse(Acc); + + [X | Xs] -> + do_map(Xs, Fun, [Fun(X) | Acc]) + end. + +map(List, Fun) -> + do_map(List, Fun, []). + +-ifdef(TEST). +map_test() -> + _ = fun(Capture1) -> + expect:equal(Capture1, []) + end(fun(Capture1) -> map(Capture1, fun(X) -> X * 2 end) end([])), + fun(Capture1) -> + expect:equal(Capture1, [0, 8, 10, 14, 6]) + end(fun(Capture1) -> + map(Capture1, fun(X) -> X * 2 end) + end([0, 4, 5, 7, 3])). +-endif. + +new() -> + []. + +-ifdef(TEST). +new_test() -> + fun(Capture1) -> expect:equal(Capture1, []) end(new()). +-endif. + +foldl(List, Acc, Fun) -> + case List of + [] -> + Acc; + + [X | Rest] -> + foldl(Rest, Fun(X, Acc), Fun) + end. + +-ifdef(TEST). +foldl_test() -> + fun(Capture1) -> + expect:equal(Capture1, [3, 2, 1]) + end(fun(Capture1) -> + foldl(Capture1, [], fun(X, Acc) -> [X | Acc] end) + end([1, 2, 3])). +-endif. + +foldr(List, Acc, Fun) -> + case List of + [] -> + Acc; + + [X | Rest] -> + Fun(X, foldr(Rest, Acc, Fun)) + end. + +-ifdef(TEST). +foldr_test() -> + fun(Capture1) -> + expect:equal(Capture1, [1, 2, 3]) + end(fun(Capture1) -> + foldr(Capture1, [], fun(X, Acc) -> [X | Acc] end) + end([1, 2, 3])). +-endif. diff --git a/gen/map.erl b/gen/map.erl new file mode 100644 index 0000000..b6327cd --- /dev/null +++ b/gen/map.erl @@ -0,0 +1,80 @@ +-module(map). +-include_lib("eunit/include/eunit.hrl"). + +-export([new/0, size/1, to_list/1, from_list/1, fetch/2, map_values/2, keys/1, values/1, filter/2]). + +new() -> + maps:new(). + +size(A) -> + maps:size(A). + +to_list(A) -> + maps:to_list(A). + +from_list(A) -> + maps:from_list(A). + +-ifdef(TEST). +from_list_test() -> + Proplist = [{4, 0}, {1, 0}], + Map = from_list(Proplist), + _ = fun(Capture1) -> expect:equal(Capture1, 2) end(size(Map)), + fun(Capture1) -> expect:equal(Capture1, Proplist) end(to_list(Map)). +-endif. + +fetch(A, B) -> + gleam__stdlib:map_fetch(A, B). + +-ifdef(TEST). +fetch_test() -> + Proplist = [{4, 0}, {1, 1}], + Map = from_list(Proplist), + _ = fun(Capture1) -> + expect:equal(Capture1, {ok, 0}) + end(fun(Capture1) -> fetch(Capture1, 4) end(Map)), + fun(Capture1) -> + expect:equal(Capture1, {ok, 1}) + end(fun(Capture1) -> fetch(Capture1, 1) end(Map)). +-endif. + +erl_map_values(A, B) -> + maps:map(A, B). + +map_values(Map, Fun) -> + erl_map_values(Fun, Map). + +-ifdef(TEST). +map_values_test() -> + fun(Capture1) -> + expect:equal(Capture1, from_list([{1, 0}, {2, 3}, {3, 5}])) + end(fun(Capture1) -> + map_values(Capture1, fun(K, V) -> K + V end) + end(from_list([{1, 0}, {2, 1}, {3, 2}]))). +-endif. + +keys(A) -> + maps:keys(A). + +-ifdef(TEST). +keys_test() -> + fun(Capture1) -> + expect:equal(Capture1, [<<"a">>, <<"b">>, <<"c">>]) + end(keys(from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]))). +-endif. + +values(A) -> + maps:values(A). + +-ifdef(TEST). +values_test() -> + fun(Capture1) -> + expect:equal(Capture1, [0, 1, 2]) + end(values(from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]))). +-endif. + +erl_filter(A, B) -> + maps:filter(A, B). + +filter(Map, Fun) -> + filter(Fun, Map). diff --git a/gen/order.erl b/gen/order.erl new file mode 100644 index 0000000..10b5dd7 --- /dev/null +++ b/gen/order.erl @@ -0,0 +1,126 @@ +-module(order). +-include_lib("eunit/include/eunit.hrl"). + +-export([reverse/1, to_int/1, compare/2, max/2, min/2]). + +reverse(Order) -> + case Order of + lt -> + gt; + + eq -> + eq; + + gt -> + lt + end. + +-ifdef(TEST). +reverse_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, gt) end(reverse(lt)), + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(reverse(eq)), + fun(Capture1) -> expect:equal(Capture1, lt) end(reverse(gt)). +-endif. + +to_int(Order) -> + case Order of + lt -> + -1; + + eq -> + 0; + + gt -> + 1 + end. + +-ifdef(TEST). +to_int_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, -1) end(to_int(lt)), + _ = fun(Capture1) -> expect:equal(Capture1, 0) end(to_int(eq)), + fun(Capture1) -> expect:equal(Capture1, 1) end(to_int(gt)). +-endif. + +compare(A, B) -> + case {A, B} of + {lt, lt} -> + eq; + + {lt, _} -> + lt; + + {eq, eq} -> + eq; + + {gt, gt} -> + eq; + + {eq, gt} -> + lt; + + _ -> + gt + end. + +-ifdef(TEST). +compare_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(compare(lt, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(compare(lt, eq)), + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(compare(lt, gt)), + _ = fun(Capture1) -> expect:equal(Capture1, gt) end(compare(eq, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(compare(eq, eq)), + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(compare(eq, gt)), + _ = fun(Capture1) -> expect:equal(Capture1, gt) end(compare(gt, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, gt) end(compare(gt, eq)), + fun(Capture1) -> expect:equal(Capture1, eq) end(compare(gt, gt)). +-endif. + +max(A, B) -> + case {A, B} of + {gt, _} -> + gt; + + {eq, lt} -> + eq; + + _ -> + B + end. + +-ifdef(TEST). +max_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(max(lt, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(max(lt, eq)), + _ = fun(Capture1) -> expect:equal(Capture1, gt) end(max(lt, gt)), + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(max(eq, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(max(eq, eq)), + _ = fun(Capture1) -> expect:equal(Capture1, gt) end(max(eq, gt)), + _ = fun(Capture1) -> expect:equal(Capture1, gt) end(max(gt, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, gt) end(max(gt, eq)), + fun(Capture1) -> expect:equal(Capture1, gt) end(max(gt, gt)). +-endif. + +min(A, B) -> + case {A, B} of + {lt, _} -> + lt; + + {eq, gt} -> + eq; + + _ -> + B + end. + +-ifdef(TEST). +min_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(min(lt, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(min(lt, eq)), + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(min(lt, gt)), + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(min(eq, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(min(eq, eq)), + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(min(eq, gt)), + _ = fun(Capture1) -> expect:equal(Capture1, lt) end(min(gt, lt)), + _ = fun(Capture1) -> expect:equal(Capture1, eq) end(min(gt, eq)), + fun(Capture1) -> expect:equal(Capture1, gt) end(min(gt, gt)). +-endif. diff --git a/gen/result.erl b/gen/result.erl new file mode 100644 index 0000000..d3337e2 --- /dev/null +++ b/gen/result.erl @@ -0,0 +1,150 @@ +-module(result). +-include_lib("eunit/include/eunit.hrl"). + +-export([is_ok/1, is_error/1, map/2, map_error/2, flatten/1, flat_map/2, unwrap/2]). + +is_ok(Result) -> + case Result of + {error, _} -> + false; + + {ok, _} -> + true + end. + +-ifdef(TEST). +is_ok_test() -> + _ = expect:true(is_ok({ok, 1})), + expect:false(is_ok({error, 1})). +-endif. + +is_error(Result) -> + case Result of + {ok, _} -> + false; + + {error, _} -> + true + end. + +-ifdef(TEST). +is_error_test() -> + _ = expect:false(is_error({ok, 1})), + expect:true(is_error({error, 1})). +-endif. + +map(Result, Fun) -> + case Result of + {ok, X} -> + {ok, Fun(X)}; + + {error, _} -> + Result + end. + +-ifdef(TEST). +map_test() -> + _ = fun(Capture1) -> + expect:equal(Capture1, {ok, 2}) + end(fun(Capture1) -> map(Capture1, fun(X) -> X + 1 end) end({ok, 1})), + fun(Capture1) -> + expect:equal(Capture1, {error, 1}) + end(fun(Capture1) -> map(Capture1, fun(X) -> X + 1 end) end({error, 1})). +-endif. + +map_error(Result, Fun) -> + case Result of + {ok, _} -> + Result; + + {error, Error} -> + {error, Fun(Error)} + end. + +-ifdef(TEST). +map_error_test() -> + _ = fun(Capture1) -> + expect:equal(Capture1, {ok, 1}) + end(fun(Capture1) -> map_error(Capture1, fun(X) -> X + 1 end) end({ok, 1})), + fun(Capture1) -> + expect:equal(Capture1, {error, 2}) + end(fun(Capture1) -> + map_error(Capture1, fun(X) -> X + 1 end) + end({error, 1})). +-endif. + +flatten(Result) -> + case Result of + {ok, X} -> + X; + + {error, Error} -> + {error, Error} + end. + +-ifdef(TEST). +flatten_test() -> + _ = fun(Capture1) -> + expect:equal(Capture1, {ok, 1}) + end(flatten({ok, {ok, 1}})), + _ = fun(Capture1) -> + expect:equal(Capture1, {error, 1}) + end(flatten({ok, {error, 1}})), + _ = fun(Capture1) -> + expect:equal(Capture1, {error, 1}) + end(flatten({error, 1})), + fun(Capture1) -> + expect:equal(Capture1, {error, {error, 1}}) + end(flatten({error, {error, 1}})). +-endif. + +flat_map(Result, Fun) -> + case Result of + {ok, X} -> + case Fun(X) of + {ok, Y} -> + {ok, Y}; + + {error, Y1} -> + {error, Y1} + end; + + {error, _} -> + Result + end. + +-ifdef(TEST). +flat_map_test() -> + _ = fun(Capture1) -> + expect:equal(Capture1, {error, 1}) + end(fun(Capture1) -> + flat_map(Capture1, fun(X) -> {ok, X + 1} end) + end({error, 1})), + _ = fun(Capture1) -> + expect:equal(Capture1, {ok, 2}) + end(fun(Capture1) -> + flat_map(Capture1, fun(X) -> {ok, X + 1} end) + end({ok, 1})), + fun(Capture1) -> + expect:equal(Capture1, {error, 1}) + end(fun(Capture1) -> + flat_map(Capture1, fun(Unused) -> {error, 1} end) + end({ok, 1})). +-endif. + +unwrap(Result, Default) -> + case Result of + {ok, V} -> + V; + + {error, _} -> + Default + end. + +-ifdef(TEST). +unwrap_test() -> + _ = fun(Capture1) -> expect:equal(Capture1, 1) end(unwrap({ok, 1}, 50)), + fun(Capture1) -> + expect:equal(Capture1, 50) + end(unwrap({error, <<"nope">>}, 50)). +-endif. diff --git a/src/bool.gleam b/src/bool.gleam index a83391d..5375d0e 100644 --- a/src/bool.gleam +++ b/src/bool.gleam @@ -1,42 +1,43 @@ -import order:[Gt, Eq, Lt] - -pub fn not(bool) { - case bool { - | True -> False - | False -> True - } -} - -test not { - not(True) - |> expect:false - - not(False) - |> expect:true -} - -pub fn compare(a, b) { - case {a, b} { - | {True, True} -> Eq - | {True, False} -> Gt - | {False, False} -> Eq - | {False, True} -> Gt - } -} - -test compare { - compare(True, True) - |> expect:equal(_, Eq) - - compare(True, False) - |> expect:equal(_, Gt) - - compare(False, False) - |> expect:equal(_, Lt) - - compare(False, True) - |> expect:equal(_, Gt) -} +import expect +// import order:[Gt, Eq, Lt] + +// pub fn not(bool) { +// case bool { +// | True -> False +// | False -> True +// } +// } + +// test not { +// not(True) +// |> expect:false + +// not(False) +// |> expect:true +// } + +// pub fn compare(a, b) { +// case {a, b} { +// | {True, True} -> Eq +// | {True, False} -> Gt +// | {False, False} -> Eq +// | {False, True} -> Gt +// } +// } + +// test compare { +// compare(True, True) +// |> expect:equal(_, Eq) + +// compare(True, False) +// |> expect:equal(_, Gt) + +// compare(False, False) +// |> expect:equal(_, Lt) + +// compare(False, True) +// |> expect:equal(_, Gt) +// } pub fn max(a, b) { case a { @@ -46,13 +47,13 @@ pub fn max(a, b) { } test max { - max(True, True) + let _ = max(True, True) |> expect:equal(_, True) - max(True, False) + let _ = max(True, False) |> expect:equal(_, True) - max(False, False) + let _ = max(False, False) |> expect:equal(_, False) max(False, True) @@ -67,13 +68,13 @@ pub fn min(a, b) { } test min { - min(True, True) + let _ = min(True, True) |> expect:equal(_, True) - min(True, False) + let _ = min(True, False) |> expect:equal(_, False) - min(False, False) + let _ = min(False, False) |> expect:equal(_, False) min(False, True) @@ -88,7 +89,7 @@ pub fn to_int(bool) { } test to_int { - to_int(True) + let _ = to_int(True) |> expect:equal(_, 1) to_int(False) diff --git a/src/list.gleam b/src/list.gleam index 1d5b82e..338ee3c 100644 --- a/src/list.gleam +++ b/src/list.gleam @@ -1,5 +1,4 @@ import expect -import result:[Ok, Error] pub enum Error = | Empty @@ -9,9 +8,9 @@ pub enum Error = pub external fn length(List(a)) -> Int = "erlang" "length" test length { - length([]) |> expect:equal(_, 0) - length([1]) |> expect:equal(_, 1) - length([1, 1]) |> expect:equal(_, 2) + let _ = length([]) |> expect:equal(_, 0) + let _ = length([1]) |> expect:equal(_, 1) + let _ = length([1, 1]) |> expect:equal(_, 2) length([1, 1, 1]) |> expect:equal(_, 3) } @@ -20,8 +19,8 @@ test length { pub external fn reverse(List(a)) -> List(a) = "erlang" "reverse" test reverse { - length([]) |> expect:equal(_, []) - length([1, 2, 3, 4, 5]) |> expect:equal(_, [5, 4, 3, 2, 1]) + let _ = length([]) |> expect:equal(_, 0) + length([1, 2, 3, 4, 5]) |> expect:equal(_, 5) } pub fn is_empty(list) { @@ -29,21 +28,21 @@ pub fn is_empty(list) { } test is_empty { - is_empty([]) |> expect:true + let _ = is_empty([]) |> expect:true is_empty([1]) |> expect:false } -pub fn member(list, elem) { +pub fn has_member(list, elem) { case list { | [] -> False - | [head | rest] -> head == elem || member(rest, elem) + | [head | rest] -> head == elem || has_member(rest, elem) } } -test is_member { - is_member([0, 4, 5, 1], 1) |> expect:true - is_member([0, 4, 5, 7], 1) |> expect:false - is_member([], 1) |> expect:false +test has_member { + let _ = has_member([0, 4, 5, 1], 1) |> expect:true + let _ = has_member([0, 4, 5, 7], 1) |> expect:false + has_member([], 1) |> expect:false } pub fn head(list) { @@ -54,7 +53,7 @@ pub fn head(list) { } test head { - head([0, 4, 5, 7]) + let _ = head([0, 4, 5, 7]) |> expect:equal(_, Ok(0)) head([]) @@ -69,50 +68,50 @@ pub fn tail(list) { } test tail { - tail([0, 4, 5, 7]) + let _ = tail([0, 4, 5, 7]) |> expect:equal(_, Ok([4, 5, 7])) - tail([0]) + let _ = tail([0]) |> expect:equal(_, Ok([])) tail([]) |> expect:equal(_, Error(Empty)) } -fn do_filter(list, fun, acc) { - case list { - | [] -> reverse(acc) - | [x | xs] -> - let new_acc = - case fun(x) { - | True -> [x | acc] - | False -> acc - } - do_filter(xs, fun, new_acc) - } -} - -pub fn filter(list, fun) { - do_filter(list, fun, []) -} - -test filter { - [] - |> filter(_, fn(x) { True }) - |> expect:equal(_, []) - - [0, 4, 5, 7, 3] - |> filter(_, fn(x) { True }) - |> expect:equal(_, [0, 4, 5, 7, 3]) - - [0, 4, 5, 7, 3] - |> filter(_, fn(x) { x > 4 }) - |> expect:equal(_, [5, 7]) - - [0, 4, 5, 7, 3] - |> filter(_, fn(x) { x < 4 }) - |> expect:equal(_, [0, 3]) -} +// fn do_filter(list, fun, acc) { +// case list { +// | [] -> reverse(acc) +// | [x | xs] -> +// let new_acc = +// case fun(x) { +// | True -> [x | acc] +// | False -> acc +// } +// do_filter(xs, fun, new_acc) +// } +// } + +// pub fn filter(list, fun) { +// do_filter(list, fun, []) +// } + +// test filter { +// let _ = [] +// |> filter(_, fn(x) { True }) +// |> expect:equal(_, []) + +// let _ = [0, 4, 5, 7, 3] +// |> filter(_, fn(x) { True }) +// |> expect:equal(_, [0, 4, 5, 7, 3]) + +// let _ = [0, 4, 5, 7, 3] +// |> filter(_, fn(x) { x > 4 }) +// |> expect:equal(_, [5, 7]) + +// [0, 4, 5, 7, 3] +// |> filter(_, fn(x) { x < 4 }) +// |> expect:equal(_, [0, 3]) +// } fn do_map(list, fun, acc) { case list { @@ -126,7 +125,7 @@ pub fn map(list, fun) { } test map { - [] + let _ = [] |> map(_, fn(x) { x * 2 }) |> expect:equal(_, []) @@ -135,82 +134,82 @@ 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 traverse(list, fun) { - do_traverse(list, fun, []) -} - -test traverse { - fun = fn(x) { - case x < 6 { - True -> Ok(x * 2) - False -> Error(x) - } - } - - [0, 4, 5, 6, 3] - |> traverse(_, fun) - |> expect:equal(_, Ok([0, 8, 10, 12, 6])) - - [0, 4, 5, 7, 3] - |> traverse(_, fun) - |> expect:equal(_, Error(7))) -} - -pub fn drop(list, n) { - case n <= 0 { - | True -> list - | False -> - case list { - | [] -> [] - | [x | xs] -> drop(xs, n - 1) - } - } -} - -test drop/2 { - [] - |> drop(_, 5) - |> expect:equal(_, []) - - [1, 2, 3, 4, 5, 6, 7, 8] - |> drop(_, 5) - |> expect:equal(_, [6, 7, 8]) -} - -fn do_take(list, n, acc) { - case n <= 0 { - | True -> reverse(acc) - | False -> - case list { - | [] -> reverse(acc) - | [x | xs] -> take(xs, n - 1, [x | acc]) - } - } -} - -pub fn take(list, n) { - do_take(list, n, []) -} - -test take { - [] - |> take(_, 5) - |> expect:equal(_, []) - [1, 2, 3, 4, 5, 6, 7, 8] - |> take(_, 5) - |> expect:equal(_, [1, 2, 3, 4, 5]) -} +// 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, []) +// } + +// test traverse { +// let fun = fn(x) { +// case x < 6 { +// | True -> Ok(x * 2) +// | False -> Error(x) +// } +// } + +// let _ = [0, 4, 5, 6, 3] +// |> traverse(_, fun) +// |> expect:equal(_, Ok([0, 8, 10, 12, 6])) + +// [0, 4, 5, 7, 3] +// |> traverse(_, fun) +// |> expect:equal(_, Error(7)) +// } + +// pub fn drop(list, n) { +// case n <= 0 { +// | True -> list +// | False -> +// case list { +// | [] -> [] +// | [x | xs] -> drop(xs, n - 1) +// } +// } +// } + +// test drop { +// let _ = [] +// |> drop(_, 5) +// |> expect:equal(_, []) + +// [1, 2, 3, 4, 5, 6, 7, 8] +// |> drop(_, 5) +// |> expect:equal(_, [6, 7, 8]) +// } + +// fn do_take(list, n, acc) { +// case n <= 0 { +// | True -> reverse(acc) +// | False -> +// case list { +// | [] -> reverse(acc) +// | [x | xs] -> take(xs, n - 1, [x | acc]) +// } +// } +// } + +// pub fn take(list, n) { +// do_take(list, n, []) +// } + +// test take { +// let _ = [] +// |> take(_, 5) +// |> expect:equal(_, []) +// [1, 2, 3, 4, 5, 6, 7, 8] +// |> take(_, 5) +// |> expect:equal(_, [1, 2, 3, 4, 5]) +// } pub fn new() { [] @@ -220,30 +219,30 @@ test new { new() |> expect:equal(_, []) } -fn do_flatten(lists, acc) { - case lists { - | [] -> acc - | [l | rest] -> flatten(rest, acc ++ l) - } -} +// fn do_flatten(lists, acc) { +// case lists { +// | [] -> acc +// | [l | rest] -> flatten(rest, acc ++ l) +// } +// } -pub fn flatten(lists) { - do_flatten(lists, []) -} +// pub fn flatten(lists) { +// do_flatten(lists, []) +// } -test flatten { - flatten([]) - |> expect:equal(_, []) +// test flatten { +// let _ = flatten([]) +// |> expect:equal(_, []) - flatten([[]]) - |> expect:equal(_, []) +// let _ = flatten([[]]) +// |> expect:equal(_, []) - flatten([[], [], []]) - |> expect:equal(_, []) +// let _ = flatten([[], [], []]) +// |> expect:equal(_, []) - flatten([[1, 2], [], [3, 4]]) - |> expect:equal(_, [1, 2, 3, 4]) -} +// flatten([[1, 2], [], [3, 4]]) +// |> expect:equal(_, [1, 2, 3, 4]) +// } pub fn foldl(list, acc, fun) { case list { @@ -254,7 +253,7 @@ pub fn foldl(list, acc, fun) { test foldl { [1, 2, 3] - |> foldl(_, [], fn(x, acc) { x :: acc }) + |> foldl(_, [], fn(x, acc) { [x | acc] }) |> expect:equal(_, [3, 2, 1]) } @@ -267,6 +266,6 @@ pub fn foldr(list, acc, fun) { test foldr { [1, 2, 3] - |> foldr(_, [], fn(x, acc) { x :: acc }) + |> foldr(_, [], fn(x, acc) { [x | acc] }) |> expect:equal(_, [1, 2, 3]) } diff --git a/src/map.gleam b/src/map.gleam index 4f9f2b2..9236ffd 100644 --- a/src/map.gleam +++ b/src/map.gleam @@ -1,109 +1,109 @@ import any -import result:Result +import result import expect pub external type Map(key, value); -pub external fn new() -> Map(key, value)) +pub external fn new() -> Map(key, value) = "maps" "new" -test new { - new() - |> size - |> expect:equal(_, 0) -} +// test new { +// new() +// |> new +// |> expect:equal(_, []) +// } -pub external fn size(Map(_, _)) -> Int +pub external fn size(Map(k, v)) -> Int = "maps" "size" -test size { - [] - |> from_list - |> size - |> expect:equal(_, 0) - - [ - {1, 1}, - ] - |> from_list - |> size - |> expect:equal(_, 1) - - [ - {"", 1.0}, - {"", 2.0}, - ] - |> from_list - |> size - |> expect:equal(_, 2) -} - -external fn is_key(key, Map(key, _)) -> Bool - = "maps" "is_key" - -pub fn has_key(map, key) { - is_key(key, map) -} - -test has_key { - [] - |> from_list - |> has_key(_, 1) - |> expect:false - - [ - {1, 0}, - ] - |> from_list - |> has_key(_, 1) - |> expect:true - - [ - {4, 0}, - {1, 0}, - ] - |> from_list - |> has_key(_, 1) - |> expect:true - - [ - {4, 0}, - {1, 0}, - ] - |> from_list - |> has_key(_, 0) - |> expect:false -} - -pub fn from_record(record: {r}) -> Map(Atom, any:Any) { - any:unsafeCoerce(record) -} - -test from_record { - map = from_record({ name = "Jane" }) - - map - |> size - |> expect:equal(_, 1) - - map - |> expect:equal(_, from_list([{"name", "Jane"}])) -} - -pub external fn to_list(Map(key, value)) -> List(Tuple(key, value)) +// test size { +// let _ = [] +// |> from_list +// |> size +// |> expect:equal(_, 0) + +// let _ = [ +// {1, 1}, +// ] +// |> from_list +// |> size +// |> expect:equal(_, 1) + +// [ +// {"", 1.0}, +// {"", 2.0}, +// ] +// |> from_list +// |> size +// |> expect:equal(_, 2) +// } + +// external fn is_key(key, Map(key, v)) -> Bool +// = "maps" "is_key" + +// pub fn has_key(map, key) { +// is_key(key, map) +// } + +// test has_key { +// let _ = [] +// |> from_list +// |> has_key(_, 1) +// |> expect:false + +// let _ = [ +// {1, 0}, +// ] +// |> from_list +// |> has_key(_, 1) +// |> expect:true + +// let _ = [ +// {4, 0}, +// {1, 0}, +// ] +// |> from_list +// |> has_key(_, 1) +// |> expect:true + +// [ +// {4, 0}, +// {1, 0}, +// ] +// |> from_list +// |> has_key(_, 0) +// |> expect:false +// } + +// pub fn from_record(record: {r}) -> Map(Atom, any:Any) { +// any:unsafeCoerce(record) +// } + +// test from_record { +// let map = from_record({ name = "Jane" }) + +// let _ = map +// |> size +// |> expect:equal(_, 1) + +// map +// |> expect:equal(_, from_list([{"name", "Jane"}])) +// } + +pub external fn to_list(Map(key, value)) -> List({key, value}) = "maps" "to_list" -pub external fn from_list(List(Tuple(key, value))) -> Map(key, value) +pub external fn from_list(List({key, value})) -> Map(key, value) = "maps" "from_list" test from_list { - proplist = [ + let proplist = [ {4, 0}, {1, 0}, ] - map = from_list(proplist) + let map = from_list(proplist) - map + let _ = map |> size |> expect:equal(_, 2) @@ -112,43 +112,43 @@ test from_list { |> expect:equal(_, proplist) } -pub external fn fetch(Map(key, value), key) -> Result(Unit, value) +pub external fn fetch(Map(key, value), key) -> Result(a, value) = "gleam__stdlib" "map_fetch"; test fetch { - proplist = [ + let proplist = [ {4, 0}, {1, 1}, ] - map = from_list(proplist) + let map = from_list(proplist) - map + let _ = map |> fetch(_, 4) - |> expect:equal(_, result:Ok(0)) + |> expect:equal(_, Ok(0)) map |> fetch(_, 1) - |> expect:equal(_, result:Ok(1)) + |> expect:equal(_, Ok(1)) - map - |> fetch(_, 2) - |> expect:equal(_, result:Error(())) + // map + // |> fetch(_, 2) + // |> expect:equal(_, Error(()) } -external fn erl_put(key, value, Map(key, value)) -> Map(key, value) - = "maps" "put"; +// external fn erl_put(key, value, Map(key, value)) -> Map(key, value) +// = "maps" "put"; -pub fn put(map, key, value) { - erl_put(key, value, map) -} +// pub fn put(map, key, value) { +// erl_put(key, value, map) +// } -test put { - new() - |> put(_, "a", 0) - |> put(_, "b", 1) - |> put(_, "c", 2) - |> expect:equal(_, result:Ok(from_list([{"a", 0}, {"b", 1}, {"c", 2}]))) -} +// test put { +// new() +// |> put(_, "a", 0) +// |> put(_, "b", 1) +// |> put(_, "c", 2) +// |> expect:equal(_, Ok(from_list([{"a", 0}, {"b", 1}, {"c", 2}]))) +// } external fn erl_map_values(fn(key, value) -> value, Map(key, value)) -> Map(key, value) = "maps" "map"; @@ -168,7 +168,7 @@ test map_values { |> expect:equal(_, from_list([{1, 0}, {2, 3}, {3, 5}])) } -pub external fn keys(Map(keys, _)) -> List(keys) +pub external fn keys(Map(keys, v)) -> List(keys) = "maps" "keys" test keys { @@ -179,10 +179,10 @@ test keys { ] |> from_list |> keys - |> expect:equal(_, ["a", "b", "c"])) + |> expect:equal(_, ["a", "b", "c"]) } -pub external fn values(Map(_, values)) -> List(values) +pub external fn values(Map(k, values)) -> List(values) = "maps" "values" test values { @@ -193,7 +193,7 @@ test values { ] |> from_list |> values - |> expect:equal(_, [0, 1, 2])) + |> expect:equal(_, [0, 1, 2]) } external fn erl_filter(fn(key, value) -> Bool, Map(key, value)) -> Map(key, value) @@ -202,15 +202,3 @@ external fn erl_filter(fn(key, value) -> Bool, Map(key, value)) -> Map(key, valu pub fn filter(map, fun) { filter(fun, map) } - -test map_values { - [ - {1, 0}, - {3, 2}, - {2, 1}, - ] - |> from_list - |> filter(_, fn(k, v) { k + v < 4 }) - |> expect:equal(_, from_list([{1, 0}, {2, 3}])) -} - diff --git a/src/order.gleam b/src/order.gleam index 9e69114..aa6756c 100644 --- a/src/order.gleam +++ b/src/order.gleam @@ -15,8 +15,8 @@ pub fn reverse(order) { } test reverse { - reverse(Lt) |> expect:equal(_, Gt) - reverse(Eq) |> expect:equal(_, Eq) + let _ = reverse(Lt) |> expect:equal(_, Gt) + let _ = reverse(Eq) |> expect:equal(_, Eq) reverse(Gt) |> expect:equal(_, Lt) } @@ -29,8 +29,8 @@ pub fn to_int(order) { } test to_int { - to_int(Lt) |> expect:equal(_, -1) - to_int(Eq) |> expect:equal(_, 0) + let _ = to_int(Lt) |> expect:equal(_, -1) + let _ = to_int(Eq) |> expect:equal(_, 0) to_int(Gt) |> expect:equal(_, 1) } @@ -46,14 +46,14 @@ pub fn compare(a, b) { } test compare { - compare(Lt, Lt) |> expect:equal(_, Eq) - compare(Lt, Eq) |> expect:equal(_, Lt) - compare(Lt, Gt) |> expect:equal(_, Lt) - compare(Eq, Lt) |> expect:equal(_, Gt) - compare(Eq, Eq) |> expect:equal(_, Eq) - compare(Eq, Gt) |> expect:equal(_, Lt) - compare(Gt, Lt) |> expect:equal(_, Gt) - compare(Gt, Eq) |> expect:equal(_, Gt) + let _ = compare(Lt, Lt) |> expect:equal(_, Eq) + let _ = compare(Lt, Eq) |> expect:equal(_, Lt) + let _ = compare(Lt, Gt) |> expect:equal(_, Lt) + let _ = compare(Eq, Lt) |> expect:equal(_, Gt) + let _ = compare(Eq, Eq) |> expect:equal(_, Eq) + let _ = compare(Eq, Gt) |> expect:equal(_, Lt) + let _ = compare(Gt, Lt) |> expect:equal(_, Gt) + let _ = compare(Gt, Eq) |> expect:equal(_, Gt) compare(Gt, Gt) |> expect:equal(_, Eq) } @@ -66,14 +66,14 @@ pub fn max(a, b) { } test max { - max(Lt, Lt) |> expect:equal(_, Lt) - max(Lt, Eq) |> expect:equal(_, Eq) - max(Lt, Gt) |> expect:equal(_, Gt) - max(Eq, Lt) |> expect:equal(_, Eq) - max(Eq, Eq) |> expect:equal(_, Eq) - max(Eq, Gt) |> expect:equal(_, Gt) - max(Gt, Lt) |> expect:equal(_, Gt) - max(Gt, Eq) |> expect:equal(_, Gt) + let _ = max(Lt, Lt) |> expect:equal(_, Lt) + let _ = max(Lt, Eq) |> expect:equal(_, Eq) + let _ = max(Lt, Gt) |> expect:equal(_, Gt) + let _ = max(Eq, Lt) |> expect:equal(_, Eq) + let _ = max(Eq, Eq) |> expect:equal(_, Eq) + let _ = max(Eq, Gt) |> expect:equal(_, Gt) + let _ = max(Gt, Lt) |> expect:equal(_, Gt) + let _ = max(Gt, Eq) |> expect:equal(_, Gt) max(Gt, Gt) |> expect:equal(_, Gt) } @@ -86,13 +86,13 @@ pub fn min(a, b) { } test min { - min(Lt, Lt) |> expect:equal(_, Lt) - min(Lt, Eq) |> expect:equal(_, Lt) - min(Lt, Gt) |> expect:equal(_, Lt) - min(Eq, Lt) |> expect:equal(_, Lt) - min(Eq, Eq) |> expect:equal(_, Eq) - min(Eq, Gt) |> expect:equal(_, Eq) - min(Gt, Lt) |> expect:equal(_, Lt) - min(Gt, Eq) |> expect:equal(_, Eq) + let _ = min(Lt, Lt) |> expect:equal(_, Lt) + let _ = min(Lt, Eq) |> expect:equal(_, Lt) + let _ = min(Lt, Gt) |> expect:equal(_, Lt) + let _ = min(Eq, Lt) |> expect:equal(_, Lt) + let _ = min(Eq, Eq) |> expect:equal(_, Eq) + let _ = min(Eq, Gt) |> expect:equal(_, Eq) + let _ = min(Gt, Lt) |> expect:equal(_, Lt) + let _ = min(Gt, Eq) |> expect:equal(_, Eq) min(Gt, Gt) |> expect:equal(_, Gt) } diff --git a/src/result.gleam b/src/result.gleam index ffd99e0..2ad7ee8 100644 --- a/src/result.gleam +++ b/src/result.gleam @@ -1,9 +1,8 @@ import expect -// doc """ // Result represents the result of something that may succeed or fail. // `Ok` means it was successful, `Error` means it failed. -// """ +// pub enum Result(error, value) = | Ok(value) | Error(error) @@ -17,7 +16,7 @@ pub fn is_ok(result) { } test is_ok { - is_ok(Ok(1)) |> expect:true + let _ = is_ok(Ok(1)) |> expect:true is_ok(Error(1)) |> expect:false } @@ -29,7 +28,7 @@ pub fn is_error(result) { } test is_error { - is_error(Ok(1)) + let _ = is_error(Ok(1)) |> expect:false is_error(Error(1)) @@ -44,13 +43,13 @@ pub fn map(result, fun) { } test map { - Ok(1) + let _ = Ok(1) |> map(_, fn(x) { x + 1 }) |> expect:equal(_, Ok(2)) Error(1) |> map(_, fn(x) { x + 1 }) - |> expect:equal(Error(1)) + |> expect:equal(_, Error(1)) } pub fn map_error(result, fun) { @@ -61,7 +60,7 @@ pub fn map_error(result, fun) { } test map_error { - Ok(1) + let _ = Ok(1) |> map_error(_, fn(x) { x + 1 }) |> expect:equal(_, Ok(1)) @@ -78,13 +77,13 @@ pub fn flatten(result) { } test flatten { - flatten(Ok(Ok(1))) + let _ = flatten(Ok(Ok(1))) |> expect:equal(_, Ok(1)) - flatten(Ok(Error(1))) + let _ = flatten(Ok(Error(1))) |> expect:equal(_, Error(1)) - flatten(Error(1)) + let _ = flatten(Error(1)) |> expect:equal(_, Error(1)) flatten(Error(Error(1))) @@ -109,11 +108,11 @@ pub fn flat_map(result, fun) { } test flat_map { - Error(1) + let _ = Error(1) |> flat_map(_, fn(x) { Ok(x + 1) }) |> expect:equal(_, Error(1)) - Ok(1) + let _ = Ok(1) |> flat_map(_, fn(x) { Ok(x + 1) }) |> expect:equal(_, Ok(2)) @@ -130,7 +129,7 @@ pub fn unwrap(result, default) { } test unwrap { - unwrap(Ok(1), 50) + let _ = unwrap(Ok(1), 50) |> expect:equal(_, 1) unwrap(Error("nope"), 50) |