2
votes

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] ?

1
You can do a => fs ap List(a) with cats or just write fs.map(_(a)), but I doubt it is really making it simpler. - Oleg Pyzhcov
I concur it maybe does not make it simple but it's good to know anyway ! So, thank you, Oleg. - Michael

1 Answers

2
votes

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.