Skip to content

Instantly share code, notes, and snippets.

object Ok {
trait Foo[A]
case class Bar(x: Int)
object Bar {
implicit class BarOps(bar: Foo[Bar]) {
val ok = "ok"
}
}
def usage(bar: Foo[Bar]) = bar.ok // Ok, the implicit conversion is found in the Bar companion object
/**
* data (f :+: g) a = Inl (f a) | Inr (g a)
*/
sealed trait Coproduct[F[+_], G[+_], A]
case class Inl[F[+_], G[+_], A](a: F[A]) extends Coproduct[F, G, A]
case class Inr[F[+_], G[+_], A](a: G[A]) extends Coproduct[F, G, A]
trait :+:[F[+_], G[+_]] {
type Apply[A] = Coproduct[F, G, A]
}
@julienrf
julienrf / Functor.scala
Created September 12, 2012 13:50
A bottom up approach to category theory: Functors (2)
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
@julienrf
julienrf / Contact.scala
Created September 12, 2012 12:23
A bottom up approach to category theory: Functors
case class Contact(name: String, email: String)
@julienrf
julienrf / Application.scala
Created April 26, 2012 20:47
Write a Mapping[A] and get a QueryStringBindable[A] for free
// --- Definition of a query string binder for type A using a Mapping[A]
object Binders {
implicit def mappingBinder[A](implicit mapping: Mapping[A]) = new QueryStringBindable[A] {
override def bind(key: String, params: Map[String, Seq[String]]): Option[Either[String, A]] = {
val data = for {
(k, ps) <- params
if k startsWith key
p <- ps.headOption
} yield (k.drop(key.length + 1), p)
scala> trait Foo[+A]
defined trait Foo
scala> implicit object intFoo extends Foo[Int]
defined module intFoo
scala> implicit def traversableFoo[F[_], A](implicit bf: collection.generic.CanBuildFrom[F[_], A, F[A]], aFoo: Foo[A]) = new Foo[F[A]] {}
traversableFoo: [F[_], A](implicit bf: scala.collection.generic.CanBuildFrom[F[_],A,F[A]], implicit aFoo: Foo[A])java.lang.Object with Foo[F[A]]
scala> implicitly[Foo[List[Int]]]
object Application extends Controller with PathLang {
def index = Ok(html.index())
}
@julienrf
julienrf / Action.scala
Created April 9, 2012 16:20
How to implement a custom PathBindable with Play 2
def show(article: Article) = Action {
Ok(views.html.article(article))
}
@julienrf
julienrf / Application.scala
Last active October 1, 2015 04:18
Composite UI without boilerplate using Play 2
object Application extends Controller {
val index = Action { implicit request =>
Ok(views.html.index())
}
val show = Action { implicit request =>
Ok(views.html.show("42"))
}
@julienrf
julienrf / reader.hs
Created December 8, 2011 21:00 — forked from paul-r-ml/reader-applicative-monadic.rb
simple reader monad
-- Un type qui sera une instance de Monad. Il s'agit d'un simple
-- conteneur pour les fonctions d'un environnement vers un résultat.
newtype Reader env res = Reader (env -> res)
-- Une fonction qui prend un Reader et un environnement, et qui
-- renvoit le résultat
runReader :: Reader env res -> env -> res
runReader (Reader f) e = f e
-- La fameuse instance de Monad. return ignore son environnement,