scala - How to permute all combinations involving both lists, with each element preserved at their respective indices? -
i'd transform list - example:
val = list(x,y,z) val b = list(x1,y1,z1)
desired output
list(list(x1,y,z),list(x,y1,z),list(x,y,z1),list(x1,y1,z), list(x1,y,z1),list(x,y1,z1),list(x1,y1,z1))
the order preserved - hence built-in combinations function wouldn't useful. there concise way of doing scala?
def permut[a](l: list[(a,a)]): list[list[a]] = l match { case nil => list(list()) // or nil :: nil case (a,b) :: tail => val t = permut(tail) t.map(a :: _) ::: t.map(b :: _) } val = list("x0", "y0", "z0") val b = list("x1", "y1", "z1") scala> permut(a zip b) res22: list[list[string]] = list(list(x0, y0, z0), list(x0, y0, z1), list(x0, y1, z0), list(x0, y1, z1), list(x1 , y0, z0), list(x1, y0, z1), list(x1, y1, z0), list(x1, y1, z1))
Comments
Post a Comment