aboutsummaryrefslogtreecommitdiff
path: root/gen/map.erl
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2019-03-02 19:08:46 +0000
committerLouis Pilfold <louis@lpil.uk>2019-03-02 19:10:45 +0000
commitea5e146cf5ffd26a1c77a6b7e3160d98fc503c3c (patch)
tree98c7273268ed5eecaccb2d9d88d983181496c293 /gen/map.erl
parentceb115b4dbc878b885521743f6018563b2dea3c8 (diff)
downloadgleam_stdlib-ea5e146cf5ffd26a1c77a6b7e3160d98fc503c3c.tar.gz
gleam_stdlib-ea5e146cf5ffd26a1c77a6b7e3160d98fc503c3c.zip
Compile stdlib
Diffstat (limited to 'gen/map.erl')
-rw-r--r--gen/map.erl80
1 files changed, 80 insertions, 0 deletions
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).