This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 }) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| val x: Nothing = 42 | |
| val y: Nothing = 7 | |
| (x + y + ("I am winning!": Nothing)): Nothing |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.rr.experiment | |
| import org.specs2.ScalaCheck | |
| import org.specs2.mutable._ | |
| import org.scalacheck._ | |
| import scalaz._ | |
| import scodec._ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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}") | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| }) | |
| () |