Skip to content

Instantly share code, notes, and snippets.

@justinhj
Created September 11, 2019 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinhj/dba9613eb3d169508a307c1f90a5b84e to your computer and use it in GitHub Desktop.
Save justinhj/dba9613eb3d169508a307c1f90a5b84e to your computer and use it in GitHub Desktop.
List and Option in for
// Ammonite repl output
@ desugar {
for (
s <- List(1,2,3);
o <- if(s % 2 == 1) Some(s) else None
) yield (s + o)
}
res12: Desugared = scala.collection.immutable.List.apply[Int](1, 2, 3).flatMap[Int, Any](((s: Int) => scala.Option.option2Iterable[Int]((if (s.%(2).==(1))
scala.Some.apply[Int](s)
else
scala.None).map[Int](((o: Int) => s.+(o))))))(scala.collection.immutable.List.canBuildFrom[Int])
// Cleaned up
List.apply[Int](1, 2, 3).flatMap[Int, Any](((s: Int) => Option.option2Iterable[Int](
(if (s.%(2).==(1))
Some.apply[Int](s)
else
None).
map[Int](((o: Int) => s.+(o))))))(List.canBuildFrom[Int] )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment