Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
. . . . . .
.
......
Python, F#, Golang and Friends
TJW* @ COSCUP 2013
2013-08-04
TJW* @ COSCUP 2013 () Python, F#, Golang...
. . . . . .
先講大家最關心的
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...
. . . . . .
樂透組合 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 ...
. . . . . .
尋找理想語言
I was asked a few weeks ago, ”What was the biggest surprise
you encountered rolling out Go?”
I knew the...
. . . . . .
The Zen of Python
>>> import this The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is ...
. . . . . .
The Zen of Python
易讀 Readability counts
易維護 Easy to maintain
易實作 Easy to implement
TJW* @ COSCUP 2013 () Pytho...
. . . . . .
Language Matters
h =



0◦ if max = min
60◦ × g−b
max−min + 0◦, if max = r and g ≥ b
60◦ × g−b
...
. . . . . .
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) ...
. . . . . .
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) ...
. . . . . .
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,
(C...
. . . . . .
Lessons Learned
Language Matters (易讀、易除錯、易寫)
語言影響思考 *
括號對可讀性的幫助不大
排版有幫助
短程式碼可以幫助可讀性
理想的程式語言: 覺得用幾句話能說明的事情,就能用幾...
. . . . . .
Lessons Learned
Language Matters (易讀、易除錯、易寫)
語言影響思考 *
括號對可讀性的幫助不大
排版有幫助
短程式碼可以幫助可讀性
理想的程式語言: 覺得用幾句話能說明的事情,就能用幾...
. . . . . .
Lessons Learned
Language Matters (易讀、易除錯、易寫)
語言影響思考 *
括號對可讀性的幫助不大
排版有幫助
短程式碼可以幫助可讀性
理想的程式語言: 覺得用幾句話能說明的事情,就能用幾...
. . . . . .
Lessons Learned
Language Matters (易讀、易除錯、易寫)
語言影響思考 *
括號對可讀性的幫助不大
排版有幫助
短程式碼可以幫助可讀性
理想的程式語言: 覺得用幾句話能說明的事情,就能用幾...
. . . . . .
Lessons Learned
Language Matters (易讀、易除錯、易寫)
語言影響思考 *
括號對可讀性的幫助不大
排版有幫助
短程式碼可以幫助可讀性
理想的程式語言: 覺得用幾句話能說明的事情,就能用幾...
. . . . . .
Lessons Learned
Language Matters (易讀、易除錯、易寫)
語言影響思考 *
括號對可讀性的幫助不大
排版有幫助
短程式碼可以幫助可讀性
理想的程式語言: 覺得用幾句話能說明的事情,就能用幾...
. . . . . .
Lessons Learned
Language Matters (易讀、易除錯、易寫)
語言影響思考 *
括號對可讀性的幫助不大
排版有幫助
短程式碼可以幫助可讀性
理想的程式語言: 覺得用幾句話能說明的事情,就能用幾...
. . . . . .
候選者
Python:
Go: 21st 世紀的 C *
F#: Caml 和.Net 的小孩裝成 Python 的樣子
Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵)
Javascrip...
. . . . . .
候選者
Python:
Go: 21st 世紀的 C *
F#: Caml 和.Net 的小孩裝成 Python 的樣子
Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵)
Javascrip...
. . . . . .
候選者
Python:
Go: 21st 世紀的 C *
F#: Caml 和.Net 的小孩裝成 Python 的樣子
Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵)
Javascrip...
. . . . . .
候選者
Python:
Go: 21st 世紀的 C *
F#: Caml 和.Net 的小孩裝成 Python 的樣子
Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵)
Javascrip...
. . . . . .
候選者
Python:
Go: 21st 世紀的 C *
F#: Caml 和.Net 的小孩裝成 Python 的樣子
Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵)
Javascrip...
. . . . . .
候選者
Python:
Go: 21st 世紀的 C *
F#: Caml 和.Net 的小孩裝成 Python 的樣子
Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵)
Javascrip...
. . . . . .
候選者
Python:
Go: 21st 世紀的 C *
F#: Caml 和.Net 的小孩裝成 Python 的樣子
Scala: 2112 年的 Java (要什麼有什麼,而且沒有不需要的耳朵)
Javascrip...
. . . . . .
先講結論
Go
適合 Web Service、標準演算法、多工
適合用 C 的方式思考的
不適合科學計算,想客製化語言的人
F# 和 Scala 差不多,看你比較討厭 { 還是 MS
適合常用 Functional Pr...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
創造理想的語言
我學 Go 的原因
聽說 Go 是一種很好的語言 (而且有靜態鴨) *
Scala 6000 lines -> Go 3000 lines
“Porting my code review tools to...
. . . . . .
Go
沒有 Class
沒有 template, function generics
沒有 operator overloading
沒有 list comprehension
“I thought I would mi...
. . . . . .
Go
Garbage collected
Interface 有 duck type 的特點
有 Go Playground 和 A Tour of Go *
自動排版
不准有 unused variable
有 go
...
. . . . . .
學 F# 的原因
本來就會一點 OCaml
Open Source
有 list comprehension
.Net
跟 Python 結合有沒有搞頭?
TJW* @ COSCUP 2013 () Python, F#...
. . . . . .
F#
Try F# * (類似 Go playground)
有 REPL
開源, 跨平台 (Mac/Linux/Android/iOS/JS&HTML5/GPU)
type inference
TJW* @ COSCU...
. . . . . .
F#
Try F# * (類似 Go playground)
有 REPL
開源, 跨平台 (Mac/Linux/Android/iOS/JS&HTML5/GPU)
type inference
TJW* @ COSCU...
. . . . . .
F#
Try F# * (類似 Go playground)
有 REPL
開源, 跨平台 (Mac/Linux/Android/iOS/JS&HTML5/GPU)
type inference
TJW* @ COSCU...
. . . . . .
F#
Try F# * (類似 Go playground)
有 REPL
開源, 跨平台 (Mac/Linux/Android/iOS/JS&HTML5/GPU)
type inference
TJW* @ COSCU...
. . . . . .
Go Example 1: map
package main
import ”fmt”
type Vertex struct {
Lat , Long float64
}
var m = map[ s t r i n g...
. . . . . .
Go Example 2: For
var pow = [ ] i n t {1 , 2 , 4 , 8 , 16 , 32 , 64 , 128}
func main () {
for i , v := range p...
. . . . . .
Go Example 3: Switch
func main () {
fmt . Println (”When ’ s Saturday?”)
today := time .Now() . Weekday()
swit...
. . . . . .
Go Example 4: method
type Vertex struct {
X, Y float64
}
func (v *Vertex ) Abs () float64 {
return math . Sqrt...
. . . . . .
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 wr...
. . . . . .
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 {
Err...
. . . . . .
Go Example 6: HTTP Server
package main
import (
”fmt”
”net/http ”
)
type Hello struct {}
func (h Hello ) Serve...
. . . . . .
Goroutines
A goroutine is a lightweight thread managed by the Go runtime.
go f ( x , y , z )
Channels
ch <− v ...
. . . . . .
Goroutines
A goroutine is a lightweight thread managed by the Go runtime.
go f ( x , y , z )
Channels
ch <− v ...
. . . . . .
Go Example 7:Goroutines
func sum(a [ ] int , c chan i n t ) {
sum := 0
for _, v := range a {
sum += v
}
c <− s...
. . . . . .
Go Example 8:Goroutines
func main () {
t i c k := time . Tick (100 * time . Millisecond )
boom := time . After...
. . . . . .
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 +...
. . . . . .
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 ...
. . . . . .
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 :...
. . . . . .
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 :...
. . . . . .
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 = Rege...
. . . . . .
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 = Rege...
. . . . . .
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 e...
. . . . . .
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 e...
. . . . . .
F#
Pattern Matching
l e t input = [ ( 1 . , 2. , 0 . ) ; ( 2 . , 1. , 1 . ) ; ( 3 . , 0. , 1 . ) ]
l e t rec s...
. . . . . .
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...
. . . . . .
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...
. . . . . .
比較
Haskell vs. F# vs. Scala: A High-level Language Features and Parallelism
Support Comparison
Prabhat Totoo, ...
. . . . . .
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 ...
. . . . . .
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 . ar...
. . . . . .
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...
. . . . . .
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
...
. . . . . .
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 mai...
. . . . . .
時間
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 ...
. . . . . .
時間
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 ...
. . . . . .
結論
其實很多 Go, F#, Scala, 語言上的特異功能
(Interface, type inference, Higher kinded type)
Python 都辦得到,甚至可以說根本不是問題
TJW* @...
. . . . . .
結論
Go
適合 Web Service、標準演算法、多工
適合用 C 的方式思考的
不適想客製化語言的人
F# 和 Scala 差不多,看你比較討厭 { 還是 MS
適合常用 Functional Programmin...
Upcoming SlideShare
Loading in …5
×

of

[COSCUP2013] Python, F#, Golang and Friends Slide 1 [COSCUP2013] Python, F#, Golang and Friends Slide 2 [COSCUP2013] Python, F#, Golang and Friends Slide 3 [COSCUP2013] Python, F#, Golang and Friends Slide 4 [COSCUP2013] Python, F#, Golang and Friends Slide 5 [COSCUP2013] Python, F#, Golang and Friends Slide 6 [COSCUP2013] Python, F#, Golang and Friends Slide 7 [COSCUP2013] Python, F#, Golang and Friends Slide 8 [COSCUP2013] Python, F#, Golang and Friends Slide 9 [COSCUP2013] Python, F#, Golang and Friends Slide 10 [COSCUP2013] Python, F#, Golang and Friends Slide 11 [COSCUP2013] Python, F#, Golang and Friends Slide 12 [COSCUP2013] Python, F#, Golang and Friends Slide 13 [COSCUP2013] Python, F#, Golang and Friends Slide 14 [COSCUP2013] Python, F#, Golang and Friends Slide 15 [COSCUP2013] Python, F#, Golang and Friends Slide 16 [COSCUP2013] Python, F#, Golang and Friends Slide 17 [COSCUP2013] Python, F#, Golang and Friends Slide 18 [COSCUP2013] Python, F#, Golang and Friends Slide 19 [COSCUP2013] Python, F#, Golang and Friends Slide 20 [COSCUP2013] Python, F#, Golang and Friends Slide 21 [COSCUP2013] Python, F#, Golang and Friends Slide 22 [COSCUP2013] Python, F#, Golang and Friends Slide 23 [COSCUP2013] Python, F#, Golang and Friends Slide 24 [COSCUP2013] Python, F#, Golang and Friends Slide 25 [COSCUP2013] Python, F#, Golang and Friends Slide 26 [COSCUP2013] Python, F#, Golang and Friends Slide 27 [COSCUP2013] Python, F#, Golang and Friends Slide 28 [COSCUP2013] Python, F#, Golang and Friends Slide 29 [COSCUP2013] Python, F#, Golang and Friends Slide 30 [COSCUP2013] Python, F#, Golang and Friends Slide 31 [COSCUP2013] Python, F#, Golang and Friends Slide 32 [COSCUP2013] Python, F#, Golang and Friends Slide 33 [COSCUP2013] Python, F#, Golang and Friends Slide 34 [COSCUP2013] Python, F#, Golang and Friends Slide 35 [COSCUP2013] Python, F#, Golang and Friends Slide 36 [COSCUP2013] Python, F#, Golang and Friends Slide 37 [COSCUP2013] Python, F#, Golang and Friends Slide 38 [COSCUP2013] Python, F#, Golang and Friends Slide 39 [COSCUP2013] Python, F#, Golang and Friends Slide 40 [COSCUP2013] Python, F#, Golang and Friends Slide 41 [COSCUP2013] Python, F#, Golang and Friends Slide 42 [COSCUP2013] Python, F#, Golang and Friends Slide 43 [COSCUP2013] Python, F#, Golang and Friends Slide 44 [COSCUP2013] Python, F#, Golang and Friends Slide 45 [COSCUP2013] Python, F#, Golang and Friends Slide 46 [COSCUP2013] Python, F#, Golang and Friends Slide 47 [COSCUP2013] Python, F#, Golang and Friends Slide 48 [COSCUP2013] Python, F#, Golang and Friends Slide 49 [COSCUP2013] Python, F#, Golang and Friends Slide 50 [COSCUP2013] Python, F#, Golang and Friends Slide 51 [COSCUP2013] Python, F#, Golang and Friends Slide 52 [COSCUP2013] Python, F#, Golang and Friends Slide 53 [COSCUP2013] Python, F#, Golang and Friends Slide 54 [COSCUP2013] Python, F#, Golang and Friends Slide 55 [COSCUP2013] Python, F#, Golang and Friends Slide 56 [COSCUP2013] Python, F#, Golang and Friends Slide 57 [COSCUP2013] Python, F#, Golang and Friends Slide 58 [COSCUP2013] Python, F#, Golang and Friends Slide 59 [COSCUP2013] Python, F#, Golang and Friends Slide 60 [COSCUP2013] Python, F#, Golang and Friends Slide 61 [COSCUP2013] Python, F#, Golang and Friends Slide 62 [COSCUP2013] Python, F#, Golang and Friends Slide 63 [COSCUP2013] Python, F#, Golang and Friends Slide 64 [COSCUP2013] Python, F#, Golang and Friends Slide 65 [COSCUP2013] Python, F#, Golang and Friends Slide 66 [COSCUP2013] Python, F#, Golang and Friends Slide 67 [COSCUP2013] Python, F#, Golang and Friends Slide 68 [COSCUP2013] Python, F#, Golang and Friends Slide 69 [COSCUP2013] Python, F#, Golang and Friends Slide 70 [COSCUP2013] Python, F#, Golang and Friends Slide 71 [COSCUP2013] Python, F#, Golang and Friends Slide 72 [COSCUP2013] Python, F#, Golang and Friends Slide 73 [COSCUP2013] Python, F#, Golang and Friends Slide 74 [COSCUP2013] Python, F#, Golang and Friends Slide 75 [COSCUP2013] Python, F#, Golang and Friends Slide 76 [COSCUP2013] Python, F#, Golang and Friends Slide 77 [COSCUP2013] Python, F#, Golang and Friends Slide 78 [COSCUP2013] Python, F#, Golang and Friends Slide 79
Upcoming SlideShare
Embedded software with Golang
Next
Download to read offline and view in fullscreen.

5 Likes

Share

Download to read offline

[COSCUP2013] Python, F#, Golang and Friends

Download to read offline

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

[COSCUP2013] Python, F#, Golang and Friends

  1. 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. 2. . . . . . . 先講大家最關心的 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 2 / 50
  3. 3. . . . . . . 真相 TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 3 / 50
  4. 4. . . . . . . F# TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 4 / 50
  5. 5. . . . . . . Scala TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 5 / 50
  6. 6. . . . . . . Go TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 6 / 50
  7. 7. . . . . . . D TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 7 / 50
  8. 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. 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. 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. 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. 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. 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. 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. 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. 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. 17. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  18. 18. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  19. 19. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  20. 20. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  21. 21. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  22. 22. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  23. 23. . . . . . . Lessons Learned Language Matters (易讀、易除錯、易寫) 語言影響思考 * 括號對可讀性的幫助不大 排版有幫助 短程式碼可以幫助可讀性 理想的程式語言: 覺得用幾句話能說明的事情,就能用幾句話寫出來。 針對程式設計師而言 * TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 17 / 50
  24. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 43. . . . . . . 學 F# 的原因 本來就會一點 OCaml Open Source 有 list comprehension .Net 跟 Python 結合有沒有搞頭? TJW* @ COSCUP 2013 () Python, F#, Golang and Friends 2013-08-04 23 / 50
  44. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  • boosterwu

    Jun. 7, 2015
  • mrpa

    Jul. 16, 2014
  • EvansLin

    Jul. 7, 2014
  • firebits

    Jun. 28, 2014
  • elleryq

    Aug. 7, 2013

Views

Total views

3,909

On Slideshare

0

From embeds

0

Number of embeds

976

Actions

Downloads

40

Shares

0

Comments

0

Likes

5

×