Skip to content

Commit ae854f6

Browse files
bsnyder788lpil
authored andcommitted
list:split_while
1 parent f7d6fe3 commit ae854f6

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-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 `range`, `repeat`, `split`, and `strict_zip` functions.
8+
- The `list` module gains `range`, `repeat`, `split`, `split_while` and `strict_zip` functions.
99

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

gen/src/list.erl

Lines changed: 19 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, strict_zip/2, intersperse/2, at/2, unique/1, sort/1, range/2, repeat/2, split/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, repeat/2, split/2, split_while/2]).
55

66
length(A) ->
77
erlang:length(A).
@@ -349,3 +349,21 @@ do_split(List, N, Taken) ->
349349

350350
split(List, N) ->
351351
do_split(List, N, []).
352+
353+
do_split_while(List, F, Acc) ->
354+
case List of
355+
[] ->
356+
{reverse(Acc), []};
357+
358+
[X | Xs] ->
359+
case F(X) of
360+
false ->
361+
{reverse(Acc), List};
362+
363+
_ ->
364+
do_split_while(Xs, F, [X | Acc])
365+
end
366+
end.
367+
368+
split_while(List, F) ->
369+
do_split_while(List, F, []).

gen/test/list_test.erl

Lines changed: 12 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, strict_zip_test/0, intersperse_test/0, at_test/0, unique_test/0, sort_test/0, index_map_test/0, range_test/0, repeat_test/0, split_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, repeat_test/0, split_test/0, split_while_test/0]).
55

66
length_test() ->
77
expect:equal(list:length([]), 0),
@@ -170,3 +170,14 @@ split_test() ->
170170
expect:equal(list:split([0, 1, 2, 3, 4], 1), {[0], [1, 2, 3, 4]}),
171171
expect:equal(list:split([0, 1, 2, 3, 4], 3), {[0, 1, 2], [3, 4]}),
172172
expect:equal(list:split([0, 1, 2, 3, 4], 9), {[0, 1, 2, 3, 4], []}).
173+
174+
split_while_test() ->
175+
expect:equal(list:split_while([], fun(X) -> X =< 5 end), {[], []}),
176+
expect:equal(list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< 5 end),
177+
{[1, 2, 3, 4, 5], []}),
178+
expect:equal(list:split_while([1, 2, 3, 4, 5], fun(X) -> X =:= 2 end),
179+
{[], [1, 2, 3, 4, 5]}),
180+
expect:equal(list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< 3 end),
181+
{[1, 2, 3], [4, 5]}),
182+
expect:equal(list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< -3 end),
183+
{[], [1, 2, 3, 4, 5]}).

src/list.gleam

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,18 @@ fn do_split(list, n, taken) {
289289
pub fn split(list, n) {
290290
do_split(list, n, [])
291291
}
292+
293+
fn do_split_while(list, f, acc) {
294+
case list {
295+
| [] -> {reverse(acc), []}
296+
| [x | xs] ->
297+
case f(x) {
298+
| False -> {reverse(acc), list}
299+
| _ -> do_split_while(xs, f, [x | acc])
300+
}
301+
}
302+
}
303+
304+
pub fn split_while(list, f) {
305+
do_split_while(list, f, [])
306+
}

test/list_test.gleam

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,20 @@ pub fn split_test() {
332332
list:split([0, 1, 2, 3, 4], 9)
333333
|> expect:equal(_, {[0, 1, 2, 3, 4], []})
334334
}
335+
336+
pub fn split_while_test() {
337+
list:split_while([], fn(x) { x <= 5 })
338+
|> expect:equal(_, {[], []})
339+
340+
list:split_while([1, 2, 3, 4, 5], fn(x) { x <= 5 })
341+
|> expect:equal(_, {[1, 2, 3, 4, 5], []})
342+
343+
list:split_while([1, 2, 3, 4, 5], fn(x) { x == 2 })
344+
|> expect:equal(_, {[], [1, 2, 3, 4, 5]})
345+
346+
list:split_while([1, 2, 3, 4, 5], fn(x) { x <= 3 })
347+
|> expect:equal(_, {[1, 2, 3], [4, 5]})
348+
349+
list:split_while([1, 2, 3, 4, 5], fn(x) { x <= -3 })
350+
|> expect:equal(_, {[], [1, 2, 3, 4, 5]})
351+
}

0 commit comments

Comments
 (0)