Converting a tuple of options to an option of tuple with Scalaz or Shapeless

Nikita Volkov picture Nikita Volkov · Sep 13, 2012 · Viewed 7.7k times · Source

Having

(Some(1), Some(2))

I expect to get

Some((1, 2))

and having

(Some(1), None)

I expect to get

None

Answer

Rex Kerr picture Rex Kerr · Sep 13, 2012

I realize you're asking about Scalaz, but it's worth pointing out that the standard method is not unbearably wordy:

val x = (Some(1), Some(2))

for (a <- x._1; b <-x._2) yield (a,b)

In the general case (e.g. arbitrary-arity tuples), Shapeless is best at this sort of thing.