2016年4月25日 星期一

[ 英語發燒句 ] Part-39

207. "祝你好運" 
A. I'm off to my job interview.
B. Break a leg!

Note. 這句話源自劇場表演, 對即將上場演出者說出 Break a leg! 並非詛咒而是透過反話送上祝福. 

208. "你不該把家醜外揚" 
A. My husband is mad at me because I told everyone in my yoga class that he leaves his dirty socks in the living room.
B. You shouldn't wash your dirty linen in public. It's no wonder he's angry when you embarrassed him like that.

Note. linen 意為 "亞麻布", dirty linen 即 "髒衣服" 之意 

209. "原來如此" 
A. Why don't we advertise our new product?
B. We simply don't have enough money to pay for an advertising campaign.
A. So that's what it boils down to.


210. "我才不吃他這一套" 
A. Tom was late because his car wouldn't start.
B. I don't buy his story. I think he just overslept again.


211. "我們處於劣勢" 
A. Do you think we'll make the sale with the big client?
B. I doubt it. We're behind the eight ball right now.

Note. 此說法源自一種在 21 世紀初名為 "八號球" 的撞球比賽. 這種比賽規定除了黑色的八號球, 參賽者必須依序把撞球檯上的撞球依編號把球撞入球袋中. 要是八號球剛好擋在下一顆要進球的前面, 要撞球入袋難度便會提高, 因此 behind the eight ball 引申為 "處於劣勢; 陷入困境" 之意. 

212. "我們去狂歡吧" 
A. It's your birthday. Do you have any plans for tonight?
B. Let' go out and paint the town red!


2016年4月24日 星期日

[ 英語發燒句 ] Part-38

201. "別擔心" 
A. I don't have enough money to pay for the movie ticket.
B. Don't sweat it. I'll buy the ticket for you.

Note. 類似說法: Don't worry about it. 

202. "要就要, 不要就拉倒" 
A. I'm sure you can lower the price even more for me.
B. No, I can't. Take it or leave it.


203. "出錢的是老大" 
A. Can't we stay in a better hontel?
B. Not with my money. He who pays the piper calls the tune.

Note. piper 指 "吹笛手", 此說法的典故源自以前英國村莊有需多藝人, 人們可以付錢請他們演奏或表演. 字面意思是 "付錢給吹笛師的人可以決定聽什麼曲子". 

204. "她抓到你什麼把柄" 
A. I hope Jenny doesn't talk to the boss about me.
B. Why? What does she have on you?

Note. have something on sb 是指 "捉到某人把柄" 

205. "好啊!" 
A. Would you like to go to a concert this weekend?
B. I'm down with that. Who's playing?

Note. 類似說法: I'm up for that; I'm fine with that; I'm OK with that. 

206. "連個人影也沒有" 
A. Did you meet up with your friends at the forest camp?
B. No, I waited there for two hours, but there was not a soul in sight.


2016年4月23日 星期六

[Linux 常見問題] What is the Linux equivalent to DOS pause?

Source From Here
Question
I have a Bash shell script in which I would like to pause execution until the user presses a key. In DOS, this is easily accomplished with the "pause" command. Is there a Linux equivalent I can use in my script?

How-To
You can leverage read command in bash. For example:
// The -r option removes a completion specification
// -p: Display readline function names and bindings in such a way that they can be re-read.
// read: usage: read [-ers] [-a array] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...]

# read -n2 -r -p "Press any key to continue..." key // Read two character from input and saved to environment variable key
Press any key to continue...a
# echo $key
a

The -n2 specifies that it only waits for two characters. The -r puts it into raw mode, which is necessary because otherwise, if you press something like backslash, it doesn't register until you hit the next key. The -p specifies the prompt, which must be quoted if it contains spaces. The key argument is only necessary if you want to know which key they pressed, in which case you can access it through $key.

f you are using bash, you can also specify a timeout with -t, which causes read to return a failure when a key isn't pressed. So for example:
  1. read -t5 -n1 -r -p 'Press any key in the next five seconds...' key  
  2. if [ "$?" -eq "0" ]; then  
  3.     echo 'A key was pressed.'  
  4. else  
  5.     echo 'No key was pressed.'  
  6. fi  


2016年4月21日 星期四

[ 英語發燒句 ] Part-37

198. "大海撈針" 
A. I don't know how we're going to locate Daniel in such as big crowd.
B. I know. It's like finding a needle in a haystack. 

