Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Goに無名関数の糖衣構文いれたらどうなるの? / Considering the introd...
Search
yuroyoro
October 28, 2019
Programming
4
3.1k
Goに無名関数の糖衣構文いれたらどうなるの? / Considering the introduction of the anonymous function syntax sugar into Go
GoCon 2019 LT
yuroyoro
October 28, 2019
Tweet
Share
More Decks by yuroyoro
See All by yuroyoro
関数の話
yuroyoro
20
17k
Scala で 作る奇妙なプログラミング言語??
yuroyoro
4
16k
Other Decks in Programming
See All in Programming
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
500
開発効率向上のためのリファクタリングの一歩目の選択肢 ~コード分割~ / JJUG CCC 2024 Fall
ryounasso
0
420
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
340
Tauriでネイティブアプリを作りたい
tsucchinoko
0
350
C#/.NETのこれまでのふりかえり
tomokusaba
1
180
役立つログに取り組もう
irof
28
9.4k
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
280
RailsのPull requestsのレビューの時に私が考えていること
yahonda
5
2.8k
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
160
デプロイを任されたので、教わった通りにデプロイしたら障害になった件 ~俺のやらかしを越えてゆけ~
techouse
53
34k
From Subtype Polymorphism To Typeclass-based Ad hoc Polymorphism- An Example
philipschwarz
PRO
0
190
Ethereum_.pdf
nekomatu
0
370
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
297
20k
Git: the NoSQL Database
bkeepers
PRO
427
64k
A Modern Web Designer's Workflow
chriscoyier
693
190k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
What's new in Ruby 2.0
geeforr
343
31k
Writing Fast Ruby
sferik
627
61k
Intergalactic Javascript Robots from Outer Space
tanoku
268
27k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.2k
Fashionably flexible responsive web design (full day workshop)
malarkey
404
65k
Transcript
Considering the introduction of the anonymous function syntax sugar into
Go !ZVSPZPSP (Pʹແ໊ؔͷҥߏจ͍ΕͨΒͲ͏ͳΔͷʁ
(Pͷແ໊ؔ var f = func(x int) int { return x
* 2 }
͍ΘΏΔ"SSPX'VODUJPO͕΄͍͠ var f = (x int) int => { return
x * 2 }
ಈతܕ͖ͳݴޠͰʁ const f = (x) => { return x *
2 } +BWB4DSJQU 1ZUIPO f = lambda x: x * 2 3VCZ f = ->(x) { x * 2 }
੩తܕ͖ͳݴޠͰʁ const f = (x:number): number => { return x
* 2 } 5ZQF4DSJQU +BWB 4DBMB Function<Integer, Integer> f = (Integer x) -> { return x * 2; }; val f: (Int) => Int = (x) => { x * 2 }
(PͰ͜ΜͳܗͷߏจʹͳΓͦ͏ (name type, …) type => { function body }
ྫ͑͜Μͳ;͏ʹ (x int) int => { return x * 2
}
(PίϯύΠϥΛվ͠Α͏ʂ src/cmd/compile/ https://github.com/golang/go
ίϯύΠϥʹͭͷϑΣʔζ͕͋Δ 1. Parsing 2. Type-checking and AST transformations 3. Generic
SSA 4. Generating machine code TSDDNEDPNQJMF3&"%.&NE
ҥߏจͷՃͳͷͰɺ1BSTFSΛมߋ 1. Parsing 2. Type-checking and AST transformations 3. Generic
SSA 4. Generating machine code TSDDNEDPNQJMF3&"%.&NE
(Pͷߏจղੳ LL(1)
͍͍ͤͥͻͱͭઌͷτʔΫϯ͔͠ઌಡΈ͠ͳ͍ var f = ( x * 2 ) ↑
current: ( : _Lparen next : x : _Name
ͷ࣍ʹ໊લ͕དྷͨΒɺࣜ &YQS ʹܾ·Δ var f = ( x * 2
) ↑ current: ( : _Lparen next : x : _Name Expr
"SSPX'VODUJPO͕ೖΔͱʜʜ var f = ( x int ) int =>
{ … } ↑ current: ( : _Lparen next : x : _Name
ͷ࣍ʹ໊લ͕དྷͯɺࣜ &YQS ͔ؔ 'VOD5ZQF ͔ Θ͔Βͳ͍ var f = (
x int ) int => { … } ↑ current: ( : _Lparen next : x : _Name Expr or FuncType?
Ͳ͏ʹ͔ͳΒͳ͍
ߏจΛม͑ͯΈΑ͏ ->(name type, …) type { function body }
3VCZͬΆ͘ ͜ΕͳΒύʔεͰ͖Δͧ ->(x int) int => { return x *
2 }
AGVODAΛAAʹม͑Δ͚ͩ ->(x int) int => { return x * 2
} func(x int) int => { return x * 2 } ↓
ॻ͍ͯΈͨ src/cmd/compile/internal/syntax/parser.go | 2 +- src/cmd/compile/internal/syntax/scanner.go | 7 ++++++- src/cmd/compile/internal/syntax/token_string.go
| 4 ++-- src/cmd/compile/internal/syntax/tokens.go | 1 + 4 files changed, 10 insertions(+), 4 deletions(-)
ؔͷܕͰ͔͚ΔΑ͏ʹ͠Α͏ var f: ->(int) int = ->(x int) int {
return x * 2 }
%FNP package main import "fmt" func main() { f :=
->(x int, y int) int { return x + y } fmt.Printf("%d\n", f(3, 4)) fmt.Printf("%d", higherFunc(f)) } func higherFunc(f ->(x int, y int) int) int { return f(1, 2) }
Ͱɺ͑ͦ͏ʁ
ΈΜͳେ͖IUUQ4FSWFS http.HandleFunc("/", ->(w http.ResponseWriter, r *http.Request) { ... })
͠(Pʹ(FOFSJDT͕ೖͬͨΒ var arr = [1, 2, 3] arr.map( ->(x int)
int { return x * 2 })
AGVODAΑΓจࣈݮͬͨ
͋Μ·ΓมΘΒͳ͍ͷͰʜʜ
ͨͱ͑ɺ+BWBͰҾͷܕએݴΛ লུͰ͖Δ ↓ +BWB Function<Integer, Integer> f = (Integer x)
-> { return x * 2; }; Function<Integer, Integer> f = (x) -> { return x * 2; };
4DBMBͰฦΓܕΛਪͯ͘͠ΕΔ ↓ 4DBMB val f: Int => Int = (x:
Int) => { x * 2 } val f = (x: Int) => { x * 2 }
SFUVSOΛলུͰ͖Δݴޠଟ͍ ↓ +BWB Function<Integer, Integer> f = (x) -> {
return x * 2; }; Function<Integer, Integer> f = (x) -> { x * 2; };
͕ؔΘΕΔॴͷܕ͕Θ͔͍ͬͯΔ߹ ؔࣗମͷҾฦΓΛলུͰ͖Δ 4DBMB val arr = Array(1, 2) // Array<Int>#mapͷҾ
// (Int) => AͳͷͰҾ͕Intͱਪɺ // ؔຊମ͔ΒฦΓ͕Intͱਪ arr.map((x) => { x * 2 })
ແ໊Ҿ1MBDFIPMEFSͳͲɺ ͬͱলུͰ͖Δֻ͚ʜ 4DBMB val arr = Array(1, 2) // ແ໊ؔͷҾΛ
_ Ͱड͚Δ arr.map(_ * 2)
ແ໊ؔΛ؆ܿ໌ྎʹॻͨ͘Ίʹ ҥߏจͷଞʹ֤छͷলུه๏ - Argument Type Inference - Return Type Inference
- implicit return statement - anonymous argument placeholder
ͦͦͦ͜·Ͱলུ͍ͨ͠
Α͏͢Δʹ Conclusion
ؾ࣋ͪΑ͘ແ໊ؔΛॻͨ͘Ίʹ ߟ͑Δ͜ͱ͕ଟ͍ - Syntax - Type Inference - etc
(Pʹ͋ͬͨจ๏ͲΜͳܗ͕Α͍͔ɺ Ұॹʹߟ͑ͯΈ·ͤΜ͔