Skip to content

Instantly share code, notes, and snippets.

View Softsapiens's full-sized avatar

Daniel Pous Montardit Softsapiens

View GitHub Profile
@jroper
jroper / Router.scala
Created July 2, 2014 07:50
Simple Play routing DSL with string interpolation
import java.util.regex.Pattern
import play.core.Routes
import play.api.mvc._
object Router extends Routes {
def routes = {
// Static paths
case Route("GET", p"") => controllers.Application.index
case Route("GET", p"/items") => controllers.Items.list
import scala.language.experimental.macros
import scala.reflect.macros.whitebox.Context
sealed trait Trampoline[A] {
def flatMap[B](f: A => Trampoline[B]): Trampoline[B] = macro Trampoline.flatMapImpl[A,B]
}
case class Return[A](a: A) extends Trampoline[A]
case class Suspend[A](k: () => Trampoline[A]) extends Trampoline[A]
@kevinwright
kevinwright / scaladays2014.md
Last active November 16, 2024 17:40
Scaladays 2014 slides

As compiled by Kevin Wright a.k.a @thecoda

(executive producer of the movie, and I didn't even know it... clever huh?)

please, please, please - If you know of any slides/code/whatever not on here, then ping me on twitter or comment this Gist!

This gist will be updated as and when I find new information. So it's probably best not to fork it, or you'll miss the updates!

Monday June 16th

sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@runarorama
runarorama / gist:a8fab38e473fafa0921d
Last active April 13, 2021 22:28
Compositional application architecture with reasonably priced monads
sealed trait Interact[A]
case class Ask(prompt: String)
extends Interact[String]
case class Tell(msg: String)
extends Interact[Unit]
trait Monad[M[_]] {
def pure[A](a: A): M[A]
@milessabin
milessabin / gist:88a21b1f2c6ec37ab6a7
Last active August 29, 2015 14:02
Deriving path-dependent type equality from value identity (in turn proved by singleton type inhabitation). For context see: https://gist.github.com/copumpkin/cb26bb921bbb81183299
scala> trait Foo { type T; val t: T }
defined trait Foo
scala> val x = new Foo { type T = Int ; val t = 23 }
x: Foo{type T = Int} = $anon$1@5aeea69b
scala> val y = new Foo { type T = String ; val t = "foo" }
y: Foo{type T = String} = $anon$1@7d7902b8
scala> def compare(a: Foo, b: Foo): Option[b.T] = a match { case a2: b.type => Some(a2.t) ; case _ => None }
import scala.concurrent.{ ExecutionContext, Future }
import shapeless._, ops.tuple.{ IsComposite, LeftFolder, Prepend }
object zipAndAdd extends Poly2 {
implicit def only[B, A](implicit p: Prepend[B, Tuple1[A]], executor: ExecutionContext) =
at[Future[B], Future[A]] {
(fb, fa) => fb.zip(fa).map { case (b, a) => p(b, Tuple1(a)) }
}
}
@milessabin
milessabin / gist:aae285025a32fac0f5c1
Last active August 26, 2017 10:28
Trivial type safe heterogenous map using only dependent method types, singleton-typed String literal keys and implicits.
scala> trait Assoc[K] { type V ; val value: V }
defined trait Assoc
scala> def mkAssoc[V0](k: String, v: V0): Assoc[k.type] { type V = V0 } =
| new Assoc[k.type] { type V = V0 ; val value = v }
mkAssoc: [V0](k: String, v: V0)Assoc[k.type]{type V = V0}
scala> implicit def nameAssoc = mkAssoc("Name", "Mary")
nameAssoc: Assoc[String("Name")]{type V = String}
@senko
senko / maybe.py
Last active May 2, 2024 18:35
A Pythonic implementation of the Maybe monad
# maybe.py - a Pythonic implementation of the Maybe monad
# Copyright (C) 2014. Senko Rasic <senko.rasic@goodcode.io>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
scala> :paste
// Entering paste mode (ctrl-D to finish)
case class Address(street : String, city : String, postcode : String)
case class Person(name : String, age : Int, address : Address)
// Exiting paste mode, now interpreting.
defined class Address
defined class Person