1.The sequence and traverse combinators函数的签名
def sequence[A](lma: List[M[A]]): M[List[A]]
def traverse[A,B](la: List[A])(f: A => M[B]): M[List[B]]
实现源码:FP IN Scala
def sequence[A](lma: List[F[A]]): F[List[A]] =
lma.foldRight(unit(List[A]()))((ma, mla) => map2(ma, mla)(_ :: _))
def traverse[A,B](la: List[A])(f: A => F[B]): F[List[B]] =
la.foldRight(unit(List[B]()))((a, mlb) => map2(f(a), mlb)(_ :: _))
我们上一次已经说过了Monads的trait其中的unit,所以在这里返回结果是具M类型构造器的类型不难理解
2.replicateM combinators
def replicateM[A](n: Int, ma: M[A]): M[List[A]]
实现源码:FP IN Scala
def replicateM[A](n: Int, ma: F[A]): F[List[A]] =
sequence(List.fill(n)(ma))
3. factor combinators
def factor[A,B](ma: M[A], mb: M[B]): M[(A, B)] = map2(ma, mb)((_, _))
4. cofactor combinators
def cofactor[A,B](e: Either[M[A], M[B]]): M[Either[A, B]]