Convert M [A => B] to => M [B]

Is there a utility in Scala or Scalaz to convert a container / collection of functions into a function that maps from the same input to the output values ​​of the collection? The signature will look something like this:

def transform[M[_], A, B](m: M[A => B]): A => M[B] = ???

Here's an example implementation for a List container:

def transform[A, B](fs: List[A => B]): A => List[B] = x =>
  fs.foldRight[List[B]](Nil) {
    (f, acc) => f(x) :: acc
  }

Ideally, this will work for any container of functions, including a tuple of functions, Option[Function1[A, B]]or even TupleN[Option[Function1[A, B]], ...].

EDIT:

I just realized that (at least for the special case of the list) the map function works:

    def transform[A, B](fs: List[A => B]): A => List[B] = x => fs map (_(x))

This can be generalized to everything that has a mapping function with appropriate semantics. What class is the appropriate class for this?

+4
source share
2

, M , mapply scalaz Functor :

def mapply[A, B](a: A)(f: F[A => B]): F[B] = map(f)((ff: A => B) => ff(a))

, :

def transform[M[_],A,B](m: M[A => B])(implicit f:Functor[M]):A => M[B] = f.mapply(_)(m)

: FunctorSyntax:

def transform[M[_]:Functor,A,B](m: M[A => B]):A => M[B] = _.mapply(m)
+5

Scalaz, Functor :

def transform[M[_]: Functor, A, B](fs: M[A => B]): A => M[B] = a => fs map (_(a))
0