Last active
November 25, 2020 10:10
-
-
Save furquanuddin94/b88f3bd2fa72eb0a026801674ccc142a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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