diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | gen/src/list.erl | 20 | ||||
-rw-r--r-- | gen/test/list_test.erl | 13 | ||||
-rw-r--r-- | src/list.gleam | 15 | ||||
-rw-r--r-- | test/list_test.gleam | 17 |
5 files changed, 64 insertions, 3 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f6b5e3..026597b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,7 @@ - The `map_dict` module gains `update`, `merge` and `delete` functions. - The `bool` module gains a `compare` function. - The `int` module gains a `compare` function. -- The `list` module gains `range`, `repeat`, `split`, and `strict_zip` functions. +- The `list` module gains `range`, `repeat`, `split`, `split_while` and `strict_zip` functions. ## v0.1.2 - 2019-04-25 diff --git a/gen/src/list.erl b/gen/src/list.erl index 1395112..8df10f6 100644 --- a/gen/src/list.erl +++ b/gen/src/list.erl @@ -1,7 +1,7 @@ -module(list). -compile(no_auto_import). --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]). +-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]). length(A) -> erlang:length(A). @@ -349,3 +349,21 @@ do_split(List, N, Taken) -> split(List, N) -> do_split(List, N, []). + +do_split_while(List, F, Acc) -> + case List of + [] -> + {reverse(Acc), []}; + + [X | Xs] -> + case F(X) of + false -> + {reverse(Acc), List}; + + _ -> + do_split_while(Xs, F, [X | Acc]) + end + end. + +split_while(List, F) -> + do_split_while(List, F, []). diff --git a/gen/test/list_test.erl b/gen/test/list_test.erl index 705cb3c..6cfe07f 100644 --- a/gen/test/list_test.erl +++ b/gen/test/list_test.erl @@ -1,7 +1,7 @@ -module(list_test). -compile(no_auto_import). --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]). +-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]). length_test() -> expect:equal(list:length([]), 0), @@ -170,3 +170,14 @@ split_test() -> expect:equal(list:split([0, 1, 2, 3, 4], 1), {[0], [1, 2, 3, 4]}), expect:equal(list:split([0, 1, 2, 3, 4], 3), {[0, 1, 2], [3, 4]}), expect:equal(list:split([0, 1, 2, 3, 4], 9), {[0, 1, 2, 3, 4], []}). + +split_while_test() -> + expect:equal(list:split_while([], fun(X) -> X =< 5 end), {[], []}), + expect:equal(list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< 5 end), + {[1, 2, 3, 4, 5], []}), + expect:equal(list:split_while([1, 2, 3, 4, 5], fun(X) -> X =:= 2 end), + {[], [1, 2, 3, 4, 5]}), + expect:equal(list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< 3 end), + {[1, 2, 3], [4, 5]}), + expect:equal(list:split_while([1, 2, 3, 4, 5], fun(X) -> X =< -3 end), + {[], [1, 2, 3, 4, 5]}). diff --git a/src/list.gleam b/src/list.gleam index 40c097e..58f36de 100644 --- a/src/list.gleam +++ b/src/list.gleam @@ -289,3 +289,18 @@ fn do_split(list, n, taken) { pub fn split(list, n) { do_split(list, n, []) } + +fn do_split_while(list, f, acc) { + case list { + | [] -> {reverse(acc), []} + | [x | xs] -> + case f(x) { + | False -> {reverse(acc), list} + | _ -> do_split_while(xs, f, [x | acc]) + } + } +} + +pub fn split_while(list, f) { + do_split_while(list, f, []) +} diff --git a/test/list_test.gleam b/test/list_test.gleam index 2311365..32484da 100644 --- a/test/list_test.gleam +++ b/test/list_test.gleam @@ -332,3 +332,20 @@ pub fn split_test() { list:split([0, 1, 2, 3, 4], 9) |> expect:equal(_, {[0, 1, 2, 3, 4], []}) } + +pub fn split_while_test() { + list:split_while([], fn(x) { x <= 5 }) + |> expect:equal(_, {[], []}) + + list:split_while([1, 2, 3, 4, 5], fn(x) { x <= 5 }) + |> expect:equal(_, {[1, 2, 3, 4, 5], []}) + + list:split_while([1, 2, 3, 4, 5], fn(x) { x == 2 }) + |> expect:equal(_, {[], [1, 2, 3, 4, 5]}) + + list:split_while([1, 2, 3, 4, 5], fn(x) { x <= 3 }) + |> expect:equal(_, {[1, 2, 3], [4, 5]}) + + list:split_while([1, 2, 3, 4, 5], fn(x) { x <= -3 }) + |> expect:equal(_, {[], [1, 2, 3, 4, 5]}) +} |