Scala “

To augment Dave’s answer, here is a translation schema for ‘for-comprehensions’ from Scala language specification: A comprehension for (enums) yield e evaluates expression e for each binding generated by the enumerators enums. An enumerator sequence always starts with a generator; this can be followed by further generators, value definitions, or guards. A generator p <- … Read more

Cartesian product of two lists

The following suggestion is not using a for-comprehension. But I don’t think it’s a good idea after all, because as you noticed you’d be tied to a certain length of your cartesian product. scala> def cartesianProduct[T](xss: List[List[T]]): List[List[T]] = xss match { | case Nil => List(Nil) | case h :: t => for(xh <- … Read more

Composing Option with List in for-comprehension gives type mismatch depending on order

For comprehensions are converted into calls to the map or flatMap method. For example this one: for(x <- List(1) ; y <- List(1,2,3)) yield (x,y) becomes that: List(1).flatMap(x => List(1,2,3).map(y => (x,y))) Therefore, the first loop value (in this case, List(1)) will receive the flatMap method call. Since flatMap on a List returns another List, … Read more