Is there a Scala Library method that performs the conversion Seq[Option[T]] -> Option[Seq[T]]
?
The Haskell equivalent would be sequence :: Monad m => [m a] -> m [a]
.
Is there a Scala Library method that performs the conversion Seq[Option[T]] -> Option[Seq[T]]
?
The Haskell equivalent would be sequence :: Monad m => [m a] -> m [a]
.
This is unfortunately not available in the standard library (although there is a Future.sequence
, as pedrofurla points out above). Part of the reason for this is probably just that the Scala standard library doesn't have any idea about applicative functors (or even monads, really).
As pedrofurla also mentions above, Scalaz does provide sequence
, and it's actually a lot more appropriately typed than Haskell's—instead of requiring something monadic inside a list as input, it accepts anything with an applicative functor instance inside something with a traversable instance (i.e., it's equivalent to Data.Traversable
's sequenceA
in Haskell, not the sequence
in the Prelude).