Skip to content

Instantly share code, notes, and snippets.

View manjuraj's full-sized avatar

Manju Rajashekhar manjuraj

View GitHub Profile
@manjuraj
manjuraj / gist:8346897
Created January 10, 2014 04:14
Ordering.by
scala> case class Foo(n: Int)
defined class Foo
scala> List[Foo]().sorted
<console>:10: error: No implicit Ordering defined for Foo.
List[Foo]().sorted
^
scala> implicit def ordFoo = Ordering.by[Foo, Int](_.n)
ordFoo: scala.math.Ordering[Foo]
@manjuraj
manjuraj / gist:8362562
Created January 10, 2014 21:03
universal equality in the face of subtyping
class Animal(val name: String) {
override def equals(other: Any): Boolean = other match {
case that: Animal =>
(that canEqual this) &&
(this.name == that.name)
case _ => false
}
def canEqual(other: Any): Boolean = other.isInstanceOf[Animal]
}
scala> scala.util.Properties.versionString
res0: String = version 2.10.3
scala> scala.util.Properties.jdkHome
res1: String = /Library/Java/JavaVirtualMachines/1.7.0.jdk/Contents/Home
@manjuraj
manjuraj / gist:8366202
Created January 11, 2014 02:23
vals and inheritance
abstract class Foo {
val a: String
val b = "hello" + a
}
class Bar extends Foo {
val a = "world"
}
// What is wrong with the above code?
@manjuraj
manjuraj / gist:8378176
Last active January 2, 2016 23:39
functional typeclasses and object oriented typeclasses
// functional typeclass pattern
// using type traits and implicit parameters
case class Person(name: String, age: Int)
case class Restaurant(name: String, location: String)
// Typeclass
trait Serializable[T] {
def ser(t: T): String
}
@manjuraj
manjuraj / gist:8378326
Last active January 2, 2016 23:49
Using higher order functions to achieve abstraction and conciness
type Pred[A] = A => Boolean
def not[A](p: Pred[A]): Pred[A] = a => !p(a)
def and[A](p1: Pred[A], p2: Pred[A]): Pred[A] = a => p1(a) && p2(a)
def or[A](p1: Pred[A], p2: Pred[A]): Pred[A] = a => p1(a) || p2(a)
def isDivisibleBy(k: Int): Pred[Int] = i => i % k == 0
@manjuraj
manjuraj / gist:8406896
Last active January 3, 2016 04:09
type selection vs type projection
scala> :pa
class Outer {
trait Inner // nested type
def y = new Inner {}
def foo(x: Inner) = null // path-dependent type - same as `def foo(x: this.Inner) = null`
def bar(x: Outer#Inner) = null // type projection
}
scala> val x = new Outer
@manjuraj
manjuraj / gist:8408219
Last active January 3, 2016 04:19
Simplifying type signatures using higher-kinded types
scala> def f[M[_]](x: M[Int]) = x
f: [M[_]](x: M[Int])M[Int]
scala> val cb = (x: Int) => println(x)
cb: Int => Unit = <function1>
scala> f[Function1](cb)
<console>:11: error: Function1 takes two type parameters, expected: one
f[Function1](cb)
@manjuraj
manjuraj / gist:8528864
Last active January 3, 2016 22:29
Code snippets from "Generics of Higher Kinds" paper
// First-order parametric polymorphism - abstract over types (simple types or proper types)
// Higher-order parametric polymorphism - abstract over type constructors (type functions)
// Other names for Higher-order polymorphism
// - Type constructor polymorphism
// - Higher-kinded types
// Higher-kinded types enables us to use type constructor as type parameters and abstract type members
// Higher-kinded types is a type that abstract over types that abstract over types
@manjuraj
manjuraj / gist:8548582
Created January 21, 2014 21:18
TryHarder - Try with finally
import scala.util.Try
implicit class TryHarder[T](t:Try[T]) {
def harder(f: => Unit) = {f; t}
}
Try(1/0).harder(println("finally")) // prints 'finally'
Try(try { ??? } finally { 1 / 0 }).harder(println("finally finally")) // prints 'finally finally'