Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥
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
React への依存を最小にするフロントエンド設計
takonda
21
8.5k
HTTP compression in PHP and Symfony apps
dunglas
1
370
Leverage LLMs in Java with LangChain4j and Quarkus
hollycummins
0
120
PaaSとSaaSの境目で信頼性と開発速度を両立する 〜TROCCO®︎のこれまでとこれから〜
gtnao
5
5.8k
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
610
似たもの同士のPerlとPHP
uzulla
1
110
N.E.X.T LEVEL
pluu
2
230
Vapor Revolution
kazupon
2
2.4k
Discord Bot with AI -for English learners-
xin9le
0
110
Modular Monolith Monorepo ~シンプルさを保ちながらmonorepoのメリットを最大化する~
yuisakamoto
10
3.7k
[KR] Open-Source Ecosystems
skydoves
0
110
AWS AppSyncを用いた GraphQL APIの開発について - NIFTY Tech Talk #22
niftycorp
PRO
0
110
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.2k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Being A Developer After 40
akosma
87
590k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Practical Orchestrator
shlominoach
186
10k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
GitHub's CSS Performance
jonrohan
1030
460k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
27
4.3k
Building a Scalable Design System with Sketch
lauravandoore
459
33k
Thoughts on Productivity
jonyablonski
67
4.3k
What's new in Ruby 2.0
geeforr
343
31k
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ʹ͋ͬͨจ๏ͲΜͳܗ͕Α͍͔ɺ Ұॹʹߟ͑ͯΈ·ͤΜ͔