diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-04-18 22:26:14 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-04-18 23:29:08 +0100 |
commit | afdabad5cd2df77eb3f309aab9da3d34e36a0b49 (patch) | |
tree | ed6ae77397a00a1715a0df25b1b187f713c20642 | |
parent | 6f486a387b623d962868bc514ebe5fcdea84e012 (diff) | |
download | gleam_stdlib-afdabad5cd2df77eb3f309aab9da3d34e36a0b49.tar.gz gleam_stdlib-afdabad5cd2df77eb3f309aab9da3d34e36a0b49.zip |
map:take
-rw-r--r-- | CHANGELOG.md | 4 | ||||
-rw-r--r-- | gen/src/map_dict.erl | 8 | ||||
-rw-r--r-- | gen/test/map_dict_test.erl | 9 | ||||
-rw-r--r-- | src/map_dict.gleam | 7 | ||||
-rw-r--r-- | test/map_dict_test.gleam | 93 |
5 files changed, 75 insertions, 46 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 98070e6..33257e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,8 @@ - `list` module gains `at`, `all`, `any`, `index_map`, `intersperse`, `sort`, `unique`, and `zip` functions. - `map_dict:Map` renamed to `map_dict:MapDict`. -- `map_dict` module gains `take`. -- `str` module gains `append`. +- `map_dict` module gains `drop`, and `take` functions. +- `str` module gains `append` function. ## v0.1.1 - 2019-04-17 diff --git a/gen/src/map_dict.erl b/gen/src/map_dict.erl index eefdfc3..7d97a7d 100644 --- a/gen/src/map_dict.erl +++ b/gen/src/map_dict.erl @@ -1,7 +1,7 @@ -module(map_dict). -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]). +-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, drop/2]). size(A) -> maps:size(A). @@ -53,3 +53,9 @@ erl_take(A, B) -> take(Map, Keys) -> erl_take(Keys, Map). + +erl_drop(A, B) -> + maps:without(A, B). + +drop(Map, Keys) -> + erl_drop(Keys, Map). diff --git a/gen/test/map_dict_test.erl b/gen/test/map_dict_test.erl index a5a0d8a..e59911e 100644 --- a/gen/test/map_dict_test.erl +++ b/gen/test/map_dict_test.erl @@ -1,7 +1,7 @@ -module(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]). +-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]). from_list_test() -> expect:equal(map_dict:size(map_dict:from_list([{4, 0}, {1, 0}])), 2). @@ -60,3 +60,10 @@ take_test() -> {<<"c">>, 2}]), [<<"a">>, <<"b">>, <<"d">>]), map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}])). + +drop_test() -> + expect:equal(map_dict:drop(map_dict:from_list([{<<"a">>, 0}, + {<<"b">>, 1}, + {<<"c">>, 2}]), + [<<"a">>, <<"b">>, <<"d">>]), + map_dict:from_list([{<<"c">>, 2}])). diff --git a/src/map_dict.gleam b/src/map_dict.gleam index 489b260..7dd00e6 100644 --- a/src/map_dict.gleam +++ b/src/map_dict.gleam @@ -1,7 +1,6 @@ import any import result -// TODO: drop // TODO: update :: fn(MapDict(k, v), k, fn(Result(v, NotFound)) -> v) -> MapDict(k, v) pub external type MapDict(key, value); @@ -65,3 +64,9 @@ external fn erl_take(List(k), MapDict(k, v)) -> MapDict(k, v) = "maps" "with" pub fn take(map, keys) { erl_take(keys, map) } + +external fn erl_drop(List(k), MapDict(k, v)) -> MapDict(k, v) = "maps" "without" + +pub fn drop(map, keys) { + erl_drop(keys, map) +} diff --git a/test/map_dict_test.gleam b/test/map_dict_test.gleam index f72e23a..a0797c2 100644 --- a/test/map_dict_test.gleam +++ b/test/map_dict_test.gleam @@ -6,49 +6,49 @@ pub fn from_list_test() { {4, 0}, {1, 0}, ] - |> map_dict:from_list - |> map_dict:size - |> expect:equal(_, 2) + |> map_dict:from_list + |> map_dict:size + |> expect:equal(_, 2) } pub fn has_key_test() { [] - |> map_dict:from_list - |> map_dict:has_key(_, 1) - |> expect:false + |> map_dict:from_list + |> map_dict:has_key(_, 1) + |> expect:false [ {1, 0}, ] - |> map_dict:from_list - |> map_dict:has_key(_, 1) - |> expect:true + |> map_dict:from_list + |> map_dict:has_key(_, 1) + |> expect:true [ {4, 0}, {1, 0}, ] - |> map_dict:from_list - |> map_dict:has_key(_, 1) - |> expect:true + |> map_dict:from_list + |> map_dict:has_key(_, 1) + |> expect:true [ {4, 0}, {1, 0}, ] - |> map_dict:from_list - |> map_dict:has_key(_, 0) - |> expect:false + |> map_dict:from_list + |> map_dict:has_key(_, 0) + |> expect:false } pub fn new_test() { map_dict:new() - |> map_dict:size - |> expect:equal(_, 0) + |> map_dict:size + |> expect:equal(_, 0) map_dict:new() - |> map_dict:to_list - |> expect:equal(_, []) + |> map_dict:to_list + |> expect:equal(_, []) } pub fn fetch_test() { @@ -59,24 +59,24 @@ pub fn fetch_test() { let m = map_dict:from_list(proplist) m - |> map_dict:fetch(_, 4) - |> expect:equal(_, Ok(0)) + |> map_dict:fetch(_, 4) + |> expect:equal(_, Ok(0)) m - |> map_dict:fetch(_, 1) - |> expect:equal(_, Ok(1)) + |> map_dict:fetch(_, 1) + |> expect:equal(_, Ok(1)) m - |> map_dict:fetch(_, 2) - |> expect:is_error + |> map_dict: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([{"a", 0}, {"b", 1}, {"c", 2}])) + |> map_dict:put(_, "a", 0) + |> map_dict:put(_, "b", 1) + |> map_dict:put(_, "c", 2) + |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}, {"c", 2}])) } pub fn map_values_test() { @@ -85,9 +85,9 @@ pub fn map_values_test() { {2, 1}, {3, 2}, ] - |> map_dict:from_list - |> map_dict:map_values(_, fn(k, v) { k + v }) - |> expect:equal(_, map_dict:from_list([{1, 1}, {2, 3}, {3, 5}])) + |> map_dict:from_list + |> map_dict:map_values(_, fn(k, v) { k + v }) + |> expect:equal(_, map_dict:from_list([{1, 1}, {2, 3}, {3, 5}])) } pub fn keys_test() { @@ -96,9 +96,9 @@ pub fn keys_test() { {"b", 1}, {"c", 2}, ] - |> map_dict:from_list - |> map_dict:keys - |> expect:equal(_, ["a", "b", "c"]) + |> map_dict:from_list + |> map_dict:keys + |> expect:equal(_, ["a", "b", "c"]) } pub fn values_test() { @@ -107,9 +107,9 @@ pub fn values_test() { {"b", 1}, {"c", 2}, ] - |> map_dict:from_list - |> map_dict:values - |> expect:equal(_, [0, 1, 2]) + |> map_dict:from_list + |> map_dict:values + |> expect:equal(_, [0, 1, 2]) } pub fn take_test() { @@ -118,7 +118,18 @@ pub fn take_test() { {"b", 1}, {"c", 2}, ] - |> map_dict:from_list - |> map_dict:take(_, ["a", "b", "d"]) - |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}])) + |> map_dict:from_list + |> map_dict:take(_, ["a", "b", "d"]) + |> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}])) +} + +pub fn drop_test() { + [ + {"a", 0}, + {"b", 1}, + {"c", 2}, + ] + |> map_dict:from_list + |> map_dict:drop(_, ["a", "b", "d"]) + |> expect:equal(_, map_dict:from_list([{"c", 2}])) } |