diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/list.gleam | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index f69e82c..662e3f7 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -1219,3 +1219,37 @@ pub fn window(l: List(a), by n: Int) -> List(List(a)) { pub fn window_by_2(l: List(a)) -> List(tuple(a, a)) { zip(l, drop(l, 1)) } + +external fn erl_drop_while(fn(a) -> Bool, List(a)) -> List(a) = + "lists" "dropwhile" + +/// Drops the first elements in a given list for which the predicate funtion returns `True`. +/// +/// ## Examples +/// +/// > drop_while([1, 2, 3, 4], fun (x) { x < 3 }) +/// [3, 4] +/// +pub fn drop_while( + in list: List(a), + satisfying predicate: fn(a) -> Bool, +) -> List(a) { + erl_drop_while(predicate, list) +} + +external fn erl_take_while(fn(a) -> Bool, List(a)) -> List(a) = + "lists" "takewhile" + +/// Takes the first elements in a given list for which the predicate funtion returns `True`. +/// +/// ## Examples +/// +/// > take_while([1, 2, 3, 4], fun (x) { x < 3 }) +/// [1, 2] +/// +pub fn take_while( + in list: List(a), + satisfying predicate: fn(a) -> Bool, +) -> List(a) { + erl_take_while(predicate, list) +} |