Note. 名詞 "haystack" 指 "稻草堆". 



199. "別怪在我頭上" 
A. Who made this mess in the office.
B. Don't point the finger at me! I just got here.



200. "少來了" 
A. I'm taking a two-month vacation to France over the summer.
B. Get out of here! Can you really get that much time off work?

Note. 類似說法: You've got to be kidding me 



2016年4月20日 星期三

[Scala 小學堂] Scala Gossic : 起步走 - 常用物件 (String)

轉載自 這裡 
前言 :
Scala 是一個可直譯、編譯、靜態、運行於 JVM 之上、可與 Java 互操作、融合物件導向編程特性與函式編程風格的程式語言. 從一些簡單的語法開始,了解 Scala 的許多特性,所以就算是起步走,東西也夠多了. 簡單的小程式可以使用 scala 指令,編譯的事就交給 scalac,編譯想快點就用 fsc. (Scala 官網), 在變與不變之間,先體會 Scala 更多特性.

String :
在 Scala 中,重用了 Java 的字串類別 java.lang.String,所以基本上,Scala 中字串與 Java 中的字串有著相同的特性(可以參考 那些字串二三事)。你可以在Scala 中直接用雙引號宣告字串,或者是使用字元陣列來建構字串,例如 :
- String1.scala 代碼 :
  1. val str1 = "Scala"  
  2. val str2 = new String(Array('S','c','a','l','a'))  
  3. println(str1)  
  4. println(str2)  

字串是不可變動的(Immutable),在建立之後就不能改變它的值,在內部字串是使用陣列儲存字元資料,你可以根據索引取得字串中的字元值,基本上你可以使用 charAt 方法,索引由 0 開始,例如 str1.charAt(0) 傳回'S'字元,str1.charAt(1) 傳回 'c' 字元,',以下是幾個與索引相關的方法(直接引自 java.lang.String 的方法宣告方式):


要取得字串長度資訊,也是透過 String 類別的 length 方法來取得,簡單來說,只要查詢 java.lang.String 的API,就可以知道在Scala中,字串有哪些方法可以操作. 除了使用 Java 的方式來操作字串之外,在 Scala 中,你還可以對字串作更多的操作,例如,有剖析字串,可以使用 toInt、toLong、 toDouble 等方法(在 Java 中則是使用對應 Wrapper 類別的 parseXXX() 方法),也可以將某個字串「乘以」某數,表示重複字串幾次 :
  1. println("123".toInt + "456".toInt)  
  2. println("Scala" * 3)  
若要循序取出字串中每個字元,可以使用 for 運算式 :
  1. for(c <- nbsp="" span="">"Scala") {  
  2.     println(c) // 每行顯示一個字元  
  3. }  
簡單的函式 曾介紹過的 foreach 方法,在字串上也可以使用 :
"Scala".foreach((c: Char) => println(c)) // 每行顯示一個字元

事實上,如果你要指定索引,取得字串中的字元,除了 charAt 方法之外,使用以下的方式會更為直覺 :
val str = "Scala"
println(str(0)) // 顯示 'S' 
println(str(1)) // 顯示 'c' 
println(str(4)) // 顯示 'a'

這並不是什麼神奇的功能,事實上當你操作字串時,呼叫了某個 java.lang.String 上所沒有提供的方法時,Scala 會使用隱式轉換(Implicit conversion)將字串使用 scala.collection.immutable.StringOps (RichString 自 2.7x 之後已經被改為了 StringOps) 包裹起來,上面幾個例子,實際上都是操作 StringOps 上的某些方法才得以完成(其中一些關於產生器、foreach、apply方法等,之後還會詳述). 在字串中若要表示特定字元,例如"號,則要使用 \ 來跳脫(Escape),例如 :
val str = "This is \"Scala\"!"
println(str) // 顯示 This is "Scala" !

以下是跳脫字元的指定方式 :

在Scala中,可以使用 """ 來括住字串,稱之為 原始字串(Raw string),上面的例子可以寫為以下形式
val str = """This is "Scala"!"""
println(str) // 顯示 This is "Scala" !

在原始字串中,你不用跳脫字元,所以可以直接在原始字串中寫下"、\符號,不用特別跳脫"、\符號,對於撰寫正則表示式(Regular expression)時的簡潔很有幫助,例如原先撰寫以下的正則表示式 :
val re = "\\d\\d\\d"
println("123-134".replaceAll(re, "###"))

因為正則表示式\d要寫在字串中,必須跳脫\符號,所以必須寫成\\d。如果使用Scala的原始字串,則只要寫成 :
val re = """\d\d\d"""
println("123-134".replaceAll(re, "###")) // 顯示 ###-###

原始字串也可以跨越多行 :
  1. val text = """在原始字串中,你不用跳脫字元,  
  2.               所以可以直接在原始字串中寫下"  
  3.               、\符號,不用特別跳脫"、\符號..."""  
  4. println(text)  
上面的程式看似只是縮排,不過要注意,在跨越多行時,空白也會被列為字串的一部份,所以程式執行後會顯示 :

如果不想連同空白也被列入,則可以使用 | 符號加上 stripMargin 方法,例如 :
  1. val text = """在原始字串中,你不用跳脫字元,  
  2.               |所以可以直接在原始字串中寫下"  
  3.               |、\符號,不用特別跳脫"、\符號...""".stripMargin  
  4. println(text)  
程式執行後會顯示 :

如果你熟悉 Java,對於字串的特性應該知道,直接在""中寫下字串,只要字元內容相同,無論出現幾次,在JVM中只會有一個字串實例,而 == 是用於比較兩個物件參考是否相同,所以下面這個 Java 程式結果是熟悉Java的程式設計人員都知道的事 :
  1. String str1 = "Java";  
  2. String str2 = "Java"  
  3. String str3 = new String(str1);  
  4. System.out.println(str1 == str2); // 顯示 true  
  5. System.out.println(str1 == str3); // 顯示 false  
  6. System.out.println(str1.equals(str3)); // 顯示 true  
先前說過,在Scala中重用了 java.lang.String,也許你會認為以下的程式也會顯示true、false、true,不過結果可能會令你訝異 :
  1. val str1 = "Scala"  
  2. val str2 = "Scala"  
  3. val str3 = new String(str1)  
  4. println(str1 == str2)       // 顯示 true  
  5. println(str1 == str3)       // 顯示 true  
  6. println(str1.equals(str3))  // 顯示 true  
這是因為==在 Scala 中,是比較物件實質內容是否相同,這點與Java並不同,也因此第二個顯示會是true,在 Scala 中基本上也沒有必要直接呼叫 equals 方法,而可以直接使用 ==取代(之後會介紹,在 Scala 中,若要定義物件實質相等性,也是重新定義 equals() 方法,因為 == 會呼叫 equals() 方法). 如果你真的要比較兩個物件參考是否相同,可以使用 eq 或 ne 方法,例如 :
  1. val str1 = "Scala"  
  2. val str2 = "Scala"  
  3. val str3 = new String(str1)  
  4. println(str1 eq str2)         // 顯示 true  
  5. println(str1 eq str3)         // 顯示 false  
  6. println(str1 == str3)         // 顯示 true  
補充說明 :
* Tutorialspoint - Scala Strings
* [ Java 小學堂 ] Java 世界裡 equals 與 == 的差別

This message was edited 26 times. Last update was at 21/04/2016 12:35:32

2016年4月18日 星期一

[Scala 小學堂] Scala Gossic : 起步走 - 函式、類別 (簡單的函式)

轉載自 這裡 
前言 : 
Scala 是一個可直譯、編譯、靜態、運行於 JVM 之上、可與 Java 互操作、融合物件導向編程特性與函式編程風格的程式語言. 從一些簡單的語法開始,了解 Scala 的許多特性,所以就算是起步走,東西也夠多了. 簡單的小程式可以使用 scala 指令,編譯的事就交給 scalac,編譯想快點就用 fsc. (Scala 官網), 在簡單的自定義後,將介紹更多的花樣. 

簡單的函式 : 
在Scala中要定義函式,是使用def來定義,例如,以下是個求最大公因數的函式定義 : 
  1. def gcd(m: Int, n: Int): Int={  
  2.         if(n==0)  
  3.                 return m  
  4.         else  
  5.                 return gcd(n, m % n)  
  6. }  
在上例中,gcd 是函式名稱,m 與 n 為參數名稱,型態都是宣告為 Int,雖然沒有標示出來,但在 Scala 中函式的參數都是 val 的,所以你不可以在函式中改變參數的值。括號接上一個冒號,而後的型態代表著傳回值型態,上例是以 Int 型態傳回函式的運算結果. 在 Scala 中,其實並不需要明確撰寫 return(也不鼓勵),如果是個運算式,則以區塊最後一個語句的傳回值作為運算結果,如果是個函式,則以函式執行的最後一個語句作為傳回結果。所以上例可以撰寫為 : 
  1. def gcd(m: Int, n: Int): Int={  
  2.         if(n == 0)  
  3.                 m  
  4.         else  
  5.                 gcd(n, m % n)  
  6. }  
如果是個單一語句,則大括號也可以省略,所以就可以寫成以下的形式 : 
  1. def gcd(m: Int, n: Int): Int = if(n ==0) m else gcd(n, m % n)  
在 Scala 中經常看到這樣的寫法。如果你沒有明確使用 return,且函式並非遞迴的情況(遞迴函式必須明確地宣告傳回值型態),Scala 在可以推斷出傳回值型態的情況下,你也可以省略傳回值型態的宣告,如下例中就只需在括號之後接上=即可, 例如 : 
  1. def max(m: Int, n: Int) = if(m>n) m else n  
如果你的函式沒有傳回值,則無需使用等號,或可以宣告為 Unit。例如 : 
  1. def max(m: Int, n: Int): Unit = {  
  2.         if(m > n)  
  3.                 println(m)  
  4.         else  
  5.                 println(n)  
  6. }  
Unit相當於其它語言(如C/C++、Java)void 的作用,當一個操作不需傳回值時,Scala 會傳回 Unit,撰寫或顯示方式為 ()(說 () 是個值也可以,這是與 void 最大的差別,你可以指定一個變數為 ()),例如上例中,println() 是在主控台顯示訊息,傳回值是 Unit,而max 函式定義傳回值也是 Unit,你沒辦法拿 Unit 作什麼事,所以就相當於沒有傳回值. 當一個函式沒有傳回值時,你也可以省略 Unit 與 = 號宣告,例如 : 
  1. def max(m: Int, n: Int) {  
  2.         if(m > n)  
  3.                 println(m)  
  4.         else  
  5.                 println(n)  
  6. }  
一個沒有傳回值(傳回 Unit)的函式,代表著這個函式會有邊際效用(Side effect),你執行了某個操作(可能也給了某些引數作為輸入),但不期待它有傳回值,那這個操作必然時某種形式對程式發生作用,也許是改變了程式中物件的狀態、改變了某些非函式中區塊變數的值、進行了輸入輸出操作(例如上例中,對主控台進行了輸出)等,不適當的邊際效用,容易對程式的維護造成負面影響. Scala 鼓勵你撰寫有傳回值的函式,這代表著你的函式會將輸入對應至輸出,這會使得你的函式易於測試。例如,若你如下撰寫程式,則你就只能使用眼睛從主控台觀察結果數字是否正確 : 
  1. def gcd(m: Int, n: Int) {   
  2.     if(n == 0)   
  3.         println(m)   
  4.     else gcd(n, m % n)   
  5. }  
但若函式將輸入對應至輸出,則你可以使用斷言方式來檢測結果是否正確(斷言成立,什麼事都不會發生,斷言失敗,則會丟出java.lang.AssertionError): 
  1. def gcd(m: Int, n: Int): Int = if(n==0) m else gcd(n, m%n)  
  2. assert(10 == gcd(10, 20))  
在Scala中,函式是一級(First-class)物件,你可以用 函式常量(Function literal) 的方式來定義一個函式,執行時期將會為其產生函式值(Function value)。例如,上面的max函式,可以用以下的方式定義 : 
  1. val max = (m: Int, n: Int) => if(m > n) m else n  
  2. println(max(10, 20))  
你使用 => 定義函式常量,在上例中,=> 左邊的 (m: Int, n: Int) 定義了函式的參數與類型,=> 右邊則是函式本體,max 的型態呢?實際上是 (Int, Int) => Int,也就是實際上完整的宣告應該是: 
val max: (Int, Int) => Int = (m: Int, n: Int) => if(m > n) m else n

一個實際的例子就是 Array,它有個 foreach 方法接受一個函式常量,例如,若你想顯示使用者所輸入的命令列引數,則可以如下撰寫,而不用使用 for 運算式 : 
args.foreach((x: String) => println(x))

每一次從陣列中取出元素,就會呼叫 foreach 所傳入的函式值,在上例中,就是被 x 參考住,而後使用 println() 函式來顯示元素值。之後還會看到更多類似的應用,並會對 一級函式 作更多的介紹 

補充說明 : 
* Tutorialspoint - Scala Functions

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...