SlideShare uma empresa Scribd logo
1 de 79
Baixar para ler offline
. . . . . .
.
......
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

Mais conteúdo relacionado

Mais procurados

教青少年寫程式
教青少年寫程式教青少年寫程式
教青少年寫程式Renyuan Lyu
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhouWill Zhou
 
Android C Library: Bionic 成長計畫
Android C Library: Bionic 成長計畫Android C Library: Bionic 成長計畫
Android C Library: Bionic 成長計畫Kito Cheng
 
Shell脚本
Shell脚本Shell脚本
Shell脚本bj
 
Introduction of Reverse Engineering
Introduction of Reverse EngineeringIntroduction of Reverse Engineering
Introduction of Reverse EngineeringYC Ling
 
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的機器學習入門 scikit-learn 連淡水阿嬤都聽得懂的機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn Cicilia Lee
 
Python 入门
Python 入门Python 入门
Python 入门kuco945
 
Light talk @ coscup 2011 : Incremental Global Prelink for Android
Light talk @ coscup 2011 : Incremental Global Prelink for AndroidLight talk @ coscup 2011 : Incremental Global Prelink for Android
Light talk @ coscup 2011 : Incremental Global Prelink for AndroidKito Cheng
 
[系列活動] 手把手教你R語言資料分析實務
[系列活動] 手把手教你R語言資料分析實務[系列活動] 手把手教你R語言資料分析實務
[系列活動] 手把手教你R語言資料分析實務台灣資料科學年會
 
全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版Simen Li
 
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack FirmwareSimen Li
 
MicroPython簡介
MicroPython簡介 MicroPython簡介
MicroPython簡介 Max Lai
 
R 語言教學: 探索性資料分析與文字探勘初探
R 語言教學: 探索性資料分析與文字探勘初探R 語言教學: 探索性資料分析與文字探勘初探
R 語言教學: 探索性資料分析與文字探勘初探Sean Yu
 
Python速成指南
Python速成指南Python速成指南
Python速成指南March Liu
 
漫談 Source Control Management
漫談 Source Control Management漫談 Source Control Management
漫談 Source Control ManagementWen-Shih Chao
 

Mais procurados (18)

Vim Hacks
Vim HacksVim Hacks
Vim Hacks
 
教青少年寫程式
教青少年寫程式教青少年寫程式
教青少年寫程式
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
 
Android C Library: Bionic 成長計畫
Android C Library: Bionic 成長計畫Android C Library: Bionic 成長計畫
Android C Library: Bionic 成長計畫
 
Shell脚本
Shell脚本Shell脚本
Shell脚本
 
Op 20090411
Op 20090411Op 20090411
Op 20090411
 
Introduction of Reverse Engineering
Introduction of Reverse EngineeringIntroduction of Reverse Engineering
Introduction of Reverse Engineering
 
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的機器學習入門 scikit-learn 連淡水阿嬤都聽得懂的機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
 
Python story
Python storyPython story
Python story
 
Python 入门
Python 入门Python 入门
Python 入门
 
Light talk @ coscup 2011 : Incremental Global Prelink for Android
Light talk @ coscup 2011 : Incremental Global Prelink for AndroidLight talk @ coscup 2011 : Incremental Global Prelink for Android
Light talk @ coscup 2011 : Incremental Global Prelink for Android
 
[系列活動] 手把手教你R語言資料分析實務
[系列活動] 手把手教你R語言資料分析實務[系列活動] 手把手教你R語言資料分析實務
[系列活動] 手把手教你R語言資料分析實務
 
全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版全端物聯網探索之旅 - 重點整理版
全端物聯網探索之旅 - 重點整理版
 
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
[ZigBee 嵌入式系統] ZigBee Architecture 與 TI Z-Stack Firmware
 
MicroPython簡介
MicroPython簡介 MicroPython簡介
MicroPython簡介
 
R 語言教學: 探索性資料分析與文字探勘初探
R 語言教學: 探索性資料分析與文字探勘初探R 語言教學: 探索性資料分析與文字探勘初探
R 語言教學: 探索性資料分析與文字探勘初探
 
Python速成指南
Python速成指南Python速成指南
Python速成指南
 
漫談 Source Control Management
漫談 Source Control Management漫談 Source Control Management
漫談 Source Control Management
 

Destaque

R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-iDr. Awase Khirni Syed
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programmingizahn
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golangBo-Yi Wu
 
The Market Segmentation Process
The Market Segmentation ProcessThe Market Segmentation Process
The Market Segmentation ProcessAiden Yeh
 
Docker introduction
Docker introductionDocker introduction
Docker introductiondotCloud
 

Destaque (7)

Hmm viterbi
Hmm viterbiHmm viterbi
Hmm viterbi
 
Golang for OO Programmers
Golang for OO ProgrammersGolang for OO Programmers
Golang for OO Programmers
 
R programming groundup-basic-section-i
R programming groundup-basic-section-iR programming groundup-basic-section-i
R programming groundup-basic-section-i
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
Write microservice in golang
Write microservice in golangWrite microservice in golang
Write microservice in golang
 
The Market Segmentation Process
The Market Segmentation ProcessThe Market Segmentation Process
The Market Segmentation Process
 
Docker introduction
Docker introductionDocker introduction
Docker introduction
 

Semelhante a [COSCUP2013] Python, F#, Golang and Friends

数据处理算法设计要点
数据处理算法设计要点数据处理算法设计要点
数据处理算法设计要点thinkinlamp
 
程式人雜誌 -- 2015 年9月號
程式人雜誌 -- 2015 年9月號程式人雜誌 -- 2015 年9月號
程式人雜誌 -- 2015 年9月號鍾誠 陳鍾誠
 
Objc under the_hood_2013
Objc under the_hood_2013Objc under the_hood_2013
Objc under the_hood_2013Michael Pan
 
程式人雜誌 -- 2015 年7月號
程式人雜誌 -- 2015 年7月號程式人雜誌 -- 2015 年7月號
程式人雜誌 -- 2015 年7月號鍾誠 陳鍾誠
 
程式人雜誌 2015年七月
程式人雜誌 2015年七月程式人雜誌 2015年七月
程式人雜誌 2015年七月鍾誠 陳鍾誠
 
8门编程语言的设计思考
8门编程语言的设计思考8门编程语言的设计思考
8门编程语言的设计思考Ray Song
 
用Erlang构建容错系统
用Erlang构建容错系统用Erlang构建容错系统
用Erlang构建容错系统Cheng Lian
 
Vim hacks
Vim hacksVim hacks
Vim hacksXuYj
 
Python basic - v01
Python   basic - v01Python   basic - v01
Python basic - v01ssuser5e7722
 
程式人雜誌 -- 2014 年8月號
程式人雜誌 -- 2014 年8月號程式人雜誌 -- 2014 年8月號
程式人雜誌 -- 2014 年8月號鍾誠 陳鍾誠
 
搜狐Pv insight(py)技术交流
搜狐Pv insight(py)技术交流搜狐Pv insight(py)技术交流
搜狐Pv insight(py)技术交流bj
 
搜狐Pv insight(py)技术交流
搜狐Pv insight(py)技术交流搜狐Pv insight(py)技术交流
搜狐Pv insight(py)技术交流jondynet
 

Semelhante a [COSCUP2013] Python, F#, Golang and Friends (13)

数据处理算法设计要点
数据处理算法设计要点数据处理算法设计要点
数据处理算法设计要点
 
程式人雜誌 -- 2015 年9月號
程式人雜誌 -- 2015 年9月號程式人雜誌 -- 2015 年9月號
程式人雜誌 -- 2015 年9月號
 
Objc under the_hood_2013
Objc under the_hood_2013Objc under the_hood_2013
Objc under the_hood_2013
 
程式人雜誌 -- 2015 年7月號
程式人雜誌 -- 2015 年7月號程式人雜誌 -- 2015 年7月號
程式人雜誌 -- 2015 年7月號
 
程式人雜誌 2015年七月
程式人雜誌 2015年七月程式人雜誌 2015年七月
程式人雜誌 2015年七月
 
8门编程语言的设计思考
8门编程语言的设计思考8门编程语言的设计思考
8门编程语言的设计思考
 
用Erlang构建容错系统
用Erlang构建容错系统用Erlang构建容错系统
用Erlang构建容错系统
 
Vim hacks
Vim hacksVim hacks
Vim hacks
 
Python basic - v01
Python   basic - v01Python   basic - v01
Python basic - v01
 
程式人雜誌 -- 2014 年8月號
程式人雜誌 -- 2014 年8月號程式人雜誌 -- 2014 年8月號
程式人雜誌 -- 2014 年8月號
 
Python Basic
Python  BasicPython  Basic
Python Basic
 
搜狐Pv insight(py)技术交流
搜狐Pv insight(py)技术交流搜狐Pv insight(py)技术交流
搜狐Pv insight(py)技术交流
 
搜狐Pv insight(py)技术交流
搜狐Pv insight(py)技术交流搜狐Pv insight(py)技术交流
搜狐Pv insight(py)技术交流
 

[COSCUP2013] Python, F#, Golang and Friends

  • 1. . . . . . . . ...... Python, F#, Golang and Friends TJW* @ COSCUP 2013 2013-08-04 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 1 / 50
  • 2. . . . . . . 先講大家最關心的 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 2 / 50
  • 3. . . . . . . 真相 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 3 / 50
  • 4. . . . . . . F# TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 4 / 50
  • 5. . . . . . . Scala TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 5 / 50
  • 6. . . . . . . Go TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 6 / 50
  • 7. . . . . . . D TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 7 / 50
  • 8. . . . . . . 樂透組合 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
  • 9. . . . . . . 樂透組合 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
  • 10. . . . . . . 尋找理想語言 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
  • 11. . . . . . . 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
  • 12. . . . . . . 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
  • 13. . . . . . . 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
  • 14. . . . . . . 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
  • 15. . . . . . . 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
  • 16. . . . . . . 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
  • 17. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  • 18. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  • 19. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  • 20. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  • 21. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  • 22. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  • 23. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  • 24. . . . . . . 候選者 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
  • 25. . . . . . . 候選者 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
  • 26. . . . . . . 候選者 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
  • 27. . . . . . . 候選者 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
  • 28. . . . . . . 候選者 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
  • 29. . . . . . . 候選者 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
  • 30. . . . . . . 候選者 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
  • 31. . . . . . . 先講結論 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
  • 32. . . . . . . 創造理想的語言 我學 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
  • 33. . . . . . . 創造理想的語言 我學 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
  • 34. . . . . . . 創造理想的語言 我學 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
  • 35. . . . . . . 創造理想的語言 我學 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
  • 36. . . . . . . 創造理想的語言 我學 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
  • 37. . . . . . . 創造理想的語言 我學 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
  • 38. . . . . . . 創造理想的語言 我學 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
  • 39. . . . . . . 創造理想的語言 我學 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
  • 40. . . . . . . 創造理想的語言 我學 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
  • 41. . . . . . . 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
  • 42. . . . . . . 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
  • 43. . . . . . . 學 F# 的原因 本來就會一點 OCaml Open Source 有 list comprehension .Net 跟 Python 結合有沒有搞頭? TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 23 / 50
  • 44. . . . . . . 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
  • 45. . . . . . . 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
  • 46. . . . . . . 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
  • 47. . . . . . . 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
  • 48. . . . . . . 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
  • 49. . . . . . . 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
  • 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
  • 51. . . . . . . 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
  • 52. . . . . . . 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
  • 53. . . . . . . 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
  • 54. . . . . . . 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
  • 55. . . . . . . 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
  • 56. . . . . . . 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
  • 57. . . . . . . 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
  • 58. . . . . . . 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
  • 59. . . . . . . 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
  • 60. . . . . . . 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
  • 61. . . . . . . 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
  • 62. . . . . . . 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
  • 63. . . . . . . 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
  • 64. . . . . . . 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
  • 65. . . . . . . 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
  • 66. . . . . . . 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
  • 67. . . . . . . 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
  • 68. . . . . . . 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
  • 69. . . . . . . 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
  • 70. . . . . . . 比較 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
  • 71. . . . . . . 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
  • 72. . . . . . . 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
  • 73. . . . . . . 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
  • 74. . . . . . . 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
  • 75. . . . . . . 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
  • 76. . . . . . . 時間 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
  • 77. . . . . . . 時間 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
  • 78. . . . . . . 結論 其實很多 Go, F#, Scala, 語言上的特異功能 (Interface, type inference, Higher kinded type) Python 都辦得到,甚至可以說根本不是問題 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 49 / 50
  • 79. . . . . . . 結論 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