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 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) | |
} |
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 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 |
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
<!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> </p> | |
<input id="shellcommand_in" width="800"/> |
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
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) |
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
quicksort' :: (Ord a) => [a] -> [a] | |
quicksort' [] = [] | |
quicksort' (x:xs) = smaller ++ [x] ++ bigger | |
where smaller = quicksort' $ filter (<=x) xs | |
bigger = quicksort' $ filter (>x) xs |
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
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)))) |
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 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 { |
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 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 | |
} |
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 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) |
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 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) |
OlderNewer