Created
November 9, 2012 05:46
-
-
Save dipakc/4043947 to your computer and use it in GitHub Desktop.
RefactoringOptListFlatten
This file contains hidden or 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
| object OptListFlatten { | |
| /////////////// | |
| val aList = List(Some(10), None, Some(20)) | |
| for { | |
| x <- aList | |
| if (x.isDefined) | |
| } yield { | |
| x.get | |
| } | |
| //------------- | |
| for { | |
| xOpt <- aList | |
| x <- xOpt | |
| } yield x | |
| //------------- | |
| aList filter (_.isDefined) map (_.get) | |
| //------------- | |
| aList.flatten | |
| //------------- | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment