Reputation: 4461
I'm trying to write a generic cross
function that would yield the cartesian product of two iterables. Here is my attempt:
def cross[a, b, A <: Iterable[a], B <: Iterable[b]](a: A, b: B): Iterable[(a, b)] =
for (i <- a; j <- b) yield (i, j)
However, the signature is not quite right. I get:
Error:(147, 15) inferred type arguments [Nothing,Nothing,List[Int],Array[String]] do not conform to method cross's type parameter bounds [a,b,A <: Iterable[a],B <: Iterable[b]]
println(cross(List(1, 2, 3), Array("a", "b", "c")))
What is the correct way to write this signature, where I want to take 2 Iterable
on different types of elements?
Upvotes: 0
Views: 488
Reputation: 1868
def cross[A, B](a: Iterable[A], b: Iterable[B]): Iterable[(A, B)] =
for (i <- a; j <- b) yield (i, j)
Upvotes: 2