$30 off During Our Annual Pro Sale. View Details »

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

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ʹແ໊ؔ਺ͷ౶ҥߏจ͍ΕͨΒͲ͏ͳΔͷʁ

    View Slide

  2. (Pͷແ໊ؔ਺
    var f = func(x int) int { return x * 2 }

    View Slide

  3. ͍ΘΏΔ"SSPX'VODUJPO͕΄͍͠
    var f = (x int) int => { return x * 2 }

    View Slide

  4. ಈతܕ෇͖ͳݴޠͰ͸ʁ
    const f = (x) => { return x * 2 }
    +BWB4DSJQU
    1ZUIPO
    f = lambda x: x * 2
    3VCZ
    f = ->(x) { x * 2 }

    View Slide

  5. ੩తܕ෇͖ͳݴޠͰ͸ʁ
    const f = (x:number): number =>
    { return x * 2 }
    5ZQF4DSJQU
    +BWB
    4DBMB
    Function f =
    (Integer x) -> { return x * 2; };
    val f: (Int) => Int = (x) =>
    { x * 2 }

    View Slide

  6. (PͰ͸͜ΜͳܗͷߏจʹͳΓͦ͏
    (name type, …) type => { function body }

    View Slide

  7. ྫ͑͹͜Μͳ;͏ʹ
    (x int) int => { return x * 2 }

    View Slide

  8. (PίϯύΠϥΛվ଄͠Α͏ʂ
    src/cmd/compile/
    https://github.com/golang/go

    View Slide

  9. ίϯύΠϥʹ͸ͭͷϑΣʔζ͕͋Δ
    1. Parsing
    2. Type-checking and AST transformations
    3. Generic SSA
    4. Generating machine code
    TSDDNEDPNQJMF3&"%.&NE

    View Slide

  10. ౶ҥߏจͷ௥ՃͳͷͰɺ1BSTFSΛมߋ
    1. Parsing
    2. Type-checking and AST transformations
    3. Generic SSA
    4. Generating machine code
    TSDDNEDPNQJMF3&"%.&NE

    View Slide

  11. (Pͷߏจղੳ
    LL(1)

    View Slide

  12. ͍͍ͤͥͻͱͭઌͷτʔΫϯ͔͠ઌಡΈ͠ͳ͍
    var f = ( x * 2 )

    current: ( : _Lparen
    next : x : _Name

    View Slide

  13. ͷ࣍ʹ໊લ͕དྷͨΒɺࣜ &YQS
    ʹܾ·Δ
    var f = ( x * 2 )

    current: ( : _Lparen
    next : x : _Name
    Expr

    View Slide

  14. "SSPX'VODUJPO͕ೖΔͱʜʜ
    var f = ( x int ) int => { … }

    current: ( : _Lparen
    next : x : _Name

    View Slide

  15. ͷ࣍ʹ໊લ͕དྷͯ΋ɺࣜ &YQS
    ͔ؔ਺ 'VOD5ZQF
    ͔
    Θ͔Βͳ͍
    var f = ( x int ) int => { … }

    current: ( : _Lparen
    next : x : _Name
    Expr or FuncType?

    View Slide

  16. Ͳ͏ʹ͔ͳΒͳ͍

    View Slide

  17. ߏจΛม͑ͯΈΑ͏
    ->(name type, …) type { function body }

    View Slide

  18. 3VCZͬΆ͘
    ͜ΕͳΒύʔεͰ͖Δͧ
    ->(x int) int => { return x * 2 }

    View Slide

  19. AGVODAΛAAʹม͑Δ͚ͩ
    ->(x int) int => { return x * 2 }
    func(x int) int => { return x * 2 }

    View Slide

  20. ॻ͍ͯΈͨ
    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(-)

    View Slide

  21. ؔ਺ͷܕ΋Ͱ͔͚ΔΑ͏ʹ͠Α͏
    var f: ->(int) int =
    ->(x int) int { return x * 2 }

    View Slide

  22. %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)
    }

    View Slide

  23. Ͱɺ࢖͑ͦ͏ʁ

    View Slide

  24. ΈΜͳେ޷͖IUUQ4FSWFS
    http.HandleFunc("/",
    ->(w http.ResponseWriter, r *http.Request) { ... })

    View Slide

  25. ΋͠(Pʹ(FOFSJDT͕ೖͬͨΒ
    var arr = [1, 2, 3]
    arr.map( ->(x int) int { return x * 2 })

    View Slide

  26. AGVODAΑΓจࣈݮͬͨ

    View Slide

  27. ͋Μ·ΓมΘΒͳ͍ͷͰ͸ʜʜ

    View Slide

  28. ͨͱ͑͹ɺ+BWBͰ͸Ҿ਺ͷܕએݴΛ
    লུͰ͖Δ

    +BWB
    Function f =
    (Integer x) -> { return x * 2; };
    Function f =
    (x) -> { return x * 2; };

    View Slide

  29. 4DBMBͰ͸ฦΓ஋ܕΛਪ࿦ͯ͘͠ΕΔ

    4DBMB
    val f: Int => Int = (x: Int) => { x * 2 }
    val f = (x: Int) => { x * 2 }

    View Slide

  30. SFUVSOΛলུͰ͖Δݴޠ΋ଟ͍

    +BWB
    Function f =
    (x) -> { return x * 2; };
    Function f =
    (x) -> { x * 2; };

    View Slide

  31. ؔ਺͕࢖ΘΕΔ৔ॴͷܕ͕Θ͔͍ͬͯΔ৔߹͸
    ؔ਺ࣗମͷҾ਺΍ฦΓ஋ΛলུͰ͖Δ
    4DBMB
    val arr = Array(1, 2)
    // Array#mapͷҾ਺͸
    // (Int) => AͳͷͰҾ਺͕Intͱਪ࿦ɺ
    // ؔ਺ຊମ͔ΒฦΓ஋͕Intͱਪ࿦
    arr.map((x) => { x * 2 })

    View Slide

  32. ແ໊Ҿ਺1MBDFIPMEFSͳͲɺ
    ΋ͬͱলུͰ͖Δ࢓ֻ͚΋ʜ
    4DBMB
    val arr = Array(1, 2)
    // ແ໊ؔ਺ͷҾ਺Λ _ Ͱड͚Δ
    arr.map(_ * 2)

    View Slide

  33. ແ໊ؔ਺Λ؆ܿ໌ྎʹॻͨ͘Ίʹ͸
    ౶ҥߏจͷଞʹ֤छͷলུه๏΋
    - Argument Type Inference
    - Return Type Inference
    - implicit return statement
    - anonymous argument placeholder

    View Slide

  34. ͦ΋ͦ΋ͦ͜·Ͱলུ͍ͨ͠

    View Slide

  35. Α͏͢Δʹ
    Conclusion

    View Slide

  36. ؾ࣋ͪΑ͘ແ໊ؔ਺Λॻͨ͘Ίʹ͸
    ߟ͑Δ͜ͱ͕ଟ͍
    - Syntax
    - Type Inference
    - etc

    View Slide

  37. (Pʹ͋ͬͨจ๏͸ͲΜͳܗ͕Α͍͔ɺ
    Ұॹʹߟ͑ͯΈ·ͤΜ͔

    View Slide