Skip to content

Commit 83525b2

Browse files
bsnyder788lpil
authored andcommitted
list:strict_zip (#167)
1 parent d2823c6 commit 83525b2

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- The `map_dict` module gains `update`, `merge` and `delete` functions.
66
- The `bool` module gains a `compare` function.
77
- The `int` module gains a `compare` function.
8-
- The `list` module gains a `range` function.
8+
- The `list` module gains `range`, and `strict_zip` functions.
99

1010
## v0.1.2 - 2019-04-25
1111

gen/src/list.erl

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

4-
-export([length/1, reverse/1, is_empty/1, contains/2, head/1, tail/1, filter/2, map/2, index_map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, fold/3, fold_right/3, find/2, all/2, any/2, zip/2, intersperse/2, at/2, unique/1, sort/1, range/2]).
4+
-export([length/1, reverse/1, is_empty/1, contains/2, head/1, tail/1, filter/2, map/2, index_map/2, traverse/2, drop/2, take/2, new/0, append/2, flatten/1, fold/3, fold_right/3, find/2, all/2, any/2, zip/2, strict_zip/2, intersperse/2, at/2, unique/1, sort/1, range/2]).
55

66
length(A) ->
77
erlang:length(A).
@@ -226,6 +226,15 @@ zip(L1, L2) ->
226226
[{X1, X2} | zip(Rest1, Rest2)]
227227
end.
228228

229+
strict_zip(L1, L2) ->
230+
case length(L1) =:= length(L2) of
231+
true ->
232+
{ok, zip(L1, L2)};
233+
234+
false ->
235+
{error, length_mismatch}
236+
end.
237+
229238
intersperse(List, Elem) ->
230239
case List of
231240
[] ->

gen/test/list_test.erl

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

4-
-export([length_test/0, reverse_test/0, is_empty_test/0, contains_test/0, head_test/0, tail_test/0, filter_test/0, map_test/0, traverse_test/0, drop_test/0, take_test/0, new_test/0, append_test/0, flatten_test/0, fold_test/0, fold_right_test/0, find_test/0, all_test/0, any_test/0, zip_test/0, intersperse_test/0, at_test/0, unique_test/0, sort_test/0, index_map_test/0, range_test/0]).
4+
-export([length_test/0, reverse_test/0, is_empty_test/0, contains_test/0, head_test/0, tail_test/0, filter_test/0, map_test/0, traverse_test/0, drop_test/0, take_test/0, new_test/0, append_test/0, flatten_test/0, fold_test/0, fold_right_test/0, find_test/0, all_test/0, any_test/0, zip_test/0, strict_zip_test/0, intersperse_test/0, at_test/0, unique_test/0, sort_test/0, index_map_test/0, range_test/0]).
55

66
length_test() ->
77
expect:equal(list:length([]), 0),
@@ -111,6 +111,13 @@ zip_test() ->
111111
expect:equal(list:zip([5, 6], [1, 2, 3]), [{5, 1}, {6, 2}]),
112112
expect:equal(list:zip([5, 6, 7], [1, 2]), [{5, 1}, {6, 2}]).
113113

114+
strict_zip_test() ->
115+
expect:is_error(list:strict_zip([], [1, 2, 3])),
116+
expect:is_error(list:strict_zip([1, 2], [])),
117+
expect:equal(list:zip([1, 2, 3], [4, 5, 6]), [{1, 4}, {2, 5}, {3, 6}]),
118+
expect:is_error(list:strict_zip([5, 6], [1, 2, 3])),
119+
expect:is_error(list:strict_zip([5, 6, 7], [1, 2])).
120+
114121
intersperse_test() ->
115122
expect:equal(list:intersperse([1, 2, 3], 4), [1, 4, 2, 4, 3]),
116123
expect:equal(list:intersperse([], 2), []).

src/list.gleam

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ pub enum Empty =
77
pub enum NotFound =
88
| NotFound
99

10+
pub enum LengthMismatch =
11+
| LengthMismatch
12+
1013
// Using the Erlang C BIF implementation.
1114
//
1215
pub external fn length(List(a)) -> Int = "erlang" "length"
@@ -192,6 +195,13 @@ pub fn zip(l1, l2) {
192195
}
193196
}
194197

198+
pub fn strict_zip(l1, l2) {
199+
case length(l1) == length(l2) {
200+
| True -> Ok(zip(l1, l2))
201+
| False -> Error(LengthMismatch)
202+
}
203+
}
204+
195205
pub fn intersperse(list, elem) {
196206
case list {
197207
| [] -> []

test/list_test.gleam

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,23 @@ pub fn zip_test() {
204204
|> expect:equal(_, [{5, 1}, {6, 2}])
205205
}
206206

207+
pub fn strict_zip_test() {
208+
list:strict_zip([], [1, 2, 3])
209+
|> expect:is_error
210+
211+
list:strict_zip([1, 2], [])
212+
|> expect:is_error
213+
214+
list:zip([1, 2, 3], [4, 5, 6])
215+
|> expect:equal(_, [{1, 4}, {2, 5}, {3, 6}])
216+
217+
list:strict_zip([5, 6], [1, 2, 3])
218+
|> expect:is_error
219+
220+
list:strict_zip([5, 6, 7], [1, 2])
221+
|> expect:is_error
222+
}
223+
207224
pub fn intersperse_test() {
208225
list:intersperse([1, 2, 3], 4)
209226
|> expect:equal(_, [1, 4, 2, 4, 3])

0 commit comments

Comments
 (0)