Closed
Description
There is a typeclass call Distributive
that is the dual of Traverse
trait Distributive[F[_]] extends Functor[F] {
def distributel[G[_]:Functor,A,B](fa: G[A])(f: A => F[B]): F[G[B]]
}
The feature that's really handy with this encoding is that it's stack safe to distribute over a Functor
of arbitrary "size". For instance:
scala> val f = {x:Int => x +1}
f: Int => Int = $$Lambda$1271/1964149457@6dbabd33
scala> val funcs = List.fill(10000)(f)
funcs: List[Int => Int] = List($$Lambda$1271/1964149457@6dbabd33, $$Lambda$1271/1964149457@6dbabd33, $$Lamb...)
scala> funcs.distribute(identity).apply(1)
res15: List[Int] = List(2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 ...)
scala> funcs.sequence.apply(1) //Stack overflow
Would this typeclass be welcome in cats?
Metadata
Metadata
Assignees
Labels
No labels