I have a bunch of factory functions that all take the same args and return a different type. Currently I invoke them all explicitly, but it's very verbose, and I want to genericize the factories into a HList
and invoke each by mapping the HList
.
case class Endpoint[A](a: A)
case class Factory[Ret](f: Int => Endpoint[Ret])
val factories = Factory[Int](a => Endpoint(a)) :: Factory[String](a => Endpoint(a.toString)) :: HNil
I have defined a Poly1
so that I can map my HList
and apply f
for each element
case class ApplyFactory(param: Int) extends Poly1 {
implicit def generic[A]: Case.Aux[Factory[A], Endpoint[A]] =
at((factory: Factory[A]) => factory.f(param))
}
val endpoints = factories.map(ApplyFactory(5))
The problem is that could not find implicit value for parameter mapper
. Changing ApplyFactory
to an object makes the code compile. How do I map over an HList
if the Poly
is defined as a class not an object? Or is there a better pattern for applying an HList
of functions with a given set of parameters and returning a new HList
with the result?