diff options
-rw-r--r-- | CHANGELOG.md | 1 | ||||
-rw-r--r-- | gen/src/gleam@map.erl (renamed from gen/src/gleam@map_dict.erl) | 2 | ||||
-rw-r--r-- | gen/test/gleam@any_test.erl | 21 | ||||
-rw-r--r-- | gen/test/gleam@map_dict_test.erl | 157 | ||||
-rw-r--r-- | gen/test/gleam@map_test.erl | 150 | ||||
-rw-r--r-- | src/gleam/map.gleam (renamed from src/gleam/map_dict.gleam) | 0 | ||||
-rw-r--r-- | test/gleam/any_test.gleam | 11 | ||||
-rw-r--r-- | test/gleam/map_test.gleam (renamed from test/gleam/map_dict_test.gleam) | 118 |
8 files changed, 237 insertions, 223 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 9359814..01d80c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Syntax has been updated for Gleam v0.4. +- The `map_dict` module has been renamed to `map`. - `list:sort` now requires a compare function as comparison operators now only work on Ints. - `list:sort`'s performance has been slightly optimised. diff --git a/gen/src/gleam@map_dict.erl b/gen/src/gleam@map.erl index 77eaf79..e3a2e35 100644 --- a/gen/src/gleam@map_dict.erl +++ b/gen/src/gleam@map.erl @@ -1,4 +1,4 @@ --module(gleam@map_dict). +-module(gleam@map). -compile(no_auto_import). -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, take/2, merge/2, delete/2, drop/2, update/3, fold/3]). diff --git a/gen/test/gleam@any_test.erl b/gen/test/gleam@any_test.erl index c8deb45..e915fe7 100644 --- a/gen/test/gleam@any_test.erl +++ b/gen/test/gleam@any_test.erl @@ -151,14 +151,29 @@ struct2_test() -> field_test() -> {ok, OkAtom} = gleam@atom:from_string(<<"ok">>), + {ok, EarlierAtom} = gleam@atom:from_string(<<"earlier">>), gleam@expect:equal( - gleam@any:field(gleam@any:from(#{ok => 1}), OkAtom), + gleam@any:field( + gleam@any:from(gleam@map:put(gleam@map:new(), OkAtom, 1)), + OkAtom + ), {ok, gleam@any:from(1)} ), gleam@expect:equal( - gleam@any:field(gleam@any:from(#{earlier => 2, ok => 3}), OkAtom), + gleam@any:field( + gleam@any:from( + gleam@map:put( + gleam@map:put(gleam@map:new(), OkAtom, 3), + EarlierAtom, + 1 + ) + ), + OkAtom + ), {ok, gleam@any:from(3)} ), - gleam@expect:is_error(gleam@any:field(gleam@any:from(#{}), OkAtom)), + gleam@expect:is_error( + gleam@any:field(gleam@any:from(gleam@map:new()), OkAtom) + ), gleam@expect:is_error(gleam@any:field(gleam@any:from(1), OkAtom)), gleam@expect:is_error(gleam@any:field(gleam@any:from([]), [])). diff --git a/gen/test/gleam@map_dict_test.erl b/gen/test/gleam@map_dict_test.erl deleted file mode 100644 index 98c1486..0000000 --- a/gen/test/gleam@map_dict_test.erl +++ /dev/null @@ -1,157 +0,0 @@ --module(gleam@map_dict_test). --compile(no_auto_import). - --export([from_list_test/0, has_key_test/0, new_test/0, fetch_test/0, put_test/0, map_values_test/0, keys_test/0, values_test/0, take_test/0, drop_test/0, merge_test/0, delete_test/0, update_test/0, fold_test/0]). - -from_list_test() -> - gleam@expect:equal( - gleam@map_dict:size(gleam@map_dict:from_list([{4, 0}, {1, 0}])), - 2 - ). - -has_key_test() -> - gleam@expect:false(gleam@map_dict:has_key(gleam@map_dict:from_list([]), 1)), - gleam@expect:true( - gleam@map_dict:has_key(gleam@map_dict:from_list([{1, 0}]), 1) - ), - gleam@expect:true( - gleam@map_dict:has_key(gleam@map_dict:from_list([{4, 0}, {1, 0}]), 1) - ), - gleam@expect:false( - gleam@map_dict:has_key(gleam@map_dict:from_list([{4, 0}, {1, 0}]), 0) - ). - -new_test() -> - gleam@expect:equal(gleam@map_dict:size(gleam@map_dict:new()), 0), - gleam@expect:equal(gleam@map_dict:to_list(gleam@map_dict:new()), []). - -fetch_test() -> - Proplist = [{4, 0}, {1, 1}], - M = gleam@map_dict:from_list(Proplist), - gleam@expect:equal(gleam@map_dict:fetch(M, 4), {ok, 0}), - gleam@expect:equal(gleam@map_dict:fetch(M, 1), {ok, 1}), - gleam@expect:is_error(gleam@map_dict:fetch(M, 2)). - -put_test() -> - gleam@expect:equal( - gleam@map_dict:put( - gleam@map_dict:put( - gleam@map_dict:put(gleam@map_dict:new(), <<"a">>, 0), - <<"b">>, - 1 - ), - <<"c">>, - 2 - ), - gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]) - ). - -map_values_test() -> - gleam@expect:equal( - gleam@map_dict:map_values( - gleam@map_dict:from_list([{1, 0}, {2, 1}, {3, 2}]), - fun(K, V) -> K + V end - ), - gleam@map_dict:from_list([{1, 1}, {2, 3}, {3, 5}]) - ). - -keys_test() -> - gleam@expect:equal( - gleam@map_dict:keys( - gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]) - ), - [<<"a">>, <<"b">>, <<"c">>] - ). - -values_test() -> - gleam@expect:equal( - gleam@map_dict:values( - gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]) - ), - [0, 1, 2] - ). - -take_test() -> - gleam@expect:equal( - gleam@map_dict:take( - gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), - [<<"a">>, <<"b">>, <<"d">>] - ), - gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}]) - ). - -drop_test() -> - gleam@expect:equal( - gleam@map_dict:drop( - gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), - [<<"a">>, <<"b">>, <<"d">>] - ), - gleam@map_dict:from_list([{<<"c">>, 2}]) - ). - -merge_test() -> - A = gleam@map_dict:from_list([{<<"a">>, 2}, {<<"c">>, 4}, {<<"d">>, 3}]), - B = gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), - gleam@expect:equal( - gleam@map_dict:merge(A, B), - gleam@map_dict:from_list( - [{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}, {<<"d">>, 3}] - ) - ), - gleam@expect:equal( - gleam@map_dict:merge(B, A), - gleam@map_dict:from_list( - [{<<"a">>, 2}, {<<"b">>, 1}, {<<"c">>, 4}, {<<"d">>, 3}] - ) - ). - -delete_test() -> - gleam@expect:equal( - gleam@map_dict:delete( - gleam@map_dict:delete( - gleam@map_dict:from_list( - [{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}] - ), - <<"a">> - ), - <<"d">> - ), - gleam@map_dict:from_list([{<<"b">>, 1}, {<<"c">>, 2}]) - ). - -update_test() -> - Dict = gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), - IncOrZero = fun(X) -> case X of - {ok, I} -> - I + 1; - - {error, _} -> - 0 - end end, - gleam@expect:equal( - gleam@map_dict:update(Dict, <<"a">>, IncOrZero), - gleam@map_dict:from_list([{<<"a">>, 1}, {<<"b">>, 1}, {<<"c">>, 2}]) - ), - gleam@expect:equal( - gleam@map_dict:update(Dict, <<"b">>, IncOrZero), - gleam@map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 2}, {<<"c">>, 2}]) - ), - gleam@expect:equal( - gleam@map_dict:update(Dict, <<"z">>, IncOrZero), - gleam@map_dict:from_list( - [{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}, {<<"z">>, 0}] - ) - ). - -fold_test() -> - Dict = gleam@map_dict:from_list( - [{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}, {<<"d">>, 3}] - ), - Add = fun(_, V, Acc) -> V + Acc end, - gleam@expect:equal(gleam@map_dict:fold(Dict, 0, Add), 6), - Concat = fun(K, _, Acc) -> gleam@string:append(Acc, K) end, - gleam@expect:equal(gleam@map_dict:fold(Dict, <<"">>, Concat), <<"abcd">>), - gleam@expect:equal( - gleam@map_dict:fold(gleam@map_dict:from_list([]), 0, Add), - 0 - ). diff --git a/gen/test/gleam@map_test.erl b/gen/test/gleam@map_test.erl new file mode 100644 index 0000000..83a86f6 --- /dev/null +++ b/gen/test/gleam@map_test.erl @@ -0,0 +1,150 @@ +-module(gleam@map_test). +-compile(no_auto_import). + +-export([from_list_test/0, has_key_test/0, new_test/0, fetch_test/0, put_test/0, map_values_test/0, keys_test/0, values_test/0, take_test/0, drop_test/0, merge_test/0, delete_test/0, update_test/0, fold_test/0]). + +from_list_test() -> + gleam@expect:equal( + gleam@map:size(gleam@map:from_list([{4, 0}, {1, 0}])), + 2 + ). + +has_key_test() -> + gleam@expect:false(gleam@map:has_key(gleam@map:from_list([]), 1)), + gleam@expect:true(gleam@map:has_key(gleam@map:from_list([{1, 0}]), 1)), + gleam@expect:true( + gleam@map:has_key(gleam@map:from_list([{4, 0}, {1, 0}]), 1) + ), + gleam@expect:false( + gleam@map:has_key(gleam@map:from_list([{4, 0}, {1, 0}]), 0) + ). + +new_test() -> + gleam@expect:equal(gleam@map:size(gleam@map:new()), 0), + gleam@expect:equal(gleam@map:to_list(gleam@map:new()), []). + +fetch_test() -> + Proplist = [{4, 0}, {1, 1}], + M = gleam@map:from_list(Proplist), + gleam@expect:equal(gleam@map:fetch(M, 4), {ok, 0}), + gleam@expect:equal(gleam@map:fetch(M, 1), {ok, 1}), + gleam@expect:is_error(gleam@map:fetch(M, 2)). + +put_test() -> + gleam@expect:equal( + gleam@map:put( + gleam@map:put( + gleam@map:put(gleam@map:new(), <<"a">>, 0), + <<"b">>, + 1 + ), + <<"c">>, + 2 + ), + gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]) + ). + +map_values_test() -> + gleam@expect:equal( + gleam@map:map_values( + gleam@map:from_list([{1, 0}, {2, 1}, {3, 2}]), + fun(K, V) -> K + V end + ), + gleam@map:from_list([{1, 1}, {2, 3}, {3, 5}]) + ). + +keys_test() -> + gleam@expect:equal( + gleam@map:keys( + gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]) + ), + [<<"a">>, <<"b">>, <<"c">>] + ). + +values_test() -> + gleam@expect:equal( + gleam@map:values( + gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]) + ), + [0, 1, 2] + ). + +take_test() -> + gleam@expect:equal( + gleam@map:take( + gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), + [<<"a">>, <<"b">>, <<"d">>] + ), + gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}]) + ). + +drop_test() -> + gleam@expect:equal( + gleam@map:drop( + gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), + [<<"a">>, <<"b">>, <<"d">>] + ), + gleam@map:from_list([{<<"c">>, 2}]) + ). + +merge_test() -> + A = gleam@map:from_list([{<<"a">>, 2}, {<<"c">>, 4}, {<<"d">>, 3}]), + B = gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), + gleam@expect:equal( + gleam@map:merge(A, B), + gleam@map:from_list( + [{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}, {<<"d">>, 3}] + ) + ), + gleam@expect:equal( + gleam@map:merge(B, A), + gleam@map:from_list( + [{<<"a">>, 2}, {<<"b">>, 1}, {<<"c">>, 4}, {<<"d">>, 3}] + ) + ). + +delete_test() -> + gleam@expect:equal( + gleam@map:delete( + gleam@map:delete( + gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), + <<"a">> + ), + <<"d">> + ), + gleam@map:from_list([{<<"b">>, 1}, {<<"c">>, 2}]) + ). + +update_test() -> + Dict = gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}]), + IncOrZero = fun(X) -> case X of + {ok, I} -> + I + 1; + + {error, _} -> + 0 + end end, + gleam@expect:equal( + gleam@map:update(Dict, <<"a">>, IncOrZero), + gleam@map:from_list([{<<"a">>, 1}, {<<"b">>, 1}, {<<"c">>, 2}]) + ), + gleam@expect:equal( + gleam@map:update(Dict, <<"b">>, IncOrZero), + gleam@map:from_list([{<<"a">>, 0}, {<<"b">>, 2}, {<<"c">>, 2}]) + ), + gleam@expect:equal( + gleam@map:update(Dict, <<"z">>, IncOrZero), + gleam@map:from_list( + [{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}, {<<"z">>, 0}] + ) + ). + +fold_test() -> + Dict = gleam@map:from_list( + [{<<"a">>, 0}, {<<"b">>, 1}, {<<"c">>, 2}, {<<"d">>, 3}] + ), + Add = fun(_, V, Acc) -> V + Acc end, + gleam@expect:equal(gleam@map:fold(Dict, 0, Add), 6), + Concat = fun(K, _, Acc) -> gleam@string:append(Acc, K) end, + gleam@expect:equal(gleam@map:fold(Dict, <<"">>, Concat), <<"abcd">>), + gleam@expect:equal(gleam@map:fold(gleam@map:from_list([]), 0, Add), 0). diff --git a/src/gleam/map_dict.gleam b/src/gleam/map.gleam index 5e24816..5e24816 100644 --- a/src/gleam/map_dict.gleam +++ b/src/gleam/map.gleam diff --git a/test/gleam/any_test.gleam b/test/gleam/any_test.gleam index ef441d7..13af0cb 100644 --- a/test/gleam/any_test.gleam +++ b/test/gleam/any_test.gleam @@ -4,6 +4,7 @@ import gleam/list import gleam/tuple import gleam/expect import gleam/result +import gleam/map pub fn string_test() { "" @@ -250,18 +251,22 @@ pub fn struct2_test() { pub fn field_test() { let Ok(ok_atom) = atom.from_string("ok") + let Ok(earlier_atom) = atom.from_string("earlier") - {ok = 1} + map.new() + |> map.put(_, ok_atom, 1) |> any.from |> any.field(_, ok_atom) |> expect.equal(_, Ok(any.from(1))) - {earlier = 2, ok = 3} + map.new() + |> map.put(_, ok_atom, 3) + |> map.put(_, earlier_atom, 1) |> any.from |> any.field(_, ok_atom) |> expect.equal(_, Ok(any.from(3))) - {} + map.new() |> any.from |> any.field(_, ok_atom) |> expect.is_error diff --git a/test/gleam/map_dict_test.gleam b/test/gleam/map_test.gleam index a258067..529afe2 100644 --- a/test/gleam/map_dict_test.gleam +++ b/test/gleam/map_test.gleam @@ -1,54 +1,54 @@ import gleam/string import gleam/expect -import gleam/map_dict +import gleam/map pub fn from_list_test() { [ struct(4, 0), struct(1, 0), ] - |> map_dict.from_list - |> map_dict.size + |> map.from_list + |> map.size |> expect.equal(_, 2) } pub fn has_key_test() { [] - |> map_dict.from_list - |> map_dict.has_key(_, 1) + |> map.from_list + |> map.has_key(_, 1) |> expect.false [ struct(1, 0), ] - |> map_dict.from_list - |> map_dict.has_key(_, 1) + |> map.from_list + |> map.has_key(_, 1) |> expect.true [ struct(4, 0), struct(1, 0), ] - |> map_dict.from_list - |> map_dict.has_key(_, 1) + |> map.from_list + |> map.has_key(_, 1) |> expect.true [ struct(4, 0), struct(1, 0), ] - |> map_dict.from_list - |> map_dict.has_key(_, 0) + |> map.from_list + |> map.has_key(_, 0) |> expect.false } pub fn new_test() { - map_dict.new() - |> map_dict.size + map.new() + |> map.size |> expect.equal(_, 0) - map_dict.new() - |> map_dict.to_list + map.new() + |> map.to_list |> expect.equal(_, []) } @@ -57,27 +57,27 @@ pub fn fetch_test() { struct(4, 0), struct(1, 1), ] - let m = map_dict.from_list(proplist) + let m = map.from_list(proplist) m - |> map_dict.fetch(_, 4) + |> map.fetch(_, 4) |> expect.equal(_, Ok(0)) m - |> map_dict.fetch(_, 1) + |> map.fetch(_, 1) |> expect.equal(_, Ok(1)) m - |> map_dict.fetch(_, 2) + |> map.fetch(_, 2) |> expect.is_error } pub fn put_test() { - map_dict.new() - |> map_dict.put(_, "a", 0) - |> map_dict.put(_, "b", 1) - |> map_dict.put(_, "c", 2) - |> expect.equal(_, map_dict.from_list([ + map.new() + |> map.put(_, "a", 0) + |> map.put(_, "b", 1) + |> map.put(_, "c", 2) + |> expect.equal(_, map.from_list([ struct("a", 0), struct("b", 1), struct("c", 2), @@ -90,9 +90,9 @@ pub fn map_values_test() { struct(2, 1), struct(3, 2), ] - |> map_dict.from_list - |> map_dict.map_values(_, fn(k, v) { k + v }) - |> expect.equal(_, map_dict.from_list([ + |> map.from_list + |> map.map_values(_, fn(k, v) { k + v }) + |> expect.equal(_, map.from_list([ struct(1, 1), struct(2, 3), struct(3, 5), @@ -105,8 +105,8 @@ pub fn keys_test() { struct("b", 1), struct("c", 2), ] - |> map_dict.from_list - |> map_dict.keys + |> map.from_list + |> map.keys |> expect.equal(_, ["a", "b", "c"]) } @@ -116,8 +116,8 @@ pub fn values_test() { struct("b", 1), struct("c", 2), ] - |> map_dict.from_list - |> map_dict.values + |> map.from_list + |> map.values |> expect.equal(_, [0, 1, 2]) } @@ -127,9 +127,9 @@ pub fn take_test() { struct("b", 1), struct("c", 2), ] - |> map_dict.from_list - |> map_dict.take(_, ["a", "b", "d"]) - |> expect.equal(_, map_dict.from_list([struct("a", 0), struct("b", 1)])) + |> map.from_list + |> map.take(_, ["a", "b", "d"]) + |> expect.equal(_, map.from_list([struct("a", 0), struct("b", 1)])) } pub fn drop_test() { @@ -138,33 +138,33 @@ pub fn drop_test() { struct("b", 1), struct("c", 2), ] - |> map_dict.from_list - |> map_dict.drop(_, ["a", "b", "d"]) - |> expect.equal(_, map_dict.from_list([struct("c", 2)])) + |> map.from_list + |> map.drop(_, ["a", "b", "d"]) + |> expect.equal(_, map.from_list([struct("c", 2)])) } pub fn merge_test() { - let a = map_dict.from_list([ + let a = map.from_list([ struct("a", 2), struct("c", 4), struct("d", 3), ]) - let b = map_dict.from_list([ + let b = map.from_list([ struct("a", 0), struct("b", 1), struct("c", 2), ]) - map_dict.merge(a, b) - |> expect.equal(_, map_dict.from_list([ + map.merge(a, b) + |> expect.equal(_, map.from_list([ struct("a", 0), struct("b", 1), struct("c", 2), struct("d", 3), ])) - map_dict.merge(b, a) - |> expect.equal(_, map_dict.from_list([ + map.merge(b, a) + |> expect.equal(_, map.from_list([ struct("a", 2), struct("b", 1), struct("c", 4), @@ -178,14 +178,14 @@ pub fn delete_test() { struct("b", 1), struct("c", 2), ] - |> map_dict.from_list - |> map_dict.delete(_, "a") - |> map_dict.delete(_, "d") - |> expect.equal(_, map_dict.from_list([struct("b", 1), struct("c", 2)])) + |> map.from_list + |> map.delete(_, "a") + |> map.delete(_, "d") + |> expect.equal(_, map.from_list([struct("b", 1), struct("c", 2)])) } pub fn update_test() { - let dict = map_dict.from_list([ + let dict = map.from_list([ struct("a", 0), struct("b", 1), struct("c", 2), @@ -199,24 +199,24 @@ pub fn update_test() { } dict - |> map_dict.update(_, "a", inc_or_zero) - |> expect.equal(_, map_dict.from_list([ + |> map.update(_, "a", inc_or_zero) + |> expect.equal(_, map.from_list([ struct("a", 1), struct("b", 1), struct("c", 2), ])) dict - |> map_dict.update(_, "b", inc_or_zero) - |> expect.equal(_, map_dict.from_list([ + |> map.update(_, "b", inc_or_zero) + |> expect.equal(_, map.from_list([ struct("a", 0), struct("b", 2), struct("c", 2), ])) dict - |> map_dict.update(_, "z", inc_or_zero) - |> expect.equal(_, map_dict.from_list([ + |> map.update(_, "z", inc_or_zero) + |> expect.equal(_, map.from_list([ struct("a", 0), struct("b", 1), struct("c", 2), @@ -225,7 +225,7 @@ pub fn update_test() { } pub fn fold_test() { - let dict = map_dict.from_list([ + let dict = map.from_list([ struct("a", 0), struct("b", 1), struct("c", 2), @@ -237,7 +237,7 @@ pub fn fold_test() { } dict - |> map_dict.fold(_, 0, add) + |> map.fold(_, 0, add) |> expect.equal(_, 6) let concat = fn(k, _, acc) { @@ -245,10 +245,10 @@ pub fn fold_test() { } dict - |> map_dict.fold(_, "", concat) + |> map.fold(_, "", concat) |> expect.equal(_, "abcd") - map_dict.from_list([]) - |> map_dict.fold(_, 0, add) + map.from_list([]) + |> map.fold(_, 0, add) |> expect.equal(_, 0) } |