implement keepIf in pure roc

This commit is contained in:
Folkert 2022-07-02 12:38:17 +02:00
parent 3986d4a87d
commit 616ddd6fe4
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C

View file

@ -452,6 +452,20 @@ all = \list, predicate ->
## list unaltered.
##
keepIf : List a, (a -> Bool) -> List a
keepIf = \list, predicate ->
length = List.len list
keepIfHelp list predicate 0 0 length
keepIfHelp : List a, (a -> Bool), Nat, Nat, Nat -> List a
keepIfHelp = \list, predicate, kept, index, length ->
if index < length then
if predicate (List.getUnsafe list index) then
keepIfHelp (List.swap list kept index) predicate (kept + 1) (index + 1) length
else
keepIfHelp list predicate kept (index + 1) length
else
List.takeFirst list kept
## Run the given function on each element of a list, and return all the
## elements for which the function returned `False`.