Skip to content

Instantly share code, notes, and snippets.

@reevik
Created February 14, 2022 20:04
Show Gist options
  • Save reevik/53d6506678289357289418f245fc83a0 to your computer and use it in GitHub Desktop.
Save reevik/53d6506678289357289418f245fc83a0 to your computer and use it in GitHub Desktop.
// "x => x + 1" could be reduced to "_ + 1" using placeholder syntax.
> Some(10) flatMap(x => x + 1)
res0: Option[Int] = Some(45)
> Some(10) flatMap(x => None)
res0: Option[Nothing] = None
> List(Some(1), None, Some(3), None).map(a => \
a.flatMap(b => Some(b + 1)))
res0: List[Option[Int]] = List(Some(2), None, Some(4), None)
// if you want to get the list of Ints
> List(Some(1), None, Some(3), None).map(a => \
a.flatMap(b => Some(b + 1)))
flatten
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment