Skip to content

Instantly share code, notes, and snippets.

@alexarchambault
Last active August 29, 2015 14:06
Show Gist options
  • Save alexarchambault/7532dcc14ba84b7974ec to your computer and use it in GitHub Desktop.
Save alexarchambault/7532dcc14ba84b7974ec to your computer and use it in GitHub Desktop.
object Obs {
import shapeless._, poly._
import shapeless.ops.tuple.Mapper
object values extends (Obs ~> Id) {
def apply[T](t: Obs[T]) = t.value
}
case class Calculate[O, T](observables: O, mapper: Mapper.Aux[O, values.type, T]) {
def withFun[Z](fun: T => Z): Z = fun(mapper(observables))
}
def calculate[O, T](observables: O)(implicit mapper: Mapper.Aux[O, values.type, T]) =
Calculate(observables, mapper)
}
object ObsExample {
import Obs._
val obs1: Obs[Double] = ???
val obs2: Obs[Option[Int]] = ???
val obs3: Obs[String] = ???
val obs4: Obs[List[Double]] = ???
calculate(obs1, obs2) withFun {
case (d, i) => d + i.getOrElse(0).toDouble
}
val f: (Option[Int], String, List[Double]) => String = ???
calculate(obs2, obs3, obs4) withFun f.tupled
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment