Skip to content

Instantly share code, notes, and snippets.

View notyy's full-sized avatar

大魔头 notyy

View GitHub Profile
@notyy
notyy / gist:1234930
Created September 22, 2011 14:35
can match any parameter
def tryMatch: PartialFunction[Any, Unit] = {
case s:String => println("its a string:" + s)
case (s1:String,s2:String) => println("two strings:" + s1 + " and "+ s2)
case i:Int => println("its a int:" + i)
}
@notyy
notyy / gist:1235065
Created September 22, 2011 15:28
onsubmit def
def onSubmit (func: (String) ⇒ Any): (NodeSeq) ⇒ NodeSeq
execute the String function when the form is submitted. This method returns a function that can be applied to form fields (input, button, textarea, select) and the function is executed when the form containing the field is submitted.
object ChatIn {
/**
* The render method in this case returns a function
* that transforms NodeSeq => NodeSeq. In this case,
* the function transforms a form input element by attaching
* behavior to the input. The behavior is to send a message
@notyy
notyy / gist:1241798
Created September 26, 2011 07:42
width no use?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<p>&nbsp;</p>
<input id="shellcommand_in" width="800"/>
@notyy
notyy / gist:1292865
Created October 17, 2011 15:29
pattern matching haskell vs scala
addList :: (Num a) => [(a,a)] -> [a]
addList xs = [x + y | (x,y) <- xs]
def addList(xs:List[(Int,Int)]): List[Int] = for((x,y) <- xs) yield(x+y)
@notyy
notyy / gist:1312942
Created October 25, 2011 14:34
how to implement this in scala?
quicksort' :: (Ord a) => [a] -> [a]
quicksort' [] = []
quicksort' (x:xs) = smaller ++ [x] ++ bigger
where smaller = quicksort' $ filter (<=x) xs
bigger = quicksort' $ filter (>x) xs
@notyy
notyy / gist:1313294
Created October 25, 2011 16:12
quicksort in lisp
user=> (defn qs [l]
(if (< (count l) 2) l
(let [left (filter #(< % (first l)) (rest l))
right (filter #(>= % (first l)) (rest l))]
(concat left (list (first l)) right))))
@notyy
notyy / gist:1321966
Created October 28, 2011 09:35
change to func
def assertArray(b1: Array[Byte], b2: Array[Byte]) {
if (b1 == null && b2 == null) return;
else if (b1 != null && b2 != null) {
if (b1.length != b2.length) throw new AssertionError("b1.length != b2.length")
else {
for (i <- b1.indices) {
if (b1(i) != b2(i)) throw new AssertionError("b1(%d) != b2(%d)".format(i, i))
}
}
} else {
@notyy
notyy / gist:1321990
Created October 28, 2011 09:51
sand...
def assertArray[T](b1: Option[Array[T]], b2: Option[Array[T]]):Boolean = (b1,b2) match {
case (None,None) => true
case (Some(arr1),Some(arr2)) if(arr1.toList == arr2.toList) => true
case (_,_) => false
}
@notyy
notyy / gist:1322045
Created October 28, 2011 10:35
flatmap
def toInt(in: String): Option[Int] =
try {
Some(Integer.parseInt(in.trim))
} catch {
case e: NumberFormatException => None
}
def sum(in: Seq[String]) = {
val ints = in.flatMap(s => toInt(s))
ints.foldLeft(0)((a, b) => a + b)
@notyy
notyy / gist:1322186
Created October 28, 2011 12:48
high order function
def mymap[A,B](xs:List[A],f: A => B): List[B] = {
xs.map(f)
}
scala> mymap(List(1,2,3),((_:Int)*2))
res2: List[Int] = List(2, 4, 6)
scala> mymap(List(1,2,3),((_:Int).toString))
res3: List[java.lang.String] = List(1, 2, 3)