我可能会使用Monoid
实例forTuple2
和Foldable
实例 for List
(通过它的instance发挥作用)来对结果列表求和。Traverse
scala> import scalaz._, Scalaz._
import scalaz._
import Scalaz._
scala> val a = List(11,222,3333,44444,555555)
a: List[Int] = List(11, 222, 3333, 44444, 555555)
scala> def f(a: Int): (List[String], List[Int]) = (a.toString.tails.toList, a.toString.tails.toList.map(_.size))
f: (a: Int)(List[String], List[Int])
scala> val mapped = a map f
mapped: List[(List[String], List[Int])] = List((List(11, 1, ""),List(2, 1, 0)), (List(222, 22, 2, ""),List(3, 2, 1, 0)), (List(3333, 333, 33, 3, ""),List(4, 3, 2, 1, 0)), (List(44444, 4444, 444, 44, 4, ""),List(5, 4, 3, 2, 1, 0)), (List(555555, 55555, 5555, 555, 55, 5, ""),List(6, 5, 4, 3, 2, 1, 0)))
scala> mapped.suml
res1: (List[String], List[Int]) = (List(11, 1, "", 222, 22, 2, "", 3333, 333, 33, 3, "", 44444, 4444, 444, 44, 4, "", 555555, 55555, 5555, 555, 55, 5, ""),List(2, 1, 0, 3, 2, 1, 0, 4, 3, 2, 1, 0, 5, 4, 3, 2, 1, 0, 6, 5, 4, 3, 2, 1, 0))
UPD:如果你真的想要Seq
,这里是你需要在范围内让它工作的东西:
implicit val isoTraverse: IsomorphismTraverse[Seq, List] = new IsomorphismTraverse[Seq, List] {
def G = Traverse[List]
def iso = new IsoFunctorTemplate[Seq, List] {
def to[A](sa: Seq[A]): List[A] = sa.toList
def from[A](la: List[A]): Seq[A] = la.toSeq
}
}