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
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
180
役立つログに取り組もう
irof
28
9.3k
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
300
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
510
AWS IaCの注目アップデート 2024年10月版
konokenj
3
3.3k
ヤプリ新卒SREの オンボーディング
masaki12
0
110
[PyCon Korea 2024 Keynote] 커뮤니티와 파이썬, 그리고 우리
beomi
0
120
色々なIaCツールを実際に触って比較してみる
iriikeita
0
310
受け取る人から提供する人になるということ
little_rubyist
0
180
Pinia Colada が実現するスマートな非同期処理
naokihaba
4
200
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
17
4.3k
WEBエンジニア向けAI活用入門
sutetotanuki
0
330
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
334
57k
Typedesign – Prime Four
hannesfritz
40
2.4k
What's in a price? How to price your products and services
michaelherold
243
12k
How to train your dragon (web standard)
notwaldorf
88
5.7k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
92
16k
Unsuck your backbone
ammeep
668
57k
Code Reviewing Like a Champion
maltzj
520
39k
It's Worth the Effort
3n
183
27k
GraphQLとの向き合い方2022年版
quramy
43
13k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
Thoughts on Productivity
jonyablonski
67
4.3k
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ʹ͋ͬͨจ๏ͲΜͳܗ͕Α͍͔ɺ Ұॹʹߟ͑ͯΈ·ͤΜ͔