Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to splitAt a scala list using shapeless

I'm trying to split a list of size S at N, where it is known that N, M sum up to S. This does not compile:

def splitIt[N <: Nat,
            M <: Nat,
            S <: Nat](u: Sized[List[Int], N] {type A = N},
                      v: Sized[List[Int], M] {type A = M},
                      t: Sized[List[Int], S] {type A = S})(implicit sum: SumAux[N, M, S]): Unit = {
  val z = t.splitAt[N]
}

Errors

No implicit view available from List[Int] => scala.collection.GenTraversableLike[S,List[Int]].

not enough arguments for method sizedOps: (implicit evidence$2: List[Int] => scala.collection.GenTraversableLike[S,List[Int]])shapeless.SizedOps[S,List[Int],S]. Unspecified value parameter evidence$2.

Final correct version

def splitIt[N <: Nat,
            M <: Nat, S <: Nat](u: Sized[List[Int], N] {type A = Int},
                                v: Sized[List[Int], M] {type A = Int},
                                t: Sized[List[Int], S] {type A = Int})(implicit sum: DiffAux[S, N, M], toInt: ToInt[N]): Unit = {
  val z = t.splitAt[N]
}
like image 622
Peteris Avatar asked Dec 24 '12 13:12

Peteris


1 Answers

The type A needs to be the type of the elements of the list, not the size argument again. That's why it's trying to convert to a GenTraversableLike[A, List[Int]]. You need to set A to Int in each case.

like image 131
Ptharien's Flame Avatar answered Oct 16 '22 07:10

Ptharien's Flame