builtins: Implement List.mapWithIndex in pure Roc

This commit is contained in:
Brian Carroll 2022-07-04 14:57:18 +01:00
parent 4523e90bc7
commit 557ae4dd9c
No known key found for this signature in database
GPG Key ID: 9CF4E3BF9C4722C7

View File

@ -570,6 +570,23 @@ map4 : List a, List b, List c, List d, (a, b, c, d -> e) -> List e
## This works like [List.map], except it also passes the index
## of the element to the conversion function.
mapWithIndex : List a, (a, Nat -> b) -> List b
mapWithIndex = \src, func ->
length = len src
dest = withCapacity length
mapWithIndexHelp src dest func 0 length
# Internal helper
mapWithIndexHelp : List a, List b, (a, Nat -> b), Nat, Nat -> List b
mapWithIndexHelp = \src, dest, func, index, length ->
if index < length then
elem = getUnsafe src index
mappedElem = func elem index
newDest = append dest mappedElem
mapWithIndexHelp src newDest func (index + 1) length
else
dest
## Returns a list of all the integers between one and another,
## including both of the given numbers.