Skip to content

Instantly share code, notes, and snippets.

@piotrga
piotrga / gist:1517022
Created December 24, 2011 09:49
:: type alias
case class FunnyPair[A,B](a:A, b:B)
type ::[A,B] = FunnyPair[A, B]
def add(x: Int :: Int) = x.a + x.b
scala> case class FunnyPair[A,B](a:A, b:B)
defined class FunnyPair
scala> type ::[A,B] = FunnyPair[A, B]
defined type alias $colon$colon
@piotrga
piotrga / gist:1517036
Created December 24, 2011 09:59
Type aliasing
case class FunnyPair[A,B](a:A, b:B)
val x = FunnyPair // apparently it's equivalent to: type x = FunnyPair.type
x(2,"9")
scala> case class FunnyPair[A,B](a:A, b:B)
defined class FunnyPair
scala> val x = FunnyPair
x: FunnyPair.type = FunnyPair
@piotrga
piotrga / gist:1517067
Created December 24, 2011 10:19
Type classes in scala
trait TypeClass1[T]{
def add(a:T, b:T) : T
}
implicit val ints = new TypeClass1[Int]{
def add(a: Int, b: Int) = a+b
}
def sum[T: TypeClass1](list: List[T]) = {
require(!list.isEmpty)
@piotrga
piotrga / gist:1519603
Created December 25, 2011 18:49
Type classes in scala 2
def sum[T](list: Seq[T])(implicit typeClass : AddTypeClass[T]) = {
list.reduce(typeClass.add(_,_))
}
trait AddTypeClass[T]{
def add(a:T, b:T) : T
}
implicit val ints = new AddTypeClass[Int]{
def add(a: Int, b: Int) = a+b
@piotrga
piotrga / gist:1519565
Created December 25, 2011 18:13
Repr
trait CurrencyLike[Repr <: CurrencyLike[Repr]]{
val value : Double
def make(d:Double) : Repr
def +(x: Repr) = make(x.value + value)
}
class USD(val value: Double) extends CurrencyLike[USD] { def make(d: Double) = new USD(d) }
class EUR(val value: Double) extends CurrencyLike[EUR] { def make(d: Double) = new EUR(d) }
@piotrga
piotrga / gist:1519778
Created December 25, 2011 21:36
Try-Catch abstraction
connected {
doSomethingDangerous()
} otherwise {
println("Error! Got disconnected.")
}
def doSomethingDangerous() = {/*...*/}
def connected(body: => Unit): Result =
@piotrga
piotrga / gist:1519814
Created December 25, 2011 22:06
Lazy prime numbers
def primes : Stream[Int] = {
def findNextPrime(primes: List[Int])={
var i = primes.head+1
while(primes.exists(x => i % x == 0)){i+=1}
i
}
def prim(primes:List[Int]): Stream[Int] = primes match{
case Nil => 1 #:: 2 #:: prim(List(2))
@piotrga
piotrga / gist:1520154
Created December 26, 2011 00:48
singleThreadedMultiplication1
@inline def singleThreadedMultiplication1(m1:Seq[Array[Double]], m2:Array[Array[Double]] ) ={
val res = Array.ofDim[Double](m1.length, m2(0).length)
var col, i = 0
var row = 0
// while statements are much faster than for statements
while(row < m1.length){ col = 0
while(col < m2(0).length){ i = 0
while(i < m1(0).length){
res(row)(col) += m1(row)(i) * m2(i)(col)
@piotrga
piotrga / gist:1520151
Created December 26, 2011 00:42
singleThreadedMultiplication2
def singleThreadedMultiplication2(m1:Seq[Array[Double]], m2:Array[Array[Double]] ) ={
val res = Array.fill(m1.length, m2(0).length)(0.0)
for(row <- 0 until m1.length;
col <- 0 until m2(0).length;
i <- 0 until m1(0).length){
res(row)(col) += m1(row)(i) * m2(i)(col)
}
res
@piotrga
piotrga / gist:1520175
Created December 26, 2011 00:55
singleThreadedMultiplicationFAST
@inline def singleThreadedMultiplicationFAST(m1:Seq[Array[Double]], m2:Array[Array[Double]] ) ={
val res = Array.ofDim[Double](m1.length, m2(0).length)
val M1_COLS = m1(0).length
val M1_ROWS = m1.length
val M2_COLS = m2(0).length
var col, i = 0
var sum = 0.0
var row = 0