Skip to content

Instantly share code, notes, and snippets.

View jedws's full-sized avatar

Jed Wesley-Smith jedws

View GitHub Profile
@jedws
jedws / IOActor.scala
Last active August 29, 2015 13:56
An Actor equivalent of SafeApp
package stuff
import akka.actor.{ Actor, actorRef2Scala }
import kadai.log.Logging
import spray.http.{ HttpFailure, HttpResponse }
import spray.http.HttpEntity.apply
trait IOActor extends Actor {
log: Logging =>
@jedws
jedws / ToFrom.scala
Last active August 29, 2015 13:56
attempts to use path dependent types to do automatic type mapping
object To {
trait Bijection[A, B] {
def to: A => B
def from: B => A
}
sealed trait Translate[A] {
type T
def apply(a: A): T
}
package test
// Type definitions
object Types {
type Tag = String
}
import Types._
// Marker definitions
case class OperatingHours(hours: String)
import scala.runtime.AbstractFunction2
class Foo[A, B, C]
extends AbstractFunction2[String, Int, Int] with ((String, Int) => Int) {
def apply(a: String, i: Int) = 3
}
@jedws
jedws / scalacbug.scala
Created June 10, 2014 03:08
Pattern matching on a case object is affected by whether the outer trait is parameterised or not
trait A {
sealed trait Foo
case object Bar extends Foo
case class Baz() extends Foo
def isBar(f: Foo) = f match {
case Bar => true
case Baz() => false
}
}
@jedws
jedws / LiftNaturalTransform.scala
Created October 9, 2014 21:45
lift a NaturalTransform into a type constructor that has a functor
import scalaz._
import scalaz.syntax.functor._
object LiftNaturalTransform {
def transform[F[_], G[_], M[_]: Functor, A](implicit nt: F ~> G): M[F[A]] => M[G[A]] =
_ map nt.apply
implicit class NaturalTransformSyntax[F[_], G[_]](f2g: F ~> G) {
def lift[M[_]: Functor] =
@jedws
jedws / Memoize.scala
Created December 10, 2014 05:18
A memoizer that memoizes partial thunks, and caches values only if found (unsafe)
import kadai.concurrent.Atomic
class Memoize[A](thunk: => Option[A]) {
val atom: Atomic[Ref] = Atomic(Empty)
def get: A =
atom.get match {
case Value(a) => a
case exec @ Executing() => exec.await match {
case Some(a) => if (atom.get == exec && atom.compareAndSet(exec, Value(a))) a else get
@jedws
jedws / typeclasses.scala
Last active August 29, 2015 14:15
non-subtyped typeclass relations idea
//
// each TC companion declares a Has[F] that expresses a relation to that TC
// impls obvs. have themselves as Has relations
//
// pro: no subtyping between TCs as the TC relations are encoded in the HasX subtypes
// pro: survives multiple relations
// cons: boilerplate for TC authors, and a little tricky to get right
// cons: impl needs to impl the full laundry list (can have template impls that do this eg: Monad.Template
trait Functor[F[_]] extends Functor.Has[F] {
sealed trait Heading {
val turn = Turn(this) _
}
case object N extends Heading
case object S extends Heading
case object E extends Heading
case object W extends Heading
object Turn {
def apply(h: Heading)(t: Turn) = t(h)
val nonIgnoredFields = {
@annotation.tailrec
def loop(k: Class[_], fields: List[Array[Field]]): List[Array[Field]] = {
if (k == null) fields
else
loop(k.getSuperclass, k.getDeclaredFields.filterNot(ignoreField _).reverse :: fields)
}
loop(klass, Nil).flatten
}