[COSCUP2013] Python, F#, Golang and Friends

Post on 31-Aug-2014

1.921 views 2 download

Tags:

description

 

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

. . . . . .

.

...... 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 , 6mn, num = 1 , 0ok = lambda l1 , l 2 : sum( i i n l 2 f o r i i n l 1 ) >= TA = l i s t ( combinat ions ( range (1 ,V+1) , M) )A2 = l i s t ( combinat ions ( range (1 ,V+1) , K) )wh i l e mn:

num+=1mn, mc = max((sum(ok ( l , i ) f o r i i n A) , l ) f o r l i n A2)A = f i l t e r ( lambda x : not ok (x ,mc) , A)

p r i n t ”%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 , 6l e t mn, num= r e f 1 , r e f 0l e t ok l 1 l 2 = (sum [ f o r i i n l 1 −> In t ( e x i s t s i l 2 ) ] ) >= Tl e t A = combinat ions [1 . . V] M |> r e fl e t A2 = combinat ions [1 . . V] Kwhi l e !mn >0 do

num := !num + 1l e t mn’ , mc = max [ f o r l i n A2 −> (sum[ f o r i i n !A −> In t (ok l i ) ] , l ) ]A := f i l t e r ( ( ok mc)>>not ) !Amn := 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 surpriseyou encountered rolling out Go?”

I knew the answer instantly: Although we expected C++programmers to see Go as an alternative, instead most Goprogrammers come from languages like Python and Ruby. Veryfew 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 PetersBeautiful 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 = min60◦ × g−b

max−min + 0◦, if max = r and g ≥ b60◦ × g−b

max−min + 360◦, if max = r and g < b60◦ × b−r

max−min + 120◦, if max = g60◦ × 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 而且對抽象化接受度高者,適合 ScalaF# 傾向重新定義「語意」的方式來客製化。

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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 areduction in line counts.”—Scott Dunlop~~Go is better than Python ~~(大意是這樣) —Aschobel, Co-founder ofCatch.com“[Go] is the most elegant imperative language ever (including dynamicones 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 findthat their absence results in cleaner code that is easier tounderstand even when compared to relatively simple listcomprehensions [in Python].”—Thirsteh

沒有分號,但還是有很多 {沒有 Exception

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 21 / 50

. . . . . .

Go

Garbage collectedInterface 有 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# 的原因

本來就會一點 OCamlOpen 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 s t r u c t {Lat , Long f l o a t64

}

var m = map[ s t r i n g ] Vertex{” Be l l Labs” : {40.68433 , −74.39967} ,”Google” : {37.42202 , −122.08408} ,

}

func main () {v , ok := m[ ”Moon” ]fmt . P r i n t l n ( ”The va lue : ” , v , ”Present ?” , ok )m[ ”Moon” ] = Vertex {0 , 0}f o r k , v := range m {

fmt . P r i n t l n (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 () {f o r i , v := range pow {

fmt . P r i n t f ( ”2**%d = %d\n” , i , v )}f o r _, va lue := range pow {

fmt . P r i n t f ( ”%d\n” , va lue )}f o r i := 0; i < len (pow) ; i++ {

fmt . P r i n t f ( ”%d , %d\n” , i , pow [ i ] )}

}

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 26 / 50

. . . . . .

Go Example 3: Switch

func main () {fmt . P r i n t l n ( ”When ’ s Saturday?” )today := time .Now() .Weekday ()switch time . Saturday {case today + 0:

fmt . P r i n t l n ( ”Today . ” )case today + 1:

fmt . P r i n t l n ( ”Tomorrow . ” )case today + 2:

fmt . P r i n t l n ( ” In two days . ” )d e f au l t :

fmt . P r i n t l n ( ”Too f a r away . ” )}

}

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 27 / 50

. . . . . .

Go Example 4: method

type Vertex s t r u c t {X, Y f l oa t64

}

func ( v *Vertex ) Abs () f l o a t64 {re tu rn math . Sqrt ( v .X*v .X + v .Y*v .Y)

}

type MyFloat f l o a t64

func ( f MyFloat ) aa () f l o a t64 {re tu rn f + 2.3

}

func main () {v := &Vertex {3 , 4}fmt . P r i n t l n ( 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 , e r r e r r o r )

}

func wr i t e (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 Writerw = os . Stdoutfmt . Fp r i n t f (w, ” he l l o , w r i t e r \n” )wr i t e ( os . Stdout , ” t e s 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 {

E r r o r ( ) 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 He l lo s t r u c t {}

func (h He l lo ) ServeHTTP(w http . ResponseWriter ,r *http . Request ) {fmt . Fpr in t (w, ”He l lo ! ” )

}

func main () {var h He l lohttp . ListenAndServe ( ” l o 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 )

Channelsch <− v // Send v to channe l ch .v := <−ch // Rece i v e from ch , and

// a s s i g n v a l u e 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 )

Channelsch <− v // Send v to channe l ch .v := <−ch // Rece i v e from ch , and

// a s s i g n v a l u e 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 [ ] in t , c chan i n t ) {sum := 0fo r _, 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 [ : l en (a ) /2 ] , c )go sum(a [ l en (a ) /2 : ] , c )x , y := <−c , <−c // r e c e i v e from c

fmt . P r i n t l n (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 . M i l l i s e cond )boom := time . Af te r (500 * time . M i l l i s e cond )f o r {

s e l e c t {case <−t i c k :

fmt . P r i n t l n ( ” t i c k . ” )case <−boom:

fmt . P r i n t l n ( ”BOOM! ”)re tu rn

de f au l t :fmt . P r i n t l n ( ” . ” )time . Sleep (50 * time . M i l l i s e cond )

}}

}

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 tadd 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 i d x =x

// v a l i d : x : ’ a −> ’ ai d 3L// v a l i t : i n t 6 4 = 3Li d ” 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 : i n t 6 4 ) y =x + y

// v a l add : x : i n t 6 4 −> y : i n t 6 4 −> in t 6 4

l e t add x y =( x : i n t 6 4 ) + y

// v a l add : x : i n t 6 4 −> y : i n t 6 4 −> in t 6 4

l e t add = ( fun x y −> x + y )// va l add : x : i n t −> y : i n t −> i n tl e t i n l i n e add x y = x+y ; ;(*v a l i n l i n e add :

x : ^a −> y : ^b −> ^cwhen ( ^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 : i n t 6 4 ) y =x + y

// v a l add : x : i n t 6 4 −> y : i n t 6 4 −> in t 6 4

l e t add x y =( x : i n t 6 4 ) + y

// v a l add : x : i n t 6 4 −> y : i n t 6 4 −> in t 6 4

l e t add = ( fun x y −> x + y )// va l add : x : i n t −> y : i n t −> i n tl e t i n l i n e add x y = x+y ; ;(*v a l i n l i n e add :

x : ^a −> y : ^b −> ^cwhen ( ^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#

Curryingl e t i n c = add 1l e t z = i n c 6

Operator overloadingl e t (| >) x f = f xl e t (^?) a b = Regex . I sMatch ( a , b )

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 38 / 50

. . . . . .

F#

Curryingl e t i n c = add 1l e t z = i n c 6

Operator overloadingl e t (| >) x f = f xl e t (^?) a b = Regex . I sMatch ( 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 ;Rat ing : i n t ;ISBN : s t r i n g }

l e t expe r tFSha rp ={ Name = ” Expe r t F#” ;

AuthorName = ”Don Syme” ;Rat ing = 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 ;Rat ing : i n t ;ISBN : s t r i n g }

l e t expe r tFSha rp ={ Name = ” Expe r t F#” ;

AuthorName = ”Don Syme” ;Rat ing = 5 ;ISBN = ”1590598504 ” }

(另外關於 “class” 語法可看 *)

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 39 / 50

. . . . . .

F#

Pattern Matchingl e t i n pu t = [ ( 1 . , 2 . , 0 . ) ; ( 2 . , 1 . , 1 . ) ; ( 3 . , 0 . , 1 . ) ]l e t r e c s e a r c h l s t =

match l s t w i th| ( 1 . , _ , z ) : : t a i l −>

p r i n t f n ” found x=1. and z=%f ” z ; s e a r c h t a i l| ( 2 . , _ , _) : : t a i l −>

p r i n t f n ” found x=2.” ; s e a r c h t a i l| _ : : t a i l −> sea r ch t a i l| [ ] −> p r i n t f n ” done . ” s e a r c h i npu t

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 40 / 50

. . . . . .

F#

Comprehensionl e t p roduct s = [ f o r x i n vec1 do f o r y i n vec2 do y i e l d x*y ]l e t vseq = seq { f o r x i n 1 . . 10 −> x}Seq .map ( fun x −> x*x ) vseq// va l i t : seq<in t> = 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#

Comprehensionl e t p roduct s = [ f o r x i n vec1 do f o r y i n vec2 do y i e l d x*y ]l e t vseq = seq { f o r x i n 1 . . 10 −> x}Seq .map ( fun x −> x*x ) vseq// va l i t : seq<in t> = 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 ParallelismSupport ComparisonPrabhat Totoo, Pantazis Deligiannis, Hans-Wolfgang Loidl. *

漂亮的程式碼 (pure),速度為醜的程式碼的

49.3% for Haskell33.8% for F#57.5% in Scala.

F# 在 Linux 上最慢,但在 Windows 上最快 (記憶體也用最少),。

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 42 / 50

. . . . . .

Compare: fibdef f i b (n) :

r e tu rn 1 i f n < 2 e l s e f i b (n−1) + f i b (n−2)f o r n in range (40) :

p r i n 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)

f o r n = 0 to 39 dop r i n t f n ”%A” ( f i b n)

package mainimport ”fmt”func f i b (n i n t ) i n t {

i f n < 2 {re tu rn 1}

re tu rn f i b (n−1) + f i b (n−2)}func main () {f o r n := 0;n<40;n++ {

fmt . P r i n t l n ( 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) :r e tu rn 1 i f n < 2 e l s e f i b (n−1) + f i b (n−2)

N=in t ( sys . argv [ 1 ] )f o r n in range (N) :

p r i n 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)

[<EntryPointAtt r ibute >]l e t main args =

l e t N = System . Convert . ToInt32 ( args . [ 0 ] )f o r 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”” st rconv ””os”)

func f i b (n i n t ) i n t {i f n < 2 {

re tu rn 1}

re tu rn f i b (n−1) + f i b (n−2)}func main () {N, _ := st rconv . Atoi ( os . Args [ 1 ] )f o r n := 0;n<N; n++ {

fmt . P r i n t l n ( f i b (n) )}}

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 46 / 50

. . . . . .

Compare: fib

ob jec t f i b {def f i b (n : I n t ) : Long = i f (n < 2) 1 e l s e f i b (n−1)+f i b (n−2)def main ( args : Array [ S t r i ng ] ) {

var n=0;var N=args (0) . t o I n tf o r (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 CN=30 0.44 0.18 0.03 0.05 0.22 0.01N=35 4.70 0.82 0.13 0.14 0.28 0.05N=40 57.38 7.62 1.30 1.08 0.99 0.43N=45 579.38 85.01 14.32 11.47 8.73 4.65N=48 365.18 60.05 47.45 36.18 19.85N=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 CN=30 0.44 0.18 0.03 0.05 0.22 0.01N=35 4.70 0.82 0.13 0.14 0.28 0.05N=40 57.38 7.62 1.30 1.08 0.99 0.43N=45 579.38 85.01 14.32 11.47 8.73 4.65N=48 365.18 60.05 47.45 36.18 19.85N=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 而且對抽象化接受度高者,適合 ScalaF# 傾向重新定義「語意」的方式來客製化。

TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 50 / 50