diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-03-02 19:08:46 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-03-02 19:10:45 +0000 |
commit | ea5e146cf5ffd26a1c77a6b7e3160d98fc503c3c (patch) | |
tree | 98c7273268ed5eecaccb2d9d88d983181496c293 /gen | |
parent | ceb115b4dbc878b885521743f6018563b2dea3c8 (diff) | |
download | gleam_stdlib-ea5e146cf5ffd26a1c77a6b7e3160d98fc503c3c.tar.gz gleam_stdlib-ea5e146cf5ffd26a1c77a6b7e3160d98fc503c3c.zip |
Compile stdlib
Diffstat (limited to 'gen')
-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 |
8 files changed, 606 insertions, 0 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. |