Skip to content

Instantly share code, notes, and snippets.

View igstan's full-sized avatar

Ionuț G. Stan igstan

View GitHub Profile
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))
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)
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.
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)
class Application(dependency: String) extends Controller {
def index = Action {
Ok(dependecy)
}
}
object Application extends Application("production message")
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
@igstan
igstan / lenses.scala
Last active August 29, 2015 14:13
Simple lenses in Scala.
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))
)
}
}
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))
}
/*
* 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
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);