This file contains 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
object ArgonautExamples { | |
import argonaut._, Argonaut._ | |
// 1. ------------------------------------------------------------------------ | |
// | |
// def parseThrak(json: JsValue): Seq[Thrak] = json match { | |
// case JsString(Regex1(x)) => Seq(SimpleText(x)) | |
// case JsString(Regex2(x)) => Seq(ComplexText(x)) | |
// case o @ JsObject(_) if o.containsKeys("k1") => Seq(Lov(o)) | |
// case o @ JsObject(_) if o.containsKeys("k2") => Seq(Uom(o)) |
This file contains 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
trait Default[T] { | |
@inline | |
def default: T | |
} | |
object Default { | |
implicit val intDefault: Default[Int] = apply(0) | |
implicit val byteDefault: Default[Byte] = apply(0) | |
implicit val shortDefault: Default[Short] = apply(0) | |
implicit val longDefault: Default[Long] = apply(0L) |
This file contains 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
Module VersionA. | |
Inductive bool : Type := | |
| true : bool | |
| false : bool. | |
Definition neg (b : bool) : bool := | |
if b then false else true. | |
Eval compute in (neg true). (* false *) | |
End VersionA. |
This file contains 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
object NumericOption { | |
implicit def numericOption[A](implicit N: Numeric[A]): Numeric[Option[A]] = | |
new Numeric[Option[A]] { | |
def fromInt(x: Int): Option[A] = Option(N.fromInt(x)) | |
def negate(x: Option[A]): Option[A] = x.map(N.negate(_)) | |
// Not sure whether these should return zero or throw an error... | |
def toDouble(x: Option[A]): Double = x.map(N.toDouble(_)).getOrElse(0) | |
def toFloat(x: Option[A]): Float = x.map(N.toFloat(_)).getOrElse(0) | |
def toInt(x: Option[A]): Int = x.map(N.toInt(_)).getOrElse(0) |
This file contains 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 Application(dependency: String) extends Controller { | |
def index = Action { | |
Ok(dependecy) | |
} | |
} | |
object Application extends Application("production message") |
This file contains 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
sealed trait Content | |
case class One(value: String) extends Content | |
case class Two(value: Int) extends Content | |
sealed trait AbstractCommand | |
case class IndexCommand(c: String) extends AbstractCommand | |
case class UpdateCommand(c: Int) extends AbstractCommand | |
trait Indexer { | |
def index(content: Content): Unit |
This file contains 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
object Lenses { | |
// Main API | |
case class Lens[A, B](get: A => B, set: (A, B) => A) { | |
def compose[C](other: Lens[B, C]): Lens[A, C] = { | |
Lens[A, C]( | |
get = (a: A) => other.get(get(a)), | |
set = (a: A, c: C) => set(a, other.set(get(a), c)) | |
) | |
} | |
} |
This file contains 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
object Zippers { | |
case class Zipper[A](before: List[A], focus: A, after: List[A]) { | |
/** | |
* Focus the element to the left, if any. | |
*/ | |
def left: Option[Zipper[A]] = this match { | |
case Zipper(List(), _, _) => None | |
case Zipper(x :: xs, y, zs) => Some(Zipper(xs, x, y :: zs)) | |
} |
This file contains 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
/* | |
* Assume you have a bunch of methods that return `Option` and you want to | |
* produce very precise output about which of those methods returned a `None` | |
* in case of "failure". | |
* | |
* There are a few strategies to doing that. Here are three of them: | |
*/ | |
object Main { | |
def foo: Option[String] = Some("foo") | |
def bar(x: String): Option[String] = None |
This file contains 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.Optional; | |
import java.util.function.Function; | |
public class Chain { | |
public static interface Function2<A,B,R> { | |
public R apply(A a, B b); | |
} | |
public static interface Function3<A,B,C,R> { | |
public R apply(A a, B b, C c); |