Cartesian product of three lists with cats
Asked Answered
P

1

6

Suppose I need to zip three lists to get a list of triplets. I can write it like that:

import cats._
import cats.data._
import cats.implicits._

(List(1, 2) |@| List(3, 4) |@| List(5, 6)) map {case (a, b, c) => (a, b, c)}
res1: List[(Int, Int, Int)] = List((1,3,5), (1,3,6), (1,4,5), (1,4,6), (2,3,5), (2,3,6), (2,4,5), (2,4,6))

Can you simplify it ?

Perimeter answered 13/6, 2018 at 16:45 Comment(0)
E
7

With 1.1.0, it's simply (a, b, c).tupled. Ammonite script with all the imports and dependencies:

@ import $ivy.`org.typelevel::cats-core:1.1.0`
@ import cats._, cats.data._, cats.implicits._

val triples = (List(1, 2), List(3, 4), List(5, 6)).tupled
println(triples)

Output:

List((1,3,5), (1,3,6), (1,4,5), (1,4,6), (2,3,5), (2,3,6), (2,4,5), (2,4,6))

I wouldn't call it a "zip", however, it's rather a Cartesian product.

Edinburgh answered 13/6, 2018 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.