Created
November 7, 2012 17:39
-
-
Save delexi/4033145 to your computer and use it in GitHub Desktop.
for expression and apply syntactic sugar
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 = List("ab", "cd", "ef") | |
| // Compiliert | |
| (for(x<-l; if x.indexOf('c') >= 0) yield (l.indexOf(x), x.indexOf('c'))).apply(0) | |
| // Compiliert nicht: | |
| // <console>:9: error: type mismatch; | |
| // found : Int(0) | |
| // required: scala.collection.generic.CanBuildFrom[List[java.lang.String],(Int, Int),?] | |
| // (for(x<-l; if x.indexOf('c') >= 0) yield (l.indexOf(x), x.indexOf('c')))(0) | |
| (for(x<-l; if x.indexOf('c') >= 0) yield (l.indexOf(x), x.indexOf('c')))(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not work because the for-expression is translated to a combination of higher order functions (map, flatMap, filterWith). All of these methods have a second parameter list expecting a CanBuildFrom and by writing
xs.map(f)(0)this second parameter list is called instead of apply.