Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added mapAccumL and mapAccumM operations #3797

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions core/src/main/scala/cats/Traverse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ import scala.annotation.implicitNotFound

override def unorderedSequence[G[_]: CommutativeApplicative, A](fga: F[G[A]]): G[F[A]] =
sequence(fga)

/**
* a combination of map and scanLeft
* it applies a function to each element of a structure,
* passing an accumulating parameter from left to right,
* and returning a final value of this accumulator together with the new structure.
*/
def mapAccumL[A, B, C](fa: F[A], start: B)(step: (B, A) => (B, C)): (B, F[C]) =
traverse(fa)(a => State((b: B) => step(b, a))).run(start).value

/**
* like [[mapAccumL]] but also combines monadic effects of `G`
* stack-safety relies on a stack safety of G
*/
def mapAccumM[G[_]: Monad, A, B, C](fa: F[A], start: B)(step: (B, A) => G[(B, C)]): G[(B, F[C])] =
traverse(fa)(a => StateT((b: B) => step(b, a))).run(start)
}

object Traverse {
Expand All @@ -166,12 +182,11 @@ object Traverse {
object ops {
implicit def toAllTraverseOps[F[_], A](target: F[A])(implicit tc: Traverse[F]): AllOps[F, A] {
type TypeClassType = Traverse[F]
} =
new AllOps[F, A] {
type TypeClassType = Traverse[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
} = new AllOps[F, A] {
type TypeClassType = Traverse[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
trait Ops[F[_], A] extends Serializable {
type TypeClassType <: Traverse[F]
Expand All @@ -191,6 +206,10 @@ object Traverse {
def traverseWithIndexM[G[_], B](f: (A, Int) => G[B])(implicit G: Monad[G]): G[F[B]] =
typeClassInstance.traverseWithIndexM[G, A, B](self)(f)(G)
def zipWithIndex: F[(A, Int)] = typeClassInstance.zipWithIndex[A](self)
def mapAccumL[B, C](start: B)(step: (B, A) => (B, C)): (B, F[C]) =
typeClassInstance.mapAccumL[A, B, C](self, start)(step)
def mapAccumM[G[_], B, C](start: B)(step: (B, A) => G[(B, C)])(implicit ev$1: Monad[G]): G[(B, F[C])] =
typeClassInstance.mapAccumM[G, A, B, C](self, start)(step)
}
trait AllOps[F[_], A]
extends Ops[F, A]
Expand All @@ -202,12 +221,11 @@ object Traverse {
trait ToTraverseOps extends Serializable {
implicit def toTraverseOps[F[_], A](target: F[A])(implicit tc: Traverse[F]): Ops[F, A] {
type TypeClassType = Traverse[F]
} =
new Ops[F, A] {
type TypeClassType = Traverse[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
} = new Ops[F, A] {
type TypeClassType = Traverse[F]
val self: F[A] = target
val typeClassInstance: TypeClassType = tc
}
}
@deprecated("Use cats.syntax object imports", "2.2.0")
object nonInheritedOps extends ToTraverseOps
Expand Down
12 changes: 12 additions & 0 deletions tests/src/test/scala/cats/tests/TraverseSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ abstract class TraverseSuite[F[_]: Traverse](name: String)(implicit ArbFInt: Arb
}
}

test(s"Traverse[$name].mapAccumL") {
forAll { (fa: F[Int], start: Long, fn: (Long, Int) => (Long, Int)) =>
val (act, fb) = fa.mapAccumL(start)(fn)
val (exp, xs) = fa.toList.foldLeft((start, List.empty[Int])) { case ((prev, acc), a) =>
val (next, b) = fn(prev, a)
(next, b :: acc)
}
assert(act === exp)
assert(fb.toList === xs.reverse)
}
}

test(s"Traverse[$name].traverse matches traverse_ with Option") {
forAll { (fa: F[Int], fn: Int => Option[Int]) =>
assert(Applicative[Option].void(fa.traverse(fn)) == fa.traverse_(fn))
Expand Down