Created
July 22, 2012 15:36
-
-
Save jackywyz/3160003 to your computer and use it in GitHub Desktop.
scala functions
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
class CC[T] { def unapply(a:Any):Option[T] = Some(a.asInstanceOf[T]) } | |
object M extends CC[Map[String, Any]] | |
object L extends CC[List[Any]] | |
object S extends CC[String] | |
object D extends CC[Double] | |
object B extends CC[Boolean] | |
for { | |
Some(M(map)) <- List(JSON.parseFull(jsonString)) | |
//这里的<- 和 = 会调用unapply | |
L(languages) = map("languages") | |
M(language) <- languages | |
S(name) = language("name") | |
B(active) = language("is_active") | |
D(completeness) = language("completeness") | |
} yield { | |
(name, active, completeness) | |
} | |
//another example | |
"jack" match { case S(x)=>println(x)} |
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
//curried 2.9.x | |
def say(a:Int,b:Int) = a+b //函数 | |
val fun = say(2,_:Int) //函数值Int=>Int<function1> | |
val cud = (say(_:Int,_:Int)).curried | |
cud(2)(3) | |
val say2 = Function.uncurried(cud) | |
say2(2,3) | |
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
import scala.tools.nsc.{Interpreter,Settings} | |
var i = new Interpreter(new Settings(str => println(str))) | |
i.interpret("class Test { def hello = \"Hello World\"}") | |
scala> var res = Array[AnyRef](null) | |
scala> i.bind("result", "Array[AnyRef]", res) | |
scala> i.interpret("result(0) = new Test") | |
scala> res | |
res11: Array[AnyRef] = Array(Test@2a871dcc) | |
var res = Array[(Int,Int)=>Int](null) | |
i.bind("result", "Array[(Int,Int)=>Int]", res) | |
i.interpret("result(0) ={def say(a:Int,b:Int) = a+b; say _}") |
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
//我给你一个花生,至于有没有花生米,你自己打开看下就知道了。Option/Maybe | |
import scalaz._ | |
import scalaz._ | |
import scalaz.effects._ | |
import java.io._ | |
object TheIO extends App { | |
val dirInIO = io { new File(System.getProperty("user.dir")) } | |
def filesInIO = for (dir <- dirInIO) yield dir.listFiles() | |
def namesInIO = for (theFiles ← filesInIO) yield theFiles.map(_.getName()) | |
def printNamesInIO = for (all ← namesInIO) yield all.map(println(_)) | |
val result = printNamesInIO | |
println("Nothings happened yet, lets go!") | |
result.unsafePerformIO | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment