Skip to content

Commit 6f486a3

Browse files
committed
map_dict:Map -> map:MapDict, map_dict:take
1 parent 187b55b commit 6f486a3

File tree

5 files changed

+49
-16
lines changed

5 files changed

+49
-16
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
- `list` module gains `at`, `all`, `any`, `index_map`, `intersperse`, `sort`,
66
`unique`, and `zip` functions.
7+
- `map_dict:Map` renamed to `map_dict:MapDict`.
8+
- `map_dict` module gains `take`.
79
- `str` module gains `append`.
810

911
## v0.1.1 - 2019-04-17

gen/src/map_dict.erl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-module(map_dict).
22
-compile(no_auto_import).
33

4-
-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]).
4+
-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]).
55

66
size(A) ->
77
maps:size(A).
@@ -47,3 +47,9 @@ erl_filter(A, B) ->
4747

4848
filter(Map, Fun) ->
4949
erl_filter(Fun, Map).
50+
51+
erl_take(A, B) ->
52+
maps:with(A, B).
53+
54+
take(Map, Keys) ->
55+
erl_take(Keys, Map).

gen/test/map_dict_test.erl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
-module(map_dict_test).
22
-compile(no_auto_import).
33

4-
-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]).
4+
-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]).
55

66
from_list_test() ->
77
expect:equal(map_dict:size(map_dict:from_list([{4, 0}, {1, 0}])), 2).
@@ -53,3 +53,10 @@ values_test() ->
5353
{<<"b">>, 1},
5454
{<<"c">>, 2}])),
5555
[0, 1, 2]).
56+
57+
take_test() ->
58+
expect:equal(map_dict:take(map_dict:from_list([{<<"a">>, 0},
59+
{<<"b">>, 1},
60+
{<<"c">>, 2}]),
61+
[<<"a">>, <<"b">>, <<"d">>]),
62+
map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}])).

src/map_dict.gleam

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,66 @@ import any
22
import result
33

44
// TODO: drop
5-
// TODO: take
6-
// TODO: update :: fn(Map(k, v), k, fn(Result(v, NotFound)) -> v) -> Map(k, v)
5+
// TODO: update :: fn(MapDict(k, v), k, fn(Result(v, NotFound)) -> v) -> MapDict(k, v)
76

8-
pub external type Map(key, value);
7+
pub external type MapDict(key, value);
98

109
pub enum NotFound =
1110
| NotFound
1211

13-
pub external fn size(Map(k, v)) -> Int
12+
pub external fn size(MapDict(k, v)) -> Int
1413
= "maps" "size"
1514

16-
pub external fn to_list(Map(key, value)) -> List({key, value})
15+
pub external fn to_list(MapDict(key, value)) -> List({key, value})
1716
= "maps" "to_list"
1817

19-
pub external fn from_list(List({key, value})) -> Map(key, value)
18+
pub external fn from_list(List({key, value})) -> MapDict(key, value)
2019
= "maps" "from_list"
2120

22-
external fn is_key(key, Map(key, v)) -> Bool
21+
external fn is_key(key, MapDict(key, v)) -> Bool
2322
= "maps" "is_key"
2423

2524
pub fn has_key(map, key) {
2625
is_key(key, map)
2726
}
2827

29-
pub external fn new() -> Map(key, value)
28+
pub external fn new() -> MapDict(key, value)
3029
= "maps" "new"
3130

32-
pub external fn fetch(Map(key, value), key) -> Result(value, NotFound)
31+
pub external fn fetch(MapDict(key, value), key) -> Result(value, NotFound)
3332
= "gleam__stdlib" "map_fetch";
3433

35-
external fn erl_put(key, value, Map(key, value)) -> Map(key, value)
34+
external fn erl_put(key, value, MapDict(key, value)) -> MapDict(key, value)
3635
= "maps" "put";
3736

3837
pub fn put(map, key, value) {
3938
erl_put(key, value, map)
4039
}
4140

42-
external fn erl_map_values(fn(key, value) -> value, Map(key, value)) -> Map(key, value)
41+
external fn erl_map_values(fn(key, value) -> value, MapDict(key, value))
42+
-> MapDict(key, value)
4343
= "maps" "map";
4444

4545
pub fn map_values(map, fun) {
4646
erl_map_values(fun, map)
4747
}
4848

49-
pub external fn keys(Map(keys, v)) -> List(keys)
49+
pub external fn keys(MapDict(keys, v)) -> List(keys)
5050
= "maps" "keys"
5151

52-
pub external fn values(Map(k, values)) -> List(values)
52+
pub external fn values(MapDict(k, values)) -> List(values)
5353
= "maps" "values"
5454

55-
external fn erl_filter(fn(key, value) -> Bool, Map(key, value)) -> Map(key, value)
55+
external fn erl_filter(fn(key, value) -> Bool, MapDict(key, value))
56+
-> MapDict(key, value)
5657
= "maps" "filter";
5758

5859
pub fn filter(map, fun) {
5960
erl_filter(fun, map)
6061
}
62+
63+
external fn erl_take(List(k), MapDict(k, v)) -> MapDict(k, v) = "maps" "with"
64+
65+
pub fn take(map, keys) {
66+
erl_take(keys, map)
67+
}

test/map_dict_test.gleam

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,14 @@ pub fn values_test() {
111111
|> map_dict:values
112112
|> expect:equal(_, [0, 1, 2])
113113
}
114+
115+
pub fn take_test() {
116+
[
117+
{"a", 0},
118+
{"b", 1},
119+
{"c", 2},
120+
]
121+
|> map_dict:from_list
122+
|> map_dict:take(_, ["a", "b", "d"])
123+
|> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}]))
124+
}

0 commit comments

Comments
 (0)