March 2019
Intermediate to advanced
208 pages
5h 11m
English
As you saw in the chapter about collections, the Belt.Array and Belt.List modules provide a keep function that produces a new collection of only those items which return true when given to a filtering function.
What if we want to keep only the indices of the matching items? For example, when filtering this array for words less than 6 characters long: [| "cow", "aardvark", "squirrel", "fish", "snake", "capybara"|], the result would be [|0, 3, 4|].
Here’s the pseudocode, given an empty array of integers as the accumulated result, a position starting at index number zero, and a predicate function f:
| | If the position is at the end of the array: |
| | return the accumulated result (base case) |
| | otherwise: |
| | If f(item at ... |
Read now
Unlock full access