HList-style operations on standard Scala tuples

shapeless allows standard Scala tuples to be manipulated in exactly the same ways as HLists

import syntax.std.tuple._

head

(23, "foo", true).head should be(res0)

tail

(23, "foo", true).tail should be(res0)

drop

(23, "foo", true).drop(2) should be(res0)

take

(23, "foo", true).take(2) should be(res0)

split

(23, "foo", true).split(1) should be(res0)

prepend

val l = 23 +: ("foo", true)
l should be(res0)

append

val l = (23, "foo") :+ true
l should be(res0)

concatenate

val l = (23, "foo") ++ (true, 2.0)
l should be(res0)

map

import poly._

object option extends (Id ~> Option) {
  def apply[T](t: T) = Option(t)
}
val l = (23, "foo", true) map option
l should be(res0)

flatMap

val l = ((23, "foo"), (), (true, 2.0)) flatMap identity
l should be(res0)

fold

object size extends Poly1 {
  implicit def caseInt = at[Int](x => 1)
  implicit def caseString = at[String](_.length)
  implicit def caseTuple[T, U](implicit st: Case.Aux[T, Int], su: Case.Aux[U, Int]) =
    at[(T, U)](t => size(t._1) + size(t._2))
}

object addSize extends Poly2 {
  implicit def default[T](implicit st: size.Case.Aux[T, Int]) =
    at[Int, T] { (acc, t) => acc + size(t) }
}
(23, "foo", (13, "wibble")).foldLeft(0)(addSize) should be(res0)

conversion to HList

(23, "foo", true).productElements should be(res0)

conversion to List

(23, "foo", true).toList should be(res0)

zipper

import syntax.zipper._
val l = (23, ("foo", true), 2.0).toZipper.right.down.put("bar").root.reify
l should be(res0)