Skip to content

Instantly share code, notes, and snippets.

View jedws's full-sized avatar

Jed Wesley-Smith jedws

View GitHub Profile
package com.atlassian.crowd.plugin.usermanagement.rest.controller;
import com.atlassian.fugue.Option;
import static com.atlassian.crowd.plugin.usermanagement.rest.controller.Visibility.Internal;
import static com.atlassian.fugue.Option.some;
class Main {
public static void main(String[] args) {
CreateComment comment1 = CreateComment.build(
@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] {
@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 / 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 / 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
}
}
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
}
package test
// Type definitions
object Types {
type Tag = String
}
import Types._
// Marker definitions
case class OperatingHours(hours: String)
@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
}
@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 / Encoding.scala
Last active December 29, 2015 05:29
the two macro implementations are almost identical, but all attempts to share implementation are frustrated by obscure runtime problems
import reflect.macros.Context
import scalaz.{ -\/ , \/-, syntax }
import syntax.id._
import util.control.NonFatal
object Encoders {
trait Base {
def instance: String => Result[BigInt]
}