March 2019
Intermediate to advanced
208 pages
5h 11m
English
The Belt.List module has two functions: take, which gives you the first n elements of a list, and drop, which gives you everything except the first n elements of a list.
Your task is to use recursion to write two new functions, takeWhile and dropWhile. Instead of taking a parameter n, the parameter is a predicate function returning true or false when given an item from the list. takeWhile gives the first elements of the list that satisfy the predicate, stopping when it encounters an element that doesn’t fit. dropWhile returns everything except the first elements of the list that satisfy the predicate. Here is an example:
| | let data = [2, 6, 42, 5, 7, 20, 3]; |
| | let isEven = (n) => { (n mod 2) == 0 }; |
| | |
| | let ... |
Read now
Unlock full access