This file contains hidden or 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
mymap :: [a] -> (a -> b) -> [b] | |
mymap xs f = map f xs | |
*Main> mymap [1..5] (*2) | |
[2,4,6,8,10] | |
*Main> mymap [1..5] (show) | |
["1","2","3","4","5"] |
This file contains hidden or 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 newMap[A,B](x:A, f: A => B): B = { | |
f(x) | |
} | |
scala> newMap(1:Int,(_*2)) | |
<console>:9: error: missing parameter type for expanded function ((x$1) => x$1.$times(2)) | |
newMap(1:Int,(_*2)) | |
^ | |
scala> newMap(1,((_:Int)*2)) |
This file contains hidden or 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 assertArray[T](b1: Array[T], b2: Array[T]):Boolean = (Option(b1),Option(b2)) match { | |
case (None,None) => true | |
case (Some(arr1),Some(arr2)) if(arr1.toList == arr2.toList) => true | |
case _ => false | |
} |
This file contains hidden or 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
scala> val multInt = for(x <- (1 to 5)) yield (x*(_:Int)) | |
multInt: scala.collection.immutable.IndexedSeq[Int => Int] = Vector(<function1>, <function1>, <function1>, <function1>, <function1>) | |
scala> val mult2 = multInt.map(_(2)) | |
mult2: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10) |
This file contains hidden or 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
getPrime :: Integer -> Integer | |
getPrime n = head $! filter (\x -> n `mod` x == 0) ([2..(sqrtIntn (realToFrac n))] ++ [n]) | |
where sqrtIntn n = ceiling (sqrt n) | |
primeList :: Integer -> [Integer] | |
primeList n | prime == n = [n] | |
| otherwise = prime : primeList (n `div` prime) | |
where prime = getPrime n | |
problem3 n = last $ primeList n |
This file contains hidden or 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
class Dispatcher { | |
private List data = new ArrayList(); | |
private List listeners = new ArrayList(); | |
public synchronized onNewData(byte[] newData){ | |
updateData() | |
notifyListeners(newData) | |
} | |
public synchronized register(Listener l) { |
This file contains hidden or 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 Wood { | |
def burn = println("I am burning") | |
} | |
trait Door { | |
def open = println("opened, you can coming") | |
} | |
class WoodDoor extends Door with Wood |
This file contains hidden or 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
import scala.collection.mutable.ArrayBuffer | |
abstract class IntQueue { | |
def get(): Int | |
def put(x:Int) | |
} | |
class BasicIntQueue extends IntQueue { | |
private val buf = new ArrayBuffer[Int] | |
def get() = buf.remove(0) |
This file contains hidden or 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
abstract class BizService { | |
def doIt() | |
} | |
class Worker extends BizService { | |
def doIt() {println("I am working")} | |
} | |
trait Logger extends BizService { | |
abstract override def doIt() {println("logMe first");super.doIt()} |
This file contains hidden or 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 test3() { | |
val fruits = Array("apple", "orange", "banana") | |
for (i <- fruits.indices) { | |
val fruit = fruits(i) | |
Actor.actor { | |
for (j <- 1 to 3) { | |
println(j+" : "+fruit) | |
Thread.sleep(5000) | |
} | |
} |