Let the type system do it for you

Let the type system do it for you

Presentation at Functional Media, ITV, London.
http://www.meetup.com/Functional-Media/events/137916712/

Ff619670d30ebdeefd49cf10af8e3292?s=128

Richard Dallaway

September 24, 2013
Tweet

Transcript

  1. Richard Dallaway @d6y www.underscoreconsulting.com Let the type system do it

    for you
  2. csv( List("Date","Ratio X"), List("Mon","6") :: List("Tue","-2") :: Nil ) Date

    Ratio X Mon 6 Tue -2
  3. csv( List("Date" ), List("Mon","6") :: List("Tue","-2") :: Nil ) Oops

    and so… ? Date Mon 6 Tue -2
  4. def csv( hdrs: Seq[String], rows: List[Seq[String]]) : String = ...

  5. def csv( hdrs: Seq[String], rows: List[Seq[String]]) : String = ...

    import shapeless._ import syntax.sized._
  6. def csv[N <: Nat]( hdrs: Seq[String], rows: List[Seq[String]]) : String

    = ... import shapeless._ import syntax.sized._
  7. def csv[N <: Nat]( hdrs: Sized[Seq[String], N], rows: List[Seq[String]]) :

    String = ... import shapeless._ import syntax.sized._
  8. def csv[N <: Nat]( hdrs: Sized[Seq[String], N], rows: List[Sized[Seq[String], N]])

    : String = ... import shapeless._ import syntax.sized._
  9. csv( Sized("Date","Ratio X"), Sized("Mon","6") :: Sized("Tue","-2") :: Nil ) Date

    Ratio X Mon 6 Tue -2
  10. csv( Sized("Date" ), Sized("Mon","6") :: Sized("Tue","-2") :: Nil ) Type

    mismatch at compile time found: Sized[IndexedSeq[String],shapeless.nat._1] required: Sized[Seq[String],Succ[_ >: shapeless.nat._1 ...] Oops
  11. class SizedOps[A, Repr, N <: Nat] ... { def head(implicit

    ev : _0 < N) : A = r.head def tail(implicit pred : Pred[N]) = wrap[Repr, pred.Out](r.tail) ... }
  12. @d6y www.underscoreconsulting.com Less code Clearer intent Summary