Skip to content

Add Distributive typeclass #2014

Closed
@coltfred

Description

@coltfred

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions