Skip to content

Commit afdabad

Browse files
committed
map:take
1 parent 6f486a3 commit afdabad

File tree

5 files changed

+75
-46
lines changed

5 files changed

+75
-46
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
- `list` module gains `at`, `all`, `any`, `index_map`, `intersperse`, `sort`,
66
`unique`, and `zip` functions.
77
- `map_dict:Map` renamed to `map_dict:MapDict`.
8-
- `map_dict` module gains `take`.
9-
- `str` module gains `append`.
8+
- `map_dict` module gains `drop`, and `take` functions.
9+
- `str` module gains `append` function.
1010

1111
## v0.1.1 - 2019-04-17
1212

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, take/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, drop/2]).
55

66
size(A) ->
77
maps:size(A).
@@ -53,3 +53,9 @@ erl_take(A, B) ->
5353

5454
take(Map, Keys) ->
5555
erl_take(Keys, Map).
56+
57+
erl_drop(A, B) ->
58+
maps:without(A, B).
59+
60+
drop(Map, Keys) ->
61+
erl_drop(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, take_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, drop_test/0]).
55

66
from_list_test() ->
77
expect:equal(map_dict:size(map_dict:from_list([{4, 0}, {1, 0}])), 2).
@@ -60,3 +60,10 @@ take_test() ->
6060
{<<"c">>, 2}]),
6161
[<<"a">>, <<"b">>, <<"d">>]),
6262
map_dict:from_list([{<<"a">>, 0}, {<<"b">>, 1}])).
63+
64+
drop_test() ->
65+
expect:equal(map_dict:drop(map_dict:from_list([{<<"a">>, 0},
66+
{<<"b">>, 1},
67+
{<<"c">>, 2}]),
68+
[<<"a">>, <<"b">>, <<"d">>]),
69+
map_dict:from_list([{<<"c">>, 2}])).

src/map_dict.gleam

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import any
22
import result
33

4-
// TODO: drop
54
// TODO: update :: fn(MapDict(k, v), k, fn(Result(v, NotFound)) -> v) -> MapDict(k, v)
65

76
pub external type MapDict(key, value);
@@ -65,3 +64,9 @@ external fn erl_take(List(k), MapDict(k, v)) -> MapDict(k, v) = "maps" "with"
6564
pub fn take(map, keys) {
6665
erl_take(keys, map)
6766
}
67+
68+
external fn erl_drop(List(k), MapDict(k, v)) -> MapDict(k, v) = "maps" "without"
69+
70+
pub fn drop(map, keys) {
71+
erl_drop(keys, map)
72+
}

test/map_dict_test.gleam

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,49 @@ pub fn from_list_test() {
66
{4, 0},
77
{1, 0},
88
]
9-
|> map_dict:from_list
10-
|> map_dict:size
11-
|> expect:equal(_, 2)
9+
|> map_dict:from_list
10+
|> map_dict:size
11+
|> expect:equal(_, 2)
1212
}
1313

1414
pub fn has_key_test() {
1515
[]
16-
|> map_dict:from_list
17-
|> map_dict:has_key(_, 1)
18-
|> expect:false
16+
|> map_dict:from_list
17+
|> map_dict:has_key(_, 1)
18+
|> expect:false
1919

2020
[
2121
{1, 0},
2222
]
23-
|> map_dict:from_list
24-
|> map_dict:has_key(_, 1)
25-
|> expect:true
23+
|> map_dict:from_list
24+
|> map_dict:has_key(_, 1)
25+
|> expect:true
2626

2727
[
2828
{4, 0},
2929
{1, 0},
3030
]
31-
|> map_dict:from_list
32-
|> map_dict:has_key(_, 1)
33-
|> expect:true
31+
|> map_dict:from_list
32+
|> map_dict:has_key(_, 1)
33+
|> expect:true
3434

3535
[
3636
{4, 0},
3737
{1, 0},
3838
]
39-
|> map_dict:from_list
40-
|> map_dict:has_key(_, 0)
41-
|> expect:false
39+
|> map_dict:from_list
40+
|> map_dict:has_key(_, 0)
41+
|> expect:false
4242
}
4343

4444
pub fn new_test() {
4545
map_dict:new()
46-
|> map_dict:size
47-
|> expect:equal(_, 0)
46+
|> map_dict:size
47+
|> expect:equal(_, 0)
4848

4949
map_dict:new()
50-
|> map_dict:to_list
51-
|> expect:equal(_, [])
50+
|> map_dict:to_list
51+
|> expect:equal(_, [])
5252
}
5353

5454
pub fn fetch_test() {
@@ -59,24 +59,24 @@ pub fn fetch_test() {
5959
let m = map_dict:from_list(proplist)
6060

6161
m
62-
|> map_dict:fetch(_, 4)
63-
|> expect:equal(_, Ok(0))
62+
|> map_dict:fetch(_, 4)
63+
|> expect:equal(_, Ok(0))
6464

6565
m
66-
|> map_dict:fetch(_, 1)
67-
|> expect:equal(_, Ok(1))
66+
|> map_dict:fetch(_, 1)
67+
|> expect:equal(_, Ok(1))
6868

6969
m
70-
|> map_dict:fetch(_, 2)
71-
|> expect:is_error
70+
|> map_dict:fetch(_, 2)
71+
|> expect:is_error
7272
}
7373

7474
pub fn put_test() {
7575
map_dict:new()
76-
|> map_dict:put(_, "a", 0)
77-
|> map_dict:put(_, "b", 1)
78-
|> map_dict:put(_, "c", 2)
79-
|> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}, {"c", 2}]))
76+
|> map_dict:put(_, "a", 0)
77+
|> map_dict:put(_, "b", 1)
78+
|> map_dict:put(_, "c", 2)
79+
|> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}, {"c", 2}]))
8080
}
8181

8282
pub fn map_values_test() {
@@ -85,9 +85,9 @@ pub fn map_values_test() {
8585
{2, 1},
8686
{3, 2},
8787
]
88-
|> map_dict:from_list
89-
|> map_dict:map_values(_, fn(k, v) { k + v })
90-
|> expect:equal(_, map_dict:from_list([{1, 1}, {2, 3}, {3, 5}]))
88+
|> map_dict:from_list
89+
|> map_dict:map_values(_, fn(k, v) { k + v })
90+
|> expect:equal(_, map_dict:from_list([{1, 1}, {2, 3}, {3, 5}]))
9191
}
9292

9393
pub fn keys_test() {
@@ -96,9 +96,9 @@ pub fn keys_test() {
9696
{"b", 1},
9797
{"c", 2},
9898
]
99-
|> map_dict:from_list
100-
|> map_dict:keys
101-
|> expect:equal(_, ["a", "b", "c"])
99+
|> map_dict:from_list
100+
|> map_dict:keys
101+
|> expect:equal(_, ["a", "b", "c"])
102102
}
103103

104104
pub fn values_test() {
@@ -107,9 +107,9 @@ pub fn values_test() {
107107
{"b", 1},
108108
{"c", 2},
109109
]
110-
|> map_dict:from_list
111-
|> map_dict:values
112-
|> expect:equal(_, [0, 1, 2])
110+
|> map_dict:from_list
111+
|> map_dict:values
112+
|> expect:equal(_, [0, 1, 2])
113113
}
114114

115115
pub fn take_test() {
@@ -118,7 +118,18 @@ pub fn take_test() {
118118
{"b", 1},
119119
{"c", 2},
120120
]
121-
|> map_dict:from_list
122-
|> map_dict:take(_, ["a", "b", "d"])
123-
|> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}]))
121+
|> map_dict:from_list
122+
|> map_dict:take(_, ["a", "b", "d"])
123+
|> expect:equal(_, map_dict:from_list([{"a", 0}, {"b", 1}]))
124+
}
125+
126+
pub fn drop_test() {
127+
[
128+
{"a", 0},
129+
{"b", 1},
130+
{"c", 2},
131+
]
132+
|> map_dict:from_list
133+
|> map_dict:drop(_, ["a", "b", "d"])
134+
|> expect:equal(_, map_dict:from_list([{"c", 2}]))
124135
}

0 commit comments

Comments
 (0)