Skip to content

Instantly share code, notes, and snippets.

@milessabin
Forked from ceedubs/TableExample.scala
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milessabin/cefecdb4a0fe63d07a0d to your computer and use it in GitHub Desktop.
Save milessabin/cefecdb4a0fe63d07a0d to your computer and use it in GitHub Desktop.
package shapeless.examples
import shapeless._
import nat._
import ops.hlist._
import test._
object TableExample extends App {
final case class Row[L <: HList](cells: L)
final case class Table[L <: HList](rows: L) {
def row[RR](rowNumber: Nat)(implicit atR: At.Aux[L, rowNumber.N, RR]): RR =
atR(rows)
}
val test = Table(
Row(1 :: 2 :: 3 :: HNil) ::
Row(4 :: 5 :: 6 :: HNil) ::
Row(7 :: 8 :: 9 :: HNil) ::
HNil)
type Int3Row = Row[Int :: Int :: Int :: HNil]
val r0 = test.row(0) // Row(1 :: 2 :: 3 :: HNil)
typed[Int3Row](r0)
assert(Row(1 :: 2 :: 3 :: HNil) == r0)
val r1 = test.row(1) // Row(4 :: 5 :: 6 :: HNil)
typed[Int3Row](r1)
assert(Row(4 :: 5 :: 6 :: HNil) == r1)
val r2 = test.row(2) // Row(7 :: 8 :: 9 :: HNil)
typed[Int3Row](r2)
assert(Row(7 :: 8 :: 9 :: HNil) == r2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment