Skip to content

Instantly share code, notes, and snippets.

View djspiewak's full-sized avatar

Daniel Spiewak djspiewak

View GitHub Profile
package com.codecommit.scatter
import scodec._
import scalaz._
import scalaz.stream._
trait Server[Key, Value] {
type Transformer <: Server.Transformer[Value]
def optional[A](guard: Codec[Boolean], target: Codec[A]): Codec[Option[A]] =
either(guard, provide(()), target).xmap[Option[A]]({ _.toOption }, { _ map { \/-(_) } getOrElse -\/(()) })
def withDefault[A](opt: Codec[Option[A]], default: Codec[A]): Codec[A] = {
val paired = opt flatZip {
case Some(a) => provide(a)
case None => default
}
paired.xmap[A]({ _._2 }, { a => (Some(a), a) })
implicit class ListCodec[L <: HList](self: Codec[L]) {
def pmap[L2 <: HList](p: Poly)(implicit m: Mapper.Aux[p.type, L, L2], m2: Mapper.Aux[p.type, L2, L]): Codec[L2] =
self.xmap({ _ map p }, { _ map p })
}
val x: Nothing = 42
val y: Nothing = 7
(x + y + ("I am winning!": Nothing)): Nothing
def lookahead[A](delegate: Parser[A]): Parser[(A, LineStream)] = new TerminalParser {
def parse(in: LineStream): Result[(A, LineStream)] = delegate parse in match {
case Success(value, tail) => Success((value, in), tail)
case r => r
}
}
implicit object ParserApplicative extends Applicative[Parser] {
def point[A](a: A): Parser[A] = "" ^^^ a
def ap[A, B](left: => Parser[A])(right: => Parser[A => B]): Parser[B] =
left ~ right ^^ { (a, f) => f(a) }
}
implicit def parserSemigroup[A]: Semigroup[Parser[A]] = new Semigroup[Parser[A]] {
def append(left: Parser[A], right: => Parser[A]): Parser[A] = left | right
}
import annotation.unchecked.uncheckedVariance
import reflect.runtime.universe.TypeTag
package object stuff {
// required for the better ??? operator
type MyTypeTag[+A] = TypeTag[A @uncheckedVariance]
def ???[A](implicit tag: MyTypeTag[A]): A =
throw new NotImplementedError(s"unimplemented value of type ${tag.tpe}")
}
class AlmostFuture[+A] {
def flatMap[B](f: A => AlmostFuture[B]): AlmostFuture[B]
def map[B](f: A => B): AlmostFuture[B]
// like zip, but runs in parallel
def both[B](f: AlmostFuture[B]): AlmostFuture[(A, B)]
// meh
def run(implicit executor: Executor): A
import scalaz._
import concurrent._
// utility for shifting off to a different thread
def thread[A](body: => A)(cb: A => Unit): Unit = {
val t = new Thread {
setName("test-thread")
override def run(): Unit = cb(body)
}
import java.util.concurrent.ExecutorService
def fork[A](t: Task[A])(implicit pool: ExecutorService = Strategy.DefaultExecutorService): Task[A] = {
Task async { cb =>
t runAsync { either =>
pool.submit(new Runnable {
def run(): Unit = cb(either)
})
()