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
Zoneless Testing
rainerhahnekamp
0
120
短期間での新規プロダクト開発における「コスパの良い」Goのテスト戦略」 / kamakura.go
n3xem
2
160
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
180
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
macOS なしで iOS アプリを開発する(※ただし xxx に限る)
mitsuharu
1
180
range over funcの使い道と非同期N+1リゾルバーの夢 / about a range over func
mackee
0
100
layerx_20241129.pdf
kyoheig3
2
280
CSC305 Lecture 26
javiergs
PRO
0
130
Haze - Real time background blurring
chrisbanes
1
500
複雑な仕様に立ち向かうアーキテクチャ
myohei
0
170
eBPF Deep Dive: Architecture and Safety Mechanisms
takehaya
12
1.4k
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
Featured
See All Featured
XXLCSS - How to scale CSS and keep your sanity
sugarenia
247
1.3M
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
111
49k
Bash Introduction
62gerente
608
210k
The Cost Of JavaScript in 2023
addyosmani
45
7k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.1k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
The Cult of Friendly URLs
andyhume
78
6.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
Being A Developer After 40
akosma
87
590k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
GitHub's CSS Performance
jonrohan
1030
460k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
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ʹ͋ͬͨจ๏ͲΜͳܗ͕Α͍͔ɺ Ұॹʹߟ͑ͯΈ·ͤΜ͔