[COSCUP2013] Python, F#, Golang and Friends
Upcoming SlideShare
Loading in...5
×
 

[COSCUP2013] Python, F#, Golang and Friends

on

  • 1,912 views

 

Statistics

Views

Total Views
1,912
Slideshare-icon Views on SlideShare
1,195
Embed Views
717

Actions

Likes
4
Downloads
25
Comments
0

21 Embeds 717

http://weijr-note.blogspot.tw 576
http://weijr-note.blogspot.com 60
http://cloud.feedly.com 28
http://digg.com 12
http://weijr-note.blogspot.hk 7
http://www.plurk.com 6
http://weijr-note.blogspot.co.nz 4
http://weijr-note.blogspot.jp 3
http://www.feedspot.com 3
http://reader.aol.com 3
http://weijr-note.blogspot.com.au 3
http://weijr-note.blogspot.cz 2
http://www.blogger.com 2
http://translate.googleusercontent.com 1
http://weijr-note.blogspot.it 1
http://weijr-note.blogspot.se 1
https://www.plurk.com 1
http://weijr-note.blogspot.de 1
http://weijr-note.blogspot.co.uk 1
http://summary 1
http://weijr-note.blogspot.sg 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

CC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike LicenseCC Attribution-NonCommercial-ShareAlike License

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    [COSCUP2013] Python, F#, Golang and Friends [COSCUP2013] Python, F#, Golang and Friends Presentation Transcript

    • . . . . . . . ...... Python, F#, Golang and Friends TJW* @ COSCUP 2013 2013-08-04 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 1 / 50
    • . . . . . . 先講大家最關心的 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 2 / 50
    • . . . . . . 真相 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 3 / 50
    • . . . . . . F# TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 4 / 50
    • . . . . . . Scala TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 5 / 50
    • . . . . . . Go TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 6 / 50
    • . . . . . . D TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 7 / 50
    • . . . . . . 樂透組合 Python 版 V,K,T,M = 49 , 6 , 3 , 6 mn, num = 1 , 0 ok = lambda l1 , l2 : sum( i in l2 for i in l1 ) >= T A = l i s t ( combinations ( range (1 ,V+1) , M) ) A2 = l i s t ( combinations ( range (1 ,V+1) , K) ) while mn: num+=1 mn, mc = max((sum(ok( l , i ) for i in A) , l ) for l in A2) A = f i l t e r (lambda x : not ok(x ,mc) , A) pr int ”%s mc=%d mn=%d”%(num, mc, mn) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 8 / 50
    • . . . . . . 樂透組合 F# 版 l e t V,K,T,M = 49 , 6 , 3 , 6 l e t mn, num= r e f 1 , r e f 0 l e t ok l1 l2 = (sum [ for i in l1 −> Int ( e x i s t s i l2 ) ] ) >= T l e t A = combinations [1 . . V] M |> r e f l e t A2 = combinations [1 . . V] K while !mn >0 do num := !num + 1 l e t mn’ , mc = max[ for l in A2 −> (sum[ for i in !A −> Int (ok l i ) ] , l ) ] A := f i l t e r (( ok mc)>>not ) !A mn := mn’ p r i n t f n ”%A mc=%A mn=%A” !num mc !mn TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 9 / 50
    • . . . . . . 尋找理想語言 I was asked a few weeks ago, ”What was the biggest surprise you encountered rolling out Go?” I knew the answer instantly: Although we expected C++ programmers to see Go as an alternative, instead most Go programmers come from languages like Python and Ruby. Very few come from C++. – Rob Pike * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 10 / 50
    • . . . . . . The Zen of Python >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is better than implicit. Simple is better than complex. Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts. Special cases aren’t special enough to break the rules. Although practicality beats purity. Errors should never pass silently. Unless explicitly silenced. In the face of ambiguity, refuse the temptation to guess. There should be one– and preferably only one –obvious way to do it. Although that way may not be obvious at first unless you’re Dutch. Now is better than never. Although never is often better than *right* now. If the implementation is hard to explain, it’s a bad idea. If the implementation is easy to explain, it may be a good idea. Namespaces are one honking great idea – let’s do more of those! TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 11 / 50
    • . . . . . . The Zen of Python 易讀 Readability counts 易維護 Easy to maintain 易實作 Easy to implement TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 12 / 50
    • . . . . . . Language Matters h =    0◦ if max = min 60◦ × g−b max−min + 0◦, if max = r and g ≥ b 60◦ × g−b max−min + 360◦, if max = r and g < b 60◦ × b−r max−min + 120◦, if max = g 60◦ × r−g max−min + 240◦, if max = b TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 13 / 50
    • . . . . . . HSV Example 1 =IF ( IF ( MAX(C2,C3,C4)=0, 0 , IF ( C2=MAX(C2, C3,C4) , (MAX(C2, C3,C4) − C4) /(MAX(C2, C3,C4) − MIN(C2, C3,C4) ) − (MAX(C2, C3,C4) − C3) /(MAX(C2,C3, C4) − MIN(C2, C3,C4) ) , IF ( C3=MAX(C2,C3,C4) , 2 + (MAX(C2, C3,C4) − C2 ) /(MAX(C2, C3,C4) − MIN(C2,C3,C4) ) − (MAX(C2, C3,C4) − C4) /(MAX(C2, C3 ,C4) − MIN(C2, C3,C4) ) , 4 + (MAX(C2,C3,C4) − C3) /(MAX(C2, C3,C4) − MIN(C2,C3,C4) ) − (MAX(C2, C3,C4) − C2) /(MAX(C2, C3, C4) − MIN(C2, C3,C4 ) ) ) ) ) / 6 < 0 , IF ( MAX(C2, C3,C4)=0, 0 , IF ( C2=MAX(C2, C3,C4) , ( MAX(C2,C3,C4) − C4) /(MAX(C2, C3,C4) − MIN(C2, C3,C4) ) − (MAX(C2, C3,C4 ) − C3) /(MAX(C2,C3,C4) − MIN(C2, C3,C4) ) , IF ( C3=MAX(C2,C3,C4) , 2 + (MAX(C2,C3,C4) − C2) /(MAX(C2, C3,C4) − MIN(C2,C3,C4) ) − (MAX(C2, C3, C4) − C4) /(MAX(C2, C3,C4) − MIN(C2, C3,C4) ) , 4 + (MAX(C2,C3,C4) − C3) /(MAX(C2, C3,C4) − MIN(C2, C3,C4) ) − (MAX(C2, C3,C4) − C2) /(MAX(C2,C3, C4) − MIN(C2, C3,C4) ) ) ) ) / 6 + 1 , IF ( MAX(C2, C3,C4)=0, 0 , IF ( C2= MAX(C2,C3,C4) , (MAX(C2, C3,C4) − C4) /(MAX(C2, C3,C4) − MIN(C2,C3,C4) ) − (MAX(C2, C3,C4) − C3) /(MAX(C2, C3,C4) − MIN(C2, C3,C4) ) , IF ( C3=MAX (C2, C3,C4) , 2 + (MAX(C2, C3,C4) − C2) /(MAX(C2,C3,C4) − MIN(C2, C3,C4) ) − (MAX(C2,C3,C4) − C4) /(MAX(C2, C3,C4) − MIN(C2, C3,C4) ) , 4 + (MAX( C2,C3,C4) − C3) /(MAX(C2, C3,C4) − MIN(C2,C3,C4) ) − (MAX(C2, C3,C4) − C2) /(MAX(C2,C3,C4) − MIN(C2, C3,C4) ) ) ) ) / 6) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 14 / 50
    • . . . . . . HSV Example 2 =i f ((C2−C3)^2=−(C3−C4) ^2 ,0 , i f (C3>C4, i f (C3>C2 , ( C4−C2) /(C3−min(C2,C4) ) +2,(C3−C4) /(C2−C4) ) , i f (C2>C4 , ( C3−C4) /(C2−C3)+6,(C2−C3) /(C4−min(C2, C3) )+4)) )/6 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 15 / 50
    • . . . . . . HSV Example 2 =i f (( C2−C3)^2=−(C3−C4) ^2 , 0 , i f (C3>C4 , i f (C3>C2 , (C4−C2) /(C3−min (C2 , C4) ) +2, (C3−C4) /(C2−C4) ) , i f (C2>C4 , (C3−C4) /(C2−C3) +6, (C2−C3) /(C4−min (C2 , C3) )+4)) ) /6 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 16 / 50
    • . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
    • . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
    • . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
    • . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
    • . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
    • . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
    • . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
    • . . . . . . 候選者 Python: Go: 21st 世紀的 C * F#: Caml 和.Net 的小孩裝成 Python 的樣子 Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵) Javascript: 裝成 C 的 Lisp * * Coffeescript: 裝成 Haskell 的 Javascript * * Lua, Clojure, Haskell, Ruby, Scheme, D, Nemerle … TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 18 / 50
    • . . . . . . 候選者 Python: Go: 21st 世紀的 C * F#: Caml 和.Net 的小孩裝成 Python 的樣子 Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵) Javascript: 裝成 C 的 Lisp * * Coffeescript: 裝成 Haskell 的 Javascript * * Lua, Clojure, Haskell, Ruby, Scheme, D, Nemerle … TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 18 / 50
    • . . . . . . 候選者 Python: Go: 21st 世紀的 C * F#: Caml 和.Net 的小孩裝成 Python 的樣子 Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵) Javascript: 裝成 C 的 Lisp * * Coffeescript: 裝成 Haskell 的 Javascript * * Lua, Clojure, Haskell, Ruby, Scheme, D, Nemerle … TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 18 / 50
    • . . . . . . 候選者 Python: Go: 21st 世紀的 C * F#: Caml 和.Net 的小孩裝成 Python 的樣子 Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵) Javascript: 裝成 C 的 Lisp * * Coffeescript: 裝成 Haskell 的 Javascript * * Lua, Clojure, Haskell, Ruby, Scheme, D, Nemerle … TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 18 / 50
    • . . . . . . 候選者 Python: Go: 21st 世紀的 C * F#: Caml 和.Net 的小孩裝成 Python 的樣子 Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵) Javascript: 裝成 C 的 Lisp * * Coffeescript: 裝成 Haskell 的 Javascript * * Lua, Clojure, Haskell, Ruby, Scheme, D, Nemerle … TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 18 / 50
    • . . . . . . 候選者 Python: Go: 21st 世紀的 C * F#: Caml 和.Net 的小孩裝成 Python 的樣子 Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵) Javascript: 裝成 C 的 Lisp * * Coffeescript: 裝成 Haskell 的 Javascript * * Lua, Clojure, Haskell, Ruby, Scheme, D, Nemerle … TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 18 / 50
    • . . . . . . 候選者 Python: Go: 21st 世紀的 C * F#: Caml 和.Net 的小孩裝成 Python 的樣子 Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵) Javascript: 裝成 C 的 Lisp * * Coffeescript: 裝成 Haskell 的 Javascript * * Lua, Clojure, Haskell, Ruby, Scheme, D, Nemerle … TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 18 / 50
    • . . . . . . 先講結論 Go 適合 Web Service、標準演算法、多工 適合用 C 的方式思考的 不適合科學計算,想客製化語言的人 F# 和 Scala 差不多,看你比較討厭 { 還是 MS 適合常用 Functional Programming 方式使用 Python 適合喜歡客製化語言的人 常用 OOP 而且對抽象化接受度高者,適合 Scala F# 傾向重新定義「語意」的方式來客製化。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 19 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . 創造理想的語言 我學 Go 的原因 聽說 Go 是一種很好的語言 (而且有靜態鴨) * Scala 6000 lines -> Go 3000 lines “Porting my code review tools to Go from Python. Surprised to see a reduction in line counts.”—Scott Dunlop ~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder of Catch.com “[Go] is the most elegant imperative language ever (including dynamic ones like Lua, Ruby, and Python)”—Quoc Anh Trinh 把 Python 跟 Go 結合在一起有沒有搞頭? 就叫做 PyGon 好了 (名字都取好了) 先用 Ply 寫個 Go 的 Parser 好了。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 20 / 50
    • . . . . . . Go 沒有 Class 沒有 template, function generics 沒有 operator overloading 沒有 list comprehension “I thought I would miss list comprehensions a lot, but I find that their absence results in cleaner code that is easier to understand even when compared to relatively simple list comprehensions [in Python].”—Thirsteh 沒有分號,但還是有很多 { 沒有 Exception TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 21 / 50
    • . . . . . . Go Garbage collected Interface 有 duck type 的特點 有 Go Playground 和 A Tour of Go * 自動排版 不准有 unused variable 有 go 特別的錯誤處理方式 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 22 / 50
    • . . . . . . 學 F# 的原因 本來就會一點 OCaml Open Source 有 list comprehension .Net 跟 Python 結合有沒有搞頭? TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 23 / 50
    • . . . . . . F# Try F# * (類似 Go playground) 有 REPL 開源, 跨平台 (Mac/Linux/Android/iOS/JS&HTML5/GPU) type inference TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 24 / 50
    • . . . . . . F# Try F# * (類似 Go playground) 有 REPL 開源, 跨平台 (Mac/Linux/Android/iOS/JS&HTML5/GPU) type inference TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 24 / 50
    • . . . . . . F# Try F# * (類似 Go playground) 有 REPL 開源, 跨平台 (Mac/Linux/Android/iOS/JS&HTML5/GPU) type inference TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 24 / 50
    • . . . . . . F# Try F# * (類似 Go playground) 有 REPL 開源, 跨平台 (Mac/Linux/Android/iOS/JS&HTML5/GPU) type inference TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 24 / 50
    • . . . . . . Go Example 1: map package main import ”fmt” type Vertex struct { Lat , Long float64 } var m = map[ s t r i n g ] Vertex{ ” Bell Labs” : {40.68433 , −74.39967}, ”Google” : {37.42202 , −122.08408}, } func main () { v , ok := m[ ”Moon” ] fmt . Println (”The value : ” , v , ” Present ?” , ok) m[ ”Moon” ] = Vertex {0 , 0} for k , v := range m { fmt . Println (k , v , m[ k ] ) } } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 25 / 50
    • . . . . . . Go Example 2: For var pow = [ ] i n t {1 , 2 , 4 , 8 , 16 , 32 , 64 , 128} func main () { for i , v := range pow { fmt . Printf (”2**%d = %dn” , i , v) } for _, value := range pow { fmt . Printf (”%dn” , value ) } for i := 0; i < len (pow) ; i++ { fmt . Printf (”%d , %dn” , i , pow[ i ] ) } } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 26 / 50
    • . . . . . . Go Example 3: Switch func main () { fmt . Println (”When ’ s Saturday?”) today := time .Now() . Weekday() switch time . Saturday { case today + 0: fmt . Println (”Today . ”) case today + 1: fmt . Println (”Tomorrow . ”) case today + 2: fmt . Println (” In two days . ”) default : fmt . Println (”Too fa r away . ”) } } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 27 / 50
    • . . . . . . Go Example 4: method type Vertex struct { X, Y float64 } func (v *Vertex ) Abs () float64 { return math . Sqrt (v .X*v .X + v .Y*v .Y) } type MyFloat float64 func ( f MyFloat ) aa () float64 { return f + 2.3 } func main () { v := &Vertex {3 , 4} fmt . Println (v . Abs () ) } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 28 / 50
    • . . . . . . Go Example 5: interface type Writer i n t e r f a c e { Write (b [ ] byte ) (n int , er r er r o r ) } func write (w Writer , s t r s t r i n g ) { w. Write ( [ ] byte ( s t r ) ) } func main () { var w Writer // os . Stdout implements Writer w = os . Stdout fmt . Fprintf (w, ” hello , writer n”) write ( os . Stdout , ” tes t n”) } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 29 / 50
    • . . . . . . Error An error is anything that can describe itself as an error string. type e r r o r i n t e r f a c e { Error () s t r i n g } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 30 / 50
    • . . . . . . Go Example 6: HTTP Server package main import ( ”fmt” ”net/http ” ) type Hello struct {} func (h Hello ) ServeHTTP( w http . ResponseWriter , r *http . Request ) { fmt . Fprint (w, ” Hello ! ”) } func main () { var h Hello http . ListenAndServe (” lo c a l h o s t :4000” , h) } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 31 / 50
    • . . . . . . Goroutines A goroutine is a lightweight thread managed by the Go runtime. go f ( x , y , z ) Channels ch <− v // Send v to channel ch . v := <−ch // Receive from ch , and // a s s i g n value to v . 類似 python 的 multiprocessing/pipe,但更好,取代 generator 的功能。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 32 / 50
    • . . . . . . Goroutines A goroutine is a lightweight thread managed by the Go runtime. go f ( x , y , z ) Channels ch <− v // Send v to channel ch . v := <−ch // Receive from ch , and // a s s i g n value to v . 類似 python 的 multiprocessing/pipe,但更好,取代 generator 的功能。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 32 / 50
    • . . . . . . Go Example 7:Goroutines func sum(a [ ] int , c chan i n t ) { sum := 0 for _, v := range a { sum += v } c <− sum // send sum to c } func main () { a := [ ] i n t {7 , 2 , 8 , −9, 4 , 0} c := make( chan i n t ) go sum(a [ : len (a) /2] , c ) go sum(a [ len (a) /2:] , c ) x , y := <−c , <−c // r ec e i ve from c fmt . Println (x , y , x+y) } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 33 / 50
    • . . . . . . Go Example 8:Goroutines func main () { t i c k := time . Tick (100 * time . Millisecond ) boom := time . After (500 * time . Millisecond ) for { s e l e c t { case <−t i c k : fmt . Println (” t i c k . ”) case <−boom: fmt . Println (”BOOM! ”) return default : fmt . Println (” . ”) time . Sleep (50 * time . Millisecond ) } } } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 34 / 50
    • . . . . . . F# 定義函數 l e t add x y = x + y // v a l add : x : i n t −> y : i n t −> i n t add 2 2 l e t add ( x , y ) = x + y // v a l add : x : i n t * y : i n t −> i n t add (2 ,2) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 35 / 50
    • . . . . . . F# l e t id x = x // v a l id : x : ’ a −> ’ a id 3L // v a l i t : int64 = 3L id ” coscup ” // v a l i t : s t r i n g = ” coscup ” TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 36 / 50
    • . . . . . . F# 標記型態 l e t add ( x : int64 ) y = x + y // v a l add : x : int64 −> y : int64 −> int64 l e t add x y = ( x : int64 ) + y // v a l add : x : int64 −> y : int64 −> int64 l e t add = ( fun x y −> x + y ) // val add : x : i n t −> y : i n t −> i n t l e t i n l i n e add x y = x+y ; ; (* val i n l i n e add : x : ^a −> y : ^b −> ^c when ( ^a or ^b) : ( s t a t i c member ( + ) : ^a * ^b −> ^c ) *) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 37 / 50
    • . . . . . . F# 標記型態 l e t add ( x : int64 ) y = x + y // v a l add : x : int64 −> y : int64 −> int64 l e t add x y = ( x : int64 ) + y // v a l add : x : int64 −> y : int64 −> int64 l e t add = ( fun x y −> x + y ) // val add : x : i n t −> y : i n t −> i n t l e t i n l i n e add x y = x+y ; ; (* val i n l i n e add : x : ^a −> y : ^b −> ^c when ( ^a or ^b) : ( s t a t i c member ( + ) : ^a * ^b −> ^c ) *) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 37 / 50
    • . . . . . . F# Currying l e t inc = add 1 l e t z = inc 6 Operator overloading l e t (| >) x f = f x l e t (^?) a b = Regex . IsMatch (a , b) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 38 / 50
    • . . . . . . F# Currying l e t inc = add 1 l e t z = inc 6 Operator overloading l e t (| >) x f = f x l e t (^?) a b = Regex . IsMatch (a , b) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 38 / 50
    • . . . . . . F# type Book = { Name : s t r i n g ; AuthorName : s t r i n g ; Rating : i n t ; ISBN : s t r i n g } l e t expertFSharp = { Name = ” Expert F#” ; AuthorName = ”Don Syme” ; Rating = 5; ISBN = ” 1590598504 ” } (另外關於 “class” 語法可看 *) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 39 / 50
    • . . . . . . F# type Book = { Name : s t r i n g ; AuthorName : s t r i n g ; Rating : i n t ; ISBN : s t r i n g } l e t expertFSharp = { Name = ” Expert F#” ; AuthorName = ”Don Syme” ; Rating = 5; ISBN = ” 1590598504 ” } (另外關於 “class” 語法可看 *) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 39 / 50
    • . . . . . . F# Pattern Matching l e t input = [ ( 1 . , 2. , 0 . ) ; ( 2 . , 1. , 1 . ) ; ( 3 . , 0. , 1 . ) ] l e t rec search l s t = match l s t with | ( 1 . , _, z ) : : t a i l −> p r i n t f n ” found x=1. and z=%f ” z ; search t a i l | ( 2 . , _, _) : : t a i l −> p r i n t f n ” found x=2.” ; search t a i l | _ : : t a i l −> search t a i l | [ ] −> p r i n t f n ”done . ” search input TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 40 / 50
    • . . . . . . F# Comprehension l e t products = [ f o r x in vec1 do f o r y in vec2 do y i e l d x*y ] l e t vseq = seq { f o r x in 1 . . 10 −> x} Seq .map ( fun x −> x*x ) vseq // val i t : seq<int > = seq [ 1 ; 4; 9; 16; . . . ] vseq |> Seq .map ( fun x −> x*x ) vseq |> PSeq .map ( fun x −> x*x ) |> Array . ofSeq TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 41 / 50
    • . . . . . . F# Comprehension l e t products = [ f o r x in vec1 do f o r y in vec2 do y i e l d x*y ] l e t vseq = seq { f o r x in 1 . . 10 −> x} Seq .map ( fun x −> x*x ) vseq // val i t : seq<int > = seq [ 1 ; 4; 9; 16; . . . ] vseq |> Seq .map ( fun x −> x*x ) vseq |> PSeq .map ( fun x −> x*x ) |> Array . ofSeq TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 41 / 50
    • . . . . . . 比較 Haskell vs. F# vs. Scala: A High-level Language Features and Parallelism Support Comparison Prabhat Totoo, Pantazis Deligiannis, Hans-Wolfgang Loidl. * 漂亮的程式碼 (pure),速度為醜的程式碼的 49.3% for Haskell 33.8% for F# 57.5% in Scala. F# 在 Linux 上最慢,但在 Windows 上最快 (記憶體也用最少),。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 42 / 50
    • . . . . . . Compare: fib def f i b (n) : return 1 i f n < 2 e l s e f i b (n−1) + f i b (n−2) for n in range (40) : prin t f i b (n) l e t rec f i b n = i f n < 2 then 1L e l s e f i b (n−1) + f i b (n−2) for n = 0 to 39 do p r i n t f n ”%A” ( f i b n) package main import ”fmt” func f i b (n i n t ) i n t { i f n < 2 { return 1 } return f i b (n−1) + f i b (n−2) } func main () { for n := 0;n<40;n++ { fmt . Println ( f i b (n) ) } } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 43 / 50
    • . . . . . . Compare: fib import sys def f i b (n) : return 1 i f n < 2 e l s e f i b (n−1) + f i b (n−2) N=i n t ( sys . argv [ 1 ] ) for n in range (N) : prin t f i b (n) TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 44 / 50
    • . . . . . . Compare: fib l e t rec f i b n = i f n < 2 then 1L e l s e f i b (n−1) + f i b (n−2) [<EntryPointAttribute >] l e t main args = l e t N = System . Convert . ToInt32 ( args . [ 0 ] ) for n = 0 to N−1 do f i b n |> p r i n t f n ”%A” 0 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 45 / 50
    • . . . . . . Compare: fib package main import ( ”fmt” ” strconv ” ”os” ) func f i b (n i n t ) i n t { i f n < 2 { return 1 } return f i b (n−1) + f i b (n−2) } func main () { N, _ := strconv . Atoi ( os . Args [ 1 ] ) for n := 0;n<N; n++ { fmt . Println ( f i b (n) ) } } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 46 / 50
    • . . . . . . Compare: fib object f i b { def f i b (n : Int ) : Long = i f (n < 2) 1 e l s e f i b (n−1)+f i b (n−2) def main( args : Array [ String ] ) { var n=0; var N=args (0) . toInt for (n <− 0 to N−1){ p r i n t l n ( f i b (n) ) } } } TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 47 / 50
    • . . . . . . 時間 Python PyPy Go F#(Mono) Scala C N=30 0.44 0.18 0.03 0.05 0.22 0.01 N=35 4.70 0.82 0.13 0.14 0.28 0.05 N=40 57.38 7.62 1.30 1.08 0.99 0.43 N=45 579.38 85.01 14.32 11.47 8.73 4.65 N=48 365.18 60.05 47.45 36.18 19.85 N=50 949.38 159.65 125.28 95.78 53.11 gccgo 和 C 差不多,win64 的 F# 和 Scala 差不多。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 48 / 50
    • . . . . . . 時間 Python PyPy Go F#(Mono) Scala C N=30 0.44 0.18 0.03 0.05 0.22 0.01 N=35 4.70 0.82 0.13 0.14 0.28 0.05 N=40 57.38 7.62 1.30 1.08 0.99 0.43 N=45 579.38 85.01 14.32 11.47 8.73 4.65 N=48 365.18 60.05 47.45 36.18 19.85 N=50 949.38 159.65 125.28 95.78 53.11 gccgo 和 C 差不多,win64 的 F# 和 Scala 差不多。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 48 / 50
    • . . . . . . 結論 其實很多 Go, F#, Scala, 語言上的特異功能 (Interface, type inference, Higher kinded type) Python 都辦得到,甚至可以說根本不是問題 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 49 / 50
    • . . . . . . 結論 Go 適合 Web Service、標準演算法、多工 適合用 C 的方式思考的 不適想客製化語言的人 F# 和 Scala 差不多,看你比較討厭 { 還是 MS 適合常用 Functional Programming 方式使用 Python 適合喜歡客製化語言的人 常用 OOP 而且對抽象化接受度高者,適合 Scala F# 傾向重新定義「語意」的方式來客製化。 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 50 / 50