aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorevuez <helloevuez@gmail.com>2021-02-20 18:16:14 +0100
committerLouis Pilfold <louis@lpil.uk>2021-02-23 13:08:29 +0000
commit62be318cc3ba9fa4262c4b036b916747000b058f (patch)
tree18f80c9c15500f74ac43583d827ddbeb558a96d0
parent3a9181fc74ae1f71e109d074b0cebaba399184d0 (diff)
downloadgleam_stdlib-62be318cc3ba9fa4262c4b036b916747000b058f.tar.gz
gleam_stdlib-62be318cc3ba9fa4262c4b036b916747000b058f.zip
Implement drop_while and take_while in Gleam
-rw-r--r--src/gleam/list.gleam24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 662e3f7..570769d 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -1220,9 +1220,6 @@ 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
@@ -1234,12 +1231,16 @@ pub fn drop_while(
in list: List(a),
satisfying predicate: fn(a) -> Bool,
) -> List(a) {
- erl_drop_while(predicate, list)
+ case list {
+ [] -> []
+ [x, ..xs] ->
+ case predicate(x) {
+ True -> drop_while(xs, predicate)
+ False -> [x, ..xs]
+ }
+ }
}
-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
@@ -1251,5 +1252,12 @@ pub fn take_while(
in list: List(a),
satisfying predicate: fn(a) -> Bool,
) -> List(a) {
- erl_take_while(predicate, list)
+ case list {
+ [] -> []
+ [x, ..xs] ->
+ case predicate(x) {
+ True -> [x, ..take_while(xs, predicate)]
+ False -> []
+ }
+ }
}