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
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 |
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
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 |
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 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) |
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 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) } |
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
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 |
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
connected { | |
doSomethingDangerous() | |
} otherwise { | |
println("Error! Got disconnected.") | |
} | |
def doSomethingDangerous() = {/*...*/} | |
def connected(body: => Unit): Result = |
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
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)) |
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
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 |
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
@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) |
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
@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 |
OlderNewer