Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.81 KB | None | 0 0
  1.   import shapeless._
  2.   import syntax.sized._
  3.  
  4.   implicit def _0SizedToHList[T](sz: Sized[IndexedSeq[T], Nat._0]) = new {
  5.     def toHList = {
  6.       val IndexedSeq(a1) = sz.unsized
  7.       a1 :: HNil
  8.     }
  9.   }
  10.  
  11.   implicit def _1SizedToHList[T](sz: Sized[IndexedSeq[T], Nat._1]) = new {
  12.     def toHList = {
  13.       val IndexedSeq(a1) = sz.unsized
  14.       a1 :: HNil
  15.     }
  16.   }
  17.  
  18.   implicit def _2SizedToHList[T](sz: Sized[IndexedSeq[T], Nat._2]) = new {
  19.     def toHList = {
  20.       val IndexedSeq(a1, a2) = sz.unsized
  21.       a1 :: a2 :: HNil
  22.     }
  23.   }
  24.  
  25.   implicit def _3SizedToHList[T](sz: Sized[IndexedSeq[T], Nat._3]) = new {
  26.     def toHList = {
  27.       val IndexedSeq(a1, a2, a3) = sz.unsized
  28.       a1 :: a2 :: a3 :: HNil
  29.     }
  30.   }
  31.  
  32.   implicit def _4SizedToHList[T](sz: Sized[IndexedSeq[T], Nat._4]) = new {
  33.     def toHList = {
  34.       val IndexedSeq(a1, a2, a3, a4) = sz.unsized
  35.       a1 :: a2 :: a3 :: a4 :: HNil
  36.     }
  37.   }
  38.  
  39.   def readAttributes[N <: Nat](node: xml.Node, attrs: Sized[IndexedSeq[String], N])
  40.                               (implicit toInt : shapeless.ops.nat.ToInt[N]): Option[Sized[IndexedSeq[String], N]] = {
  41.     attrs.map(node.attribute).collect({case Some(s) => s.mkString}).sized[N]
  42.   }
  43.  
  44.   val node = <Node title = "Title" author = "Author" price = "50"></Node>
  45.  
  46.   val funcs3 = ((s: String) => s.length > 0,
  47.                 (s: String) => s.toUpperCase,
  48.                 (s: String) => s.toInt)
  49.   val a3 = readAttributes(node, Sized("title", "author", "price"))
  50.   println(a3.map(args => HList(funcs3) zipApply args.toHList tupled)) // Some((true,AUTHOR,50))
  51.  
  52.   val funcs2 = ((s: String) => s.length > 0,
  53.                 (s: String) => s.toInt)
  54.   val a2 = readAttributes(node, Sized("title", "price"))
  55.   println(a2.map(args => HList(funcs2) zipApply args.toHList tupled))  // Some((true,50))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement