Skip to content

Instantly share code, notes, and snippets.

View milessabin's full-sized avatar

Miles Sabin milessabin

View GitHub Profile
scala> class Foo
defined class Foo
scala> val foo = new Foo
foo: Foo = Foo@6df280a7
scala> foo: foo.type
res0: foo.type = Foo@6df280a7
import shapeless._
import shapeless.ops.hlist.Tupler
trait Evaluator[L <: HList] {
type Out <: HList
def apply(value:String, l:L):Option[Out]
}
object Evaluator {
@milessabin
milessabin / gist:25b7b669b5a9ac051a71
Created June 5, 2015 14:32
A safe ADT+shapeless drop-in replacement for the unsafe standard Scala Enumeration ...
// An ADT+shapeless as a drop-in replacement for a standard Scala Enumeration.
//
// First the unsafe standard Scala Enumeration ...
//
object ScalaEnumDemo extends App {
// Example from scala.Enumeration scaladoc. Terse ...
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
import shapeless._
object Values {
implicit def conv[T](self: this.type)(implicit v: MkValues[T]): Set[T] = Values[T]
def apply[T](implicit v: MkValues[T]): Set[T] = v.values.toSet
trait MkValues[T] {
def values: List[T]
}
@milessabin
milessabin / gist:b16e0765e1acdc90fd29
Last active August 16, 2016 16:25
Functional update of common fields of an open family of case classes via a case-class-like copy through the common super type ...
import shapeless._
/**
* Functional update of common fields of an *open* family of case classes
* via a case-class-like copy through the common super type ...
*
* This is a follow up to my earlier post showin how to do functional
* update for a sealed family of case classes,
*
* https://gist.github.com/milessabin/a212d946aef33811fee1
@milessabin
milessabin / gist:c3c9fbc57b1d913c2ee4
Last active December 2, 2020 11:37
Merging arbitrary case classes via shapeless ... (LabelledGeneric and record merge).
import shapeless._
object CaseClassMergeDemo extends App {
import mergeSyntax._
case class Foo(i: Int, s: String, b: Boolean)
case class Bar(b: Boolean, s: String)
val foo = Foo(23, "foo", true)
val bar = Bar(false, "bar")
@milessabin
milessabin / gist:a212d946aef33811fee1
Last active August 29, 2015 14:20
Functional update of common fields of a sealed family of case classes via a case-class-like copy through the common super type ...
import shapeless._
/**
* Functional update of common fields of a sealed family of case classes
* via a case-class-like copy through the common super type ...
*/
object BaseCopyDemo extends App {
import copySyntax._
// Sealed family of case classes ...
trait Pet {
type This <: Pet
def name: String
def renamed(newName: String): This
}
case class Fish(name: String, age: Int) extends Pet {
type This = Fish
def renamed(newName: String): This = copy(name = newName)
}
@milessabin
milessabin / gist:8f4498c057da9e376014
Created April 30, 2015 09:22
Live coding (mainly type class derivation) during the shapeless workshop at flatMap Oslo 2015
package shapeless.examples
import shapeless._
import poly._
import labelled._
sealed trait Message
case class Ping(id: Long, sender: String) extends Message
case class Pong(id: Long, receiver: String) extends Message
package fommil.polys
import shapeless._
sealed trait Trait
case class Foo() extends Trait
case class Bar() extends Trait
trait Thing[T] { def thingy: String }