Skip to content

Instantly share code, notes, and snippets.

@furquanuddin94
Last active November 25, 2020 10:10
Show Gist options
  • Save furquanuddin94/b88f3bd2fa72eb0a026801674ccc142a to your computer and use it in GitHub Desktop.
Save furquanuddin94/b88f3bd2fa72eb0a026801674ccc142a to your computer and use it in GitHub Desktop.
//list of integers
val myList: List[Int] = List(1, 2, 7, 3)
//function to give neighbouring integers of an integer
def neighbourFunc: Int => List[Int] = value => List(value - 1, value + 1)
//using map() will give us an unflattened list
myList.map(neighbourFunc) //output: List(List(0, 2), List(1, 3), List(6, 8), List(2, 4))
//using map() + flatten() will give us a flattened list
myList.map(neighbourFunc).flatten //output: List(0, 2, 1, 3, 6, 8, 2, 4)
//using flatMap() will give us a flattened list
myList.flatMap(neighbourFunc) //output: List(0, 2, 1, 3, 6, 8, 2, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment