Upgrade to Pro — share decks privately, control downloads, hide ads and more …

内包表記(仮)

 内包表記(仮)

Tomohiko Himura

November 02, 2014
Tweet

More Decks by Tomohiko Himura

Other Decks in Programming

Transcript

  1. ಺แදه(Ծ) 2014-11-01 ͍͢͝߹ಉษڧձ

  2. ͻΉΒ ͱ΋ͻ͜ GitHub eiel Hiλoshima Hiroshima.rb ޿ౡEmacsษڧձ http://nemca.net/

  3. 10݄ͷπΠʔτ https://twitter.com/eielh/status/517681959837761536

  4. ࠓ೔͸໨৽͍͜͠ͱ͸஻Βͳ͍

  5. ಺แදه

  6. ಺แදه ͱݺ͹ΕΔ΋ͷ͕͋ΔΒ͍͠ݴޠ • Python • Erlang • Scala • JavaScript

    1.7 • Haskell
  7. ྫ ۝۝Ͱ݁Ռ͕24ʹͳΔ΋ͷ

  8. Python [(x,y) for x in range(1,10) for y in range(1,10)

    if x * y == 24 ] [(3, 8), (4, 6), (6, 4), (8, 3)]
  9. ֎Ԇͱ಺แ

  10. (ͪΐͬͱ਺ֶ) ू߹ͷهड़ํ๏

  11. ೋͭͷه๏ ֎Ԇతه๏(extension definition) ۩ମతʹॻ͘ ಺แతه๏(intensional definition) ੑ࣭Λॻ͘

  12. ۩ମྫ 10ະຬͷਖ਼ͷح਺ ֎Ԇ { 1, 3, 5, 7, 9} ಺แ

    { x | x ͸ 10ະຬͷਖ਼ͷح਺ } { x | x ∈ ℕ, x < 10, x % 2 = 1 }
  13. ҰͭҰͭฒ΂ͨ͘ͳ͍

  14. ͳΜΒ͔ͷ๏ଇ͕͋Δ

  15. ಺แදهΛ࢖͏

  16. Haskell 10ະຬͷਖ਼ͷح਺ [ x | x <- [1..9], x `mod`

    2 == 1 ]
  17. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  18. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  19. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  20. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  21. ࣅͯΔʁ ू߹ { x | x ∈ ℕ, x <

    10, x % 2 = 1 } Haskell [ x | x <- [1..9], x `mod` 2 == 1 ]
  22. ∈ <-

  23. ऄ଍ ӳޠͩͱ Ϧετͷ಺แදه list comprehension ಺แతه๏ intensional definition

  24. ಺แදهͷಡΈํ

  25. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)]
  26. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ
  27. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ ݸͷϦετ
  28. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ ݸͷϦετ ௚ੵ
  29. ௚ੵ 81ݸͷϦετ [(x,y) | x <- [1..9], y <- [1..9]]

    [(1,1),(1,2),(1,3),(1,4),(1,5),(1,6),(1,7),(1,8),(1,9), (2,1),(2,2),(2,3),(2,4),(2,5),(2,6),(2,7),(2,8),(2,9), (3,1),(3,2),(3,3),(3,4),(3,5),(3,6),(3,7),(3,8),(3,9), (4,1),(4,2),(4,3),(4,4),(4,5),(4,6),(4,7),(4,8),(4,9), (5,1),(5,2),(5,3),(5,4),(5,5),(5,6),(5,7),(5,8),(5,9), (6,1),(6,2),(6,3),(6,4),(6,5),(6,6),(6,7),(6,8),(6,9), (7,1),(7,2),(7,3),(7,4),(7,5),(7,6),(7,7),(7,8),(7,9), (8,1),(8,2),(8,3),(8,4),(8,5),(8,6),(8,7),(8,8),(8,9), (9,1),(9,2),(9,3),(9,4),(9,5),(9,6),(9,7),(9,8),(9,9)]
  30. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ ݸͷϦετ ௚ੵ ʹͳΔ΋ͷ͚ͩʹ͢Δ
  31. Python [(x,y) for x in range(1,10) for y in range(1,10)

    if x * y == 24 ] [(3, 8), (4, 6), (6, 4), (8, 3)] ݸͷϦετ ݸͷϦετ ௚ੵ ʹͳΔ΋ͷ͚ͩʹ͢Δ
  32. ಺แදهͬͯศརͳΜʁ

  33. –Python νϡʔτϦΞϧ “Ϧετͷ಺แදه (list comprehension) ͸ɺ map() ΍ filter() ΍

    lambda Λ࢖ΘͣʹϦετ Λੜ੒͢ΔͨΊͷ؆ܿͳํ๏Λఏڙ͍ͯ͠· ͢ɻ಺แදهʹΑΔϦετͷఆٛ͸ɺ͍ͨͯ ͍ map(), filter(), lambda Λ࢖ͬͯϦετΛੜ ੒͢ΔίʔυΑΓ΋໌շʹͳΓ·͢ɻ”
  34. Ruby [*1..9].map do |x| [*1..9].map do |y| [x,y] end end.flatten(1).select

    do |x,y| x * y == 24 end
  35. ͋ͬ…͏Μ

  36. 3ͭͷੵ͕24

  37. Haskell [(x,y,z) | x <- [1..9], y <- [1..9], z

    <- [1..9], x * y * z == 24]
  38. Ruby [*1..9].map do |x| [*1..9].map do |y| [*1..9].map do |z|

    [x,y,z] end end end.flatten(2).select do |x,y,z| x * y * z == 24 end
  39. ࣼΊʹ৳ͼΔ… ͳʹ͜Επϥ͍

  40. ऄ଍ ࣼΊʹ৳ͼΔίʔυ ඇಉظॲཧ nodejs ίʔϧόοΫ஍ࠈ

  41. map Ͱ΍Ζ͏ͱ͢Δ͔Β ͓͔͘͠ͳΔ

  42. ௚ੵ͢Ε͹͍͍

  43. Ruby [*1..9] .product([*1..9],[*1..9]) .select do |x,y,z| x * y *

    z == 24 end
  44. ͜ΕͳΒ࢖͑Δ

  45. ू߹ͱ͍͑͹

  46. σʔλϕʔε

  47. ಺แදهͱSQL

  48. in PosgreSQL SELECT x,y FROM generate_series(1,9) AS X, generate_series(1,9) AS

    Y WHERE x * y = 24;
  49. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24]
  50. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] TFMFDU
  51. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] TFMFDU GSPN
  52. Haskell [(x,y) | x <- [1..9], y <- [1..9], x

    * y == 24] TFMFDU GSPN XIFSF
  53. ͦͬ͘Γ

  54. ΋͏಺แදهා͘ͳ͍

  55. SQLͬΆ͔ͭ͑͘Δ?

  56. ҰൠԽ(SQL෩) Ϧετ಺แදه

  57. LIMIT :set -XTransformListComp [ (x,y) | x <- [1..9], y

    <- [1..9], x * y == 24, then take 2] [(3, 8), (4, 6)]
  58. ORDER :set -XTransformListComp :m GHC.Exts [ (x,y) | x <-

    [1..9], y <- [1..9], x * y == 24, then sortWith by y] [(8, 3) (6, 4), (4, 6), (3, 8)]
  59. GROUPͱ͔লུ

  60. ͱ͜ΖͰ Ϟφυ

  61. ϞφυͰ΋Ͱ͖ΔΑ do x <- [1..9] y <- [1..9] guard (x

    * y == 24) return (x,y) [(3, 8), (4, 6), (6, 4), (8, 3)]
  62. ϞφυͰ΋Ͱ͖ΔΑ do x <- [1..9] y <- [1..9] guard (x

    * y == 24) return (x,y) TFMFDU [(3, 8), (4, 6), (6, 4), (8, 3)]
  63. ϞφυͰ΋Ͱ͖ΔΑ do x <- [1..9] y <- [1..9] guard (x

    * y == 24) return (x,y) GSPN TFMFDU [(3, 8), (4, 6), (6, 4), (8, 3)]
  64. ϞφυͰ΋Ͱ͖ΔΑ do x <- [1..9] y <- [1..9] guard (x

    * y == 24) return (x,y) XIFSF GSPN TFMFDU [(3, 8), (4, 6), (6, 4), (8, 3)]
  65. ͦͬ͘ΓͰ͢Ͷ

  66. ϞφυͳΒ ϦετҎ֎΋͋ΔΑͶ

  67. Maybe do x <- Just 3 y <- Just 8

    guard (x * y == 24) return (x,y) Just (3,8)
  68. Maybe do x <- Just 3 y <- Just 8

    guard (x * y == 25) return (x,y) Nothing
  69. Maybeͷ಺แදه

  70. Maybeͷ಺แදه :set -XMonadComprehensions [ (x,y) | x <- Just 3,

    y <- Just 8, x * y == 24]
  71. Ϟφυͷ಺แදه

  72. ·ͱΊ

  73. ·ͱΊ ֎Ԇ - શ෦ฒ΂Δ ಺แ - ੑ࣭Ͱදݱ ಺แදهͱSQL͕ࣅͯΔͬΆ͍ ϦετҎ֎ͷ಺แදه͕͋Δ

  74. Ϟφυා͘ͳͦ͞͏

  75. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͝͞·ͨ͠