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

NDS_36_RubyCurry

 NDS_36_RubyCurry

NDS#36 の言語セッションでしゃべった内容です。

82d6167c4d14393c2e20b37a74b363c5?s=128

kasacchiful

March 15, 2014
Tweet

Transcript

  1. RubyͷCurryΛ গ͠৯΂ͯΈͨ ּݪɹ޺ʢ@kasacchifulʣ

  2. ࣗݾ঺հ • ּݪɹ޺ʢ@kasacchifulʣ • ৽ׁࢢࡏॅ • SIerۈ຿ʢΠϯϑϥ੔උ͕ଟΊʣ • Ruby͕͓ؾʹೖΓ •

    VB.NET, Javascript, SQL, PHP, PowerShell
  3. એ఻

  4. JaSST’14 Niigata • ೔࣌ɿ2014೥4݄11೔ʢۚʣ10:00 ʙ 16:50 • ձ৔ɿग࡝ϝοηɹதձٞࣨ302B • جௐߨԋɿদඌ୩

    ప ࢯʮςετͷલʹνʔϜͷݱ৔ྗΛσόοά!!ɹɹ ʙݱ৔ྗΛܭଌ͠ɼ߹ཧతͳຏ͖ํΛֶͿʙʯ • ϫʔΫγϣοϓɿ૿ా ྱࢠ ࢯʮݱ৔ྗͱνʔϜϏϧσΟϯάɹɹɹɹ ʙνʔϜΛ࡞ͬͯɼຏ͍͍ͯͨ͘Ίʹʙʯ • ਃࠐక੾ɿ2014೥4݄4೔ʢۚʣ • Webɿhttp://jasst.jp/symposium/jasst14niigata.html
  5. Ruby • 1993೥2݄24೔ੜ·Εɺ1995೥ެ։ • ΦϒδΣΫτࢦ޲ʢ͢΂͕ͯΦϒδΣΫτʣ • ڧ͍ಈతܕ෇͚ • ެࣜϖʔδɿhttps://www.ruby-lang.org/ja/

  6. Ruby ͷ Curry ͬͯԿʁ

  7. Ruby ͷ Curry hoge = -> (x,y,z) { x +

    y * z } ## hoge = lambda {|x,y,z| x + y * z} ͱಉ͡ curry_hoge = hoge.curry ! hoge.call(1,2,3) #=> 7 hoge[2,3,4] #=> 14 ! curry_hoge.call(1).call(2).call(3) #=> 7 curry_hoge[2][3][4] #=> 14
  8. ͦ΋ͦ΋ Curry ͬͯԿʁ

  9. Curry ʮΧϦʔԽʯͷ͜ͱ ΧϦʔԽ (currying) ͱ͸ɺܭࢉػՊֶ෼໺ͷٕ๏ͷҰͭɻ ෳ਺ͷҾ਺ΛͱΔؔ਺ΛɺҾ਺͕ʮ΋ͱͷؔ਺ͷ࠷ॳͷ Ҿ਺ʯͰ໭Γ஋͕ʮ΋ͱͷؔ਺ͷ࢒ΓͷҾ਺ΛऔΓ݁Ռ Λฦؔ͢਺ʯͰ͋ΔΑ͏ͳؔ਺ʹ͢Δ͜ͱɻ ʢग़యɿʮΧϦʔԽ -

    Wikipediaʯʣ http://ja.wikipedia.org/wiki/%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96
  10. Ruby ͷ Curry hoge = ->(x,y,z) { x + y

    * z } curry_hoge = hoge.curry ## curry_hoge = ->(x) { ->(y) { ->(z) { x + y * z }}} ͱಉ͡ ! ɾProc#curry ͸ Ruby 1.9 ͔Β࢖͑Δ
  11. Curry ͓͍͍ͬͯ͠ͷʁ

  12. ͓͍͍͖ͨͩ͘͠·͠ΐ͏ ΧϦʔԽ͢Δ͜ͱͰɺطଘͷؔ਺ʹҾ਺ͷ஋Λ෦෼ద༻͠ ͯɺผͷؔ਺Λ࡞Δ͜ͱ͕ग़དྷ·͢ɻ ! طଘͷؔ਺ͷ࠶ར༻ɹɹɹɹɹʮDRYʯɹ ! ͏·͘࢖͑͹ɺ͓͍͘͠ͳΓ·͢ɻ

  13. Wikipedia ͷྫΛ Ruby Ͱ div = ->(x,y) { x /

    y } ## currying cdiv = div.curry #=> ->(x) {->(y) { x/ y }} ## partial application inv = cdiv[1.0] #=> ->(y) { 1.0 / y } inv[3] #=> 0.3333333333333333 inv[10] #=> 0.1
  14. ஫ҙ ʮΧϦʔԽʯͱʮ෦෼ద༻ʯΛؒҧ͑ͳ͍Α͏ʹ ! ΧϦʔԽɿ->(x,y){x/y} Λ ->(x){->(y){x/y}} ʹม׵͢Δ͜ͱ ෦෼ద༻ɿҰ෦ͷҾ਺ʹ஋Λద༻ͯ͠ɺผͷؔ਺Λͭ͘Δ ͜ͱ

  15. ଞݴޠͰͷ Curry • ୈҰڃؔ਺Λѻ͑ΔݴޠͰ͋Ε͹ɺΧϦʔԽΛ࣮ࢪ͢Δ ؔ਺Λ࣮૷Մೳɻ • ྫɿLISP, Scheme, F#, Scala,

    Erlang, Eiffel, Perl, Python, Rݴޠ, Sݴޠ, JavaScript • ϥΠϒϥϦͰɺΧϦʔԽؔ਺Λ࢖͑Δݴޠ΋͋Δ • Groovy ͷ curry ͸෦෼ద༻ͳͷͰ஫ҙ ग़యɿʮΧϦʔԽ - Wikipediaʯ http://ja.wikipedia.org/wiki/%E3%82%AB%E3%83%AA%E3%83%BC%E5%8C%96
  16. Curry Λ࢖ͬͯΈΔ

  17. FizzBuzzͯ͠ΈΔ

  18. ·ͱΊ

  19. ͓͍͍͠CurryΛ࡞ͬͯ ͓͍͍͖ͨͩ͘͠·͠ΐ͏

  20. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