scala list collect 用法
collect methoad 接受 PartialFunction 作為參數。結果產生一個 list。輸出 list 的內容是該 PartialFunction 有定義且由 PartialFunction 運算後的值
PartialFunction 長的像這樣
{
case a: Int => a.toString
case a: String => 1
}
以下面例子作為說明
List(1, 2, "3", Nil) collect { case a: Int => a.toString case b: String => 1}
其結果為
List(1, 2, 1)
說明
原始的 list 內容是整數 1, 2
和字串 3 和一個 Nil 物件
整數 1 和 2 有定義在 PartialFunction,
因此1 和 2 經過 PartialFunction 運算後,會放在結果
又字串 3 有定義在 PartialFunction,
因此 3 經過 PartialFunction 運算後,會放在結果
又 Nil 不沒有定義在 PartialFunction
因此沒有出現在結果
2015年6月7日 星期日
2014年11月11日 星期二
run play 2.3 in cloud9
For play 2.3 or above, run the below command in terminal:
activator run -Dhttp.address=$IP -Dhttp.port=$PORT run
activator run -Dhttp.address=$IP -Dhttp.port=$PORT run
2014年11月9日 星期日
scala shell(console) with vim key bind.
If you want to use scala shell with vim key bind,
you can press `esc + enter`.
Now you enter to vim mode.
When you enter vim mode, you will in Insert mode.
In Insert mode, it will display anything you type.
If you want to switch to Normal mode, just type `esc` or `ctrl + [`.
It's similar as vim.
Now you can move your cursor using `h`,`l`.
Or you can delete line using `dd`, etc.
Enjoy it.
How does it work?
https://github.com/jline/jline2/wiki/JLine-2.x-Wiki
2014年11月8日 星期六
How to get the play version for scala.
type below in your terminal
or
In scala console,
type
It will display the play version you use.
activator console
or
play console
In scala console,
type
play.core.PlayVersion.current
2014年11月1日 星期六
用 scala akka 寫一個簡單的 grep 程式
akka 是一個 actor model 。因此他的思維和一般寫程式的思維很不一樣。akka 可以讓我們很容易寫出 concurrent 的程式。我用 akka 寫了兩個 grep 程式,用不一樣的方式。
我會介紹的兩個程式碼在此
JGrep1.scala 是推薦寫法。 akka 鼓勵你大量建立 actor 。在這隻程式裡,每個資料夾或檔案都會對應到一個 actor ,一個 actor 只會負責一個檔案或目錄。
JGrep2.scala 是一個特別的寫法。這隻程式會重複利用 FolderExplorer (這個例子中,最多會有15 個 actor )和 FileReader (這個例子中,最多會有25 個 actor )。這個例子是為了示範 RoundRobinRouter 的使用方法。
scala 中,將 list 根據條件分成兩個,一個是滿足條件的 list,一個是不滿足條件的 list。
Scala 中, list 有許多內建好用的函數。partition 是其中一個。
來看個簡單的例子
List(1, 2, 3, 1, 2).partition(_ == 2)
換產生
res0: (List[Int], List[Int]) = (List(2, 2),List(1, 3, 1))
第一個 tuple 的元素是滿足條件的 list,第二個 tuple 的元素是不滿足條件的 list。
有了 partition ,就可以很容易作到一些事情,如建立字元出現頻率的對照表
來看個簡單的例子
List(1, 2, 3, 1, 2).partition(_ == 2)
換產生
res0: (List[Int], List[Int]) = (List(2, 2),List(1, 3, 1))
第一個 tuple 的元素是滿足條件的 list,第二個 tuple 的元素是不滿足條件的 list。
有了 partition ,就可以很容易作到一些事情,如建立字元出現頻率的對照表
def makeMap(data: List[Char], output: Map[Char, Int]): Map[Char, Int] = data match {
case x :: xs => {
val (expected, rest) = data.partition(_ == x)
makeMap(rest, output + (x -> expected.length))
}
case Nil => output
}
2014年7月12日 星期六
scala 中 for ...yield 與 map, flatMap, withFilter 之互換
-------------------------------
for(x <- expr1) yield expr2
expr1.map(x => expr2)
-------------------------------
for(x <- expr1 if expr2) expr3
expr1.withFilter(x => expr2).map(x => expr3)
-------------------------------
for(x <- expr1 if expr2; seq) yield expr3
for(x <- expr1 withFilter expr2; seq) yield expr3
-------------------------------
for(x <- expr1; y <- expr2; seq) yield expr3
expr1.flatMap(x => for (y <- expr2; seq) yield expr3)
-------------------------------
for ((x1, x2) <- expr1) yield expr2
expr1.map{case(x1, x2) => expr2}
-------------------------------
for(pat <- expr1) yield2
expr1 withFilter {
case pat = > true
case _ => false
} map {
case pat => expr2
}
2014年5月3日 星期六
在 sbt 下使用 scala 內建的 actor
在 build.sbt 裡新增
libraryDependencies +=
"org.scala-lang" % "scala-actors" % "2.10.0"
scala-actors 是 library name
2.10.0 是版本號
他有提供什麼 library 以及版本可從下面列表去找
https://oss.sonatype.org/content/groups/public/org/scala-lang/
2014年4月6日 星期日
scala 中對於不接受參數的函數定義方式與使用方法
scala 對於不接受參數的函數定義方式有兩種。如下面程式碼所示。
第一種寫法,沒有 () 。使用時,不可以加上 () 否則會出錯。因此呼叫時只有一種寫法,test1。
第二種寫法,有 () 。使用時,可以加上 (),也可以不要加上 () 。因此呼叫時有兩種寫法, test2 或 test2()。
def test1 = { // 1. without ()
println("SS")
}
def test2() = { //2. with ()
println("GG")
}
test1()
第一種寫法,沒有 () 。使用時,不可以加上 () 否則會出錯。因此呼叫時只有一種寫法,test1。
第二種寫法,有 () 。使用時,可以加上 (),也可以不要加上 () 。因此呼叫時有兩種寫法, test2 或 test2()。
scala 語言特性優缺點分析
這篇文章闡述哪些 scala 特性"我認為"是好的,哪些是不好的。因為是"我認為",所以這是一篇充滿主觀意見的文章。
優點:
範例1
缺點:
優點:
- type inference: 這個特性可以減少程式設計師輸入重複的資訊。
- lazy keyword: 可以做到延遲計算的功能
- currying: 讓人更容易產生 partial function。
- 對於接受參數型別為 => Unit(Int等),要傳參數時,可以簡單的用 {
} 代替。詳見範例 1 actor: 多執行緒的函式庫,用 message queue share memory 的問題(當然,還是要看你的寫法。沒寫好還是有 share memory 的問題)
範例1
def repeat(times: Int) (func: => Unit) {
for (n <- 1 to times) func
}
repeat (5) {
println("SSS")
}
缺點:
- implicit conversion: 這會影響可讀性。因為你在閱讀時,無法立刻知道他在執行時,是以何種物件在執行。不過這個特性應該是為了能夠和 jvm 整合所產生的特性。
- 探討 scala 的函數呼叫與自動推論型別所產生的模糊情形
結論: scala 是一個很好的語言。不過他是 function programming language 的關係,可能不會紅起來。但是下一個如果能夠吸收 scala 的優點,改進 scala 的缺點,我猜能夠成為下一個主流語言。
2014年4月5日 星期六
scala play framework 使用方式
要建立專案,輸入
$ play new myFirstApp
進入 play 的 shell (用來輸入指令的)
$ cd myFirstApp
$ play shell
如果要啟用 dev server 在 play shell 輸入
run
或是
$ play run
進入 play 的 console (用來輸入程式碼的)
$ play debug console
如果要使用 stand-alone server
$ play start
$ play start
你也可以把你的程式打包起來,複製到其他地方執行
$ play dist
他會產生一個 zip 檔,包含所有執行時必要的 jar。
其他可能有用指令
因為 neo4j 只支援 openjdk-7 ,所以用下面指令檢查 java version。不然 play start 未必能正常運作。
$javac -version
$java -version
2014年2月22日 星期六
2013年10月20日 星期日
探討 scala 的函數呼叫與自動推論型別所產生的模糊情形
scala 是靜態型別語言,不過他擁有自動推論型別的功能,因此可以減少重複宣告型別的情形。如以下程式碼:
val a = 1
由於 1 是整數,因此可以推論出 a 的型別為 int。再看看定義 function 的情形。
def func(a:int, b:int) = a + b
func 接受兩個參數,他的資料型別是 int,編譯器會推論出 func 的回傳值為 int。
訂閱:
文章 (Atom)