Skip to content

Instantly share code, notes, and snippets.

View notyy's full-sized avatar

大魔头 notyy

View GitHub Profile
@notyy
notyy / gist:1322200
Created October 28, 2011 12:54
high order in haskell
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"]
@notyy
notyy / gist:1322257
Created October 28, 2011 13:28
cant infer
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))
@notyy
notyy / gist:1322299
Created October 28, 2011 13:48
scala style
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
}
@notyy
notyy / gist:1326066
Created October 30, 2011 15:59
strange
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)
@notyy
notyy / gist:1326865
Created October 31, 2011 03:45
super fast
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
@notyy
notyy / gist:1326956
Created October 31, 2011 05:22
is this efficent?
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) {
@notyy
notyy / gist:1336825
Created November 3, 2011 15:46
multi inheri
trait Wood {
def burn = println("I am burning")
}
trait Door {
def open = println("opened, you can coming")
}
class WoodDoor extends Door with Wood
@notyy
notyy / gist:1343027
Created November 6, 2011 15:28
stacked modification
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)
@notyy
notyy / gist:1343063
Created November 6, 2011 15:57
logger mixin
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()}
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)
}
}