Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

Goに無名関数の糖衣構文いれたらどうなるの? / Considering the introd...

yuroyoro
October 28, 2019

Goに無名関数の糖衣構文いれたらどうなるの? / Considering the introduction of the anonymous function syntax sugar into Go

GoCon 2019 LT

yuroyoro

October 28, 2019
Tweet

More Decks by yuroyoro

Other Decks in Programming

Transcript

  1. Considering the introduction of the anonymous function syntax sugar into

    Go !ZVSPZPSP (Pʹແ໊ؔ਺ͷ౶ҥߏจ͍ΕͨΒͲ͏ͳΔͷʁ
  2. ಈతܕ෇͖ͳݴޠͰ͸ʁ const f = (x) => { return x *

    2 } +BWB4DSJQU 1ZUIPO f = lambda x: x * 2 3VCZ f = ->(x) { x * 2 }
  3. ੩తܕ෇͖ͳݴޠͰ͸ʁ 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 }
  4. ͷ࣍ʹ໊લ͕དྷͨΒɺࣜ &YQS ʹܾ·Δ var f = ( x * 2

    ) ↑ current: ( : _Lparen next : x : _Name Expr
  5. "SSPX'VODUJPO͕ೖΔͱʜʜ var f = ( x int ) int =>

    { … } ↑ current: ( : _Lparen next : x : _Name
  6. ͷ࣍ʹ໊લ͕དྷͯ΋ɺࣜ &YQS ͔ؔ਺ 'VOD5ZQF ͔ Θ͔Βͳ͍ var f = (

    x int ) int => { … } ↑ current: ( : _Lparen next : x : _Name Expr or FuncType?
  7. AGVODAΛAAʹม͑Δ͚ͩ ->(x int) int => { return x * 2

    } func(x int) int => { return x * 2 } ↓
  8. ॻ͍ͯΈͨ 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(-)
  9. %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) }
  10. ͨͱ͑͹ɺ+BWBͰ͸Ҿ਺ͷܕએݴΛ লུͰ͖Δ ↓ +BWB Function<Integer, Integer> f = (Integer x)

    -> { return x * 2; }; Function<Integer, Integer> f = (x) -> { return x * 2; };
  11. 4DBMBͰ͸ฦΓ஋ܕΛਪ࿦ͯ͘͠ΕΔ ↓ 4DBMB val f: Int => Int = (x:

    Int) => { x * 2 } val f = (x: Int) => { x * 2 }
  12. SFUVSOΛলུͰ͖Δݴޠ΋ଟ͍ ↓ +BWB Function<Integer, Integer> f = (x) -> {

    return x * 2; }; Function<Integer, Integer> f = (x) -> { x * 2; };
  13. ؔ਺͕࢖ΘΕΔ৔ॴͷܕ͕Θ͔͍ͬͯΔ৔߹͸ ؔ਺ࣗମͷҾ਺΍ฦΓ஋ΛলུͰ͖Δ 4DBMB val arr = Array(1, 2) // Array<Int>#mapͷҾ਺͸

    // (Int) => AͳͷͰҾ਺͕Intͱਪ࿦ɺ // ؔ਺ຊମ͔ΒฦΓ஋͕Intͱਪ࿦ arr.map((x) => { x * 2 })