aboutsummaryrefslogtreecommitdiff
path: root/gen/map.erl
blob: c2a07d4a7cc49ca6dbff400de4a00f0223f5e346 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-module(map).
-compile(no_auto_import).
-include_lib("eunit/include/eunit.hrl").

-export([size/1, to_list/1, from_list/1, has_key/2, new/0, fetch/2, put/3, map_values/2, keys/1, values/1, filter/2]).

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)).
-endif.

is_key(A, B) ->
    maps:is_key(A, B).

has_key(Map, Key) ->
    is_key(Key, Map).

-ifdef(TEST).
has_key_test() ->
    _ = expect:false((fun(Capture1) ->
                         has_key(Capture1, 1)
                     end)(from_list([]))),
    _ = expect:true((fun(Capture1) ->
                        has_key(Capture1, 1)
                    end)(from_list([{1, 0}]))),
    _ = expect:true((fun(Capture1) ->
                        has_key(Capture1, 1)
                    end)(from_list([{4, 0}, {1, 0}]))),
    expect:false((fun(Capture1) ->
                     has_key(Capture1, 0)
                 end)(from_list([{4, 0}, {1, 0}]))).
-endif.

new() ->
    maps:new().

-ifdef(TEST).
new_test() ->
    _ = (fun(Capture1) -> expect:equal(Capture1, 0) end)(size(new())),
    (fun(Capture1) -> expect:equal(Capture1, []) end)(to_list(new())).
-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_put(A, B, C) ->
    maps:put(A, B, C).

put(Map, Key, Value) ->
    erl_put(Key, Value, Map).

-ifdef(TEST).
put_test() ->
    (fun(Capture1) ->
        expect:equal(Capture1,
                     from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]))
    end)((fun(Capture1) ->
             put(Capture1, <<"c">>, 2)
         end)((fun(Capture1) ->
                  put(Capture1, <<"b">>, 1)
              end)((fun(Capture1) -> put(Capture1, <<"a">>, 0) end)(new())))).
-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, 1}, {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) ->
    erl_filter(Fun, Map).