Valentin Tihomirov
Valentin Tihomirov

Reputation: 1

shapeless HList mapping

I try

import shapeless._
val ilist = List(1,2,3) ; 
val slist = List("a", "b", "c") ; 
(ilist :: slist :: HNil).map(list: List[_] => list -> list.length)

and compiler says that it needs argument type in the map function or it cannot find list value when type List[_] is provided. Is there a simple example of mapping?

There is no such problem with plain Scala lists

    val list1 = 1 :: 2 :: Nil; val list2 = 3 :: 4 :: Nil
    (list1 :: list2 :: Nil) map {list => list -> list.length}

compiles fine.

Upvotes: 1

Views: 407

Answers (1)

Archeg
Archeg

Reputation: 8462

I'm a no shapeless expert, but I'm sure you can do that with ~>>. The thing is that map accepts Poly, so you need a Poly function that returns a constant. This is what ~>> is.

  import shapeless._
  import poly._
  val ilist = List(1,2,3) ;
  val slist = List("a", "b", "c") ;

  object fun extends (List ~>> Int) {
    override def apply[T](f: List[T]): Int = f.size
  }

  println((ilist :: slist :: HNil).map(fun))

Note, that ~>> is actually:

type ~>>[F[_], R] = ~>[F, Const[R]#λ]

Upvotes: 2

Related Questions