I would probably use the Monoid
instance for Tuple2
and the Foldable
for List
(which comes into play through the Traverse
instance ) to sum as a result of the list.
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: If you really want Seq
, here is what you need to have in the area in order to make it work:
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 } }