Skip to content

Instantly share code, notes, and snippets.

View igstan's full-sized avatar

Ionuț G. Stan igstan

View GitHub Profile
@igstan
igstan / lower-bound.scala
Created April 4, 2014 11:38
Scala lower bounds + multiple argument list.
class Sup
class Sub(val element: Int) extends Sup
def test1[B >: Sub](a: Sub, b: B): B = if (a.element < 0) b else a
// ||
// || difference
// ||
// \/
def test2[B >: Sub](a: Sub)(b: B): B = if (a.element < 0) b else a
trait Fn[A] {
def apply[B >: A](t: B): B
}
class Sup
class Sub extends Sup
def test[S >: Sub](a: Sub): Fn[S] =
new Fn[S] {
override def apply[B >: S](b: B): B = if (true) a else b
trait Fn[A,B] {
// AA is contravariant; BB is covariant
// Similar to Function1[-AA, +BB]
def apply[AA >: A, BB <: B](t: AA): BB
}
class Sup
class Sub extends Sup
def test[S >: Sub](a: Sub): Fn[S,S] =
object Example {
trait HttpParser[A] {
// For the sake of simplicity, parse doesn't take any params.
// Returning an Option is to reflect the fact that parse may
// fail.
def parse: Option[A]
}
trait HttpRenderer[B] {
object Example {
trait HttpParser[A] {
def parse: Option[A]
}
trait HttpRenderer[B] {
def render(b: B): String
}
@igstan
igstan / decluttered-australis-ui.png
Last active August 29, 2015 14:00
Decluttered UI in Firefox 29
decluttered-australis-ui.png
// Dependency comes first when currying: OO-style constructors.
function foo(dependency) {
return function (a) {
return dependency(a);
}
}
// Dependency comes last when currying: Reader Monad.
function foo(a) {
return function (dependency) {
<?php
namespace acme;
function array_key_exists($key, $a) {
if ($a instanceof \ArrayAccess) {
return $a->offsetExists($key);
} else {
return \array_key_exists($key, $a);
}
<?php
namespace acme;
function array_key_exists($key, $a) {
if ($a instanceof \ArrayAccess) {
return $a->offsetExists($key);
} else {
return \array_key_exists($key, $a);
}
trait A {
val count: Int
val elements: Array[String] = new Array(count)
}
class C extends A {
override val count = 10
}
val c = new C