Skip to content

Instantly share code, notes, and snippets.

@elnygren
Last active November 3, 2020 14:42
Show Gist options
  • Save elnygren/b758de1df48db5acdbceadfb1188136e to your computer and use it in GitHub Desktop.
Save elnygren/b758de1df48db5acdbceadfb1188136e to your computer and use it in GitHub Desktop.
FlatMapFilter

Assignment 1

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)
[]

Assignment 2

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.)

Assignment 3

Implement map without loops.

> map([1,2], x => x*2)
[2, 4]

Assignment 4

Implement flat for any level of depth.

> flat([[1, [2, 3]], [4, 5, [6, [7]]]])
[1, 2, 3, 4, 5, 6, 7]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment