Map for general HList

Say we have the following method

def func[T <: HList](hlist: T, poly: Poly)
    (implicit mapper : Mapper[poly.type, T]): Unit = {
    hlist map poly 
}

and custom poly

object f extends (Set ~>> String) {
    def apply[T](s : Set[T]) = s.head.toString
}

Therefore, I can use this funcas

func(Set(1, 2) :: Set(3, 4) :: HNil, f)

In my code, I have a small number of policies and a large number of funcinvocations. To do this, I tried moving poly: Polyto implicit parameters and received the expected message

illegal dependent method type: parameter appears in the type of another parameter in the same section or an earlier one

How can I change or expand a parameter poly: Polyto avoid this error (I need to save the type signature func[T <: HList](...))?

0
source share
1 answer

Perhaps you can use the "partially applied" trick using a class with a method apply:

import shapeless._
import ops.hlist.Mapper

final class PartFunc[P <: Poly](val poly: P) {
  def apply[L <: HList](l: L)(implicit mapper: Mapper[poly.type, L]): mapper.Out =
    l map poly
}

def func[P <: Poly](poly: P) = new PartFunc(poly)

With your poly f:

val ff = func(f)
ff(Set(1, 2) :: Set(3, 4) :: HNil)          // 1 :: 3 :: HNil
ff(Set("a", "b") :: Set("c", "d") :: HNil)  // a :: c :: HNil
+1
source

All Articles