Question

I was looking at scalaz and seeing that most monads have the ability to be traversed List, Option, Identity etc and wondering if that means that every traversable object can also have a sequence - consider.

If that is in fact the case (I could indeed be wrong), then what is the output of a Identity for example:

Identity(Option(1)) would this become Option(Identity(1))? Or am I missing something, if that is the case any pointers would be extremely helpful.

Edit

Indeed I was correct about the identity sequence being the following:

Identity(Option(1)) does indeed become Option(Identity(1)). So that part of the question is settled.

So my question is refined to the original title - is every traversable monad sequencable?

Was it helpful?

Solution

sequence can be very straightforwardly defined as traversal with the identity function—i.e. in Haskell:

sequence = traverse id

Or in Scalaz 6 (from your second link above), the considerably more verbose:

def sequence[N[_], B](implicit
  a: A <:< N[B],
  t: Traverse[M],
  n: Applicative[N]
): N[M[B]] = traverse((z: A) => (z: N[B]))

So yes, any type with a traversable instance can be sequenced (and it doesn't even need to be a monad).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top