Created
October 1, 2011 13:24
-
-
Save micrypt/1256034 to your computer and use it in GitHub Desktop.
Nifty tricks
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
| val l = Seq(Seq(1,2), Seq(3,4), Seq(5,6)) | |
| val l2 = l.map(_.map(Seq(_))) | |
| l2.reduceLeft((xs, ys) => for {x <- xs; y <- ys} yield x ++ y) |
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
| val items = List("a","b","c","d") | |
| items flatMap((item) => for { l <- item; n <- items.filter(_ != item) } yield (l,n)) | |
| // List[(java.lang.String, java.lang.String)] = List((a,b), (a,c), (a,d), (b,a), (b,c), (b,d), (c,a), (c,b), (c,d), (d,a), (d,b), (d,c)) | |
| items flatMap { item => items.filter(_ != item) map { (item,_) } } | |
| // List[(java.lang.String, java.lang.String)] = List((a,b), (a,c), (a,d), (b,a), (b,c), (b,d), (c,a), (c,b), (c,d), (d,a), (d,b), (d,c)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment