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
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
340
Outline View in SwiftUI
1024jp
1
330
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
300
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
890
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Macとオーディオ再生 2024/11/02
yusukeito
0
370
Enabling DevOps and Team Topologies Through Architecture: Architecting for Fast Flow
cer
PRO
0
340
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
9
3.3k
ECS Service Connectのこれまでのアップデートと今後のRoadmapを見てみる
tkikuc
2
250
役立つログに取り組もう
irof
28
9.6k
Featured
See All Featured
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Side Projects
sachag
452
42k
Raft: Consensus for Rubyists
vanstee
136
6.6k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
0
100
Scaling GitHub
holman
458
140k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
28
2k
Ruby is Unlike a Banana
tanoku
97
11k
Large-scale JavaScript Application Architecture
addyosmani
510
110k
How GitHub (no longer) Works
holman
310
140k
Into the Great Unknown - MozCon
thekraken
32
1.5k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
A Modern Web Designer's Workflow
chriscoyier
693
190k
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ʹ͋ͬͨจ๏ͲΜͳܗ͕Α͍͔ɺ Ұॹʹߟ͑ͯΈ·ͤΜ͔