Code filter
function by using Array.map
and Array.flat
Example usage:
> filter([1,2,3], x => x !== 2)
[1,3]
> filter([1,2,3], x => true)
[1,2,3]
> filter([1,2,3], x => false)
[]
Also implement map
and flat
yourself.
It's enough that flat
only handles one level of depth.
> map([1,2], x => x*2)
[2, 4]
> flat([[1], [2], []])
[1, 2]
(Make your filter use your custom map
and flat
.)
Implement map
without loops.
> map([1,2], x => x*2)
[2, 4]
Implement flat
for any level of depth.
> flat([[1, [2, 3]], [4, 5, [6, [7]]]])
[1, 2, 3, 4, 5, 6, 7]