Suppose I've got a list of functions List[A => B] and need a function that returns List[B] for a given value of type A:
def foo[A, B](fs: List[A => B]): A => List[B] = a => fs.map(_.apply(a))
Is there any simpler (maybe with cats) way to write List[A => B] => A => List[B] ?
As @Oleg points out, you can use Applicative to generate the function:
import cats.implicits._
def foo[A, B](fs: List[A => B]): A => List[B] = a => fs ap List(a)
Although I don't think it makes much of a difference in this particular case.
来源:https://stackoverflow.com/questions/45160406/simple-way-to-apply-a-list-of-functions-to-a-value