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

関数型言語と私/function-and-me

 関数型言語と私/function-and-me

2017-07-12 の(car (cdr ファンクション倶楽部)) で使用したスライドです。

color_box

July 12, 2017
Tweet

More Decks by color_box

Other Decks in Technology

Transcript

  1. ؔ਺ܕݴޠͱࢲ
    • ϑΝϯΫγϣϯ۞ָ෦
    • @color_box

    View Slide

  2. ࣗݾ঺հ
    • @color_box
    • Ӭ࿨γεςϜϚωδϝϯτ
    • ޷͖ͳؔ਺͸map

    View Slide

  3. ؔ਺ܕͷܦݧ
    • SML2ϲ݄
    • Elixir3ϲ݄

    View Slide

  4. ΞδΣϯμ
    • ࠷ॳʹ৮ͬͨؔ਺ܕݴޠ͔Βಘͨ΋ͷ
    • Elixirͷ࢓ࣄ͔Βಘͨ΋ͷ

    View Slide

  5. ࠷ॳʹ৮ͬͨؔ਺ܕݴޠ
    • SML(StandardMetaLanguage)
    • 8೥͘Β͍લ
    • ֶੜ࣌ͷ՝୊

    View Slide

  6. hd::tl
    • ͦΕ·Ͱ৮ͬͨ͜ͱͷͳ͍Ϧετͷѻ͍ํʹ
    ۤ࿑ͨ͠
    • hd::tlͰϦετͷૢ࡞Λߦ͏֓೦Λशಘ
    • ؔ਺ܕݴޠʹର͢Δҙຯෆ໌ײ

    View Slide

  7. ҙຯෆ໌ײ
    • ࠷ॳ͸ۤखҙ͕ࣝ͋ͬͨ΋ͷͷɺ՝୊Ͱ໿50
    ݸͷجຊతͳؔ਺Λ࠶࣮૷
    • ͓ͳ͡Έͷؔ਺Λ࣮૷
    • flatten,foldl,foldr,flat,map,filter,split

    View Slide

  8. ੍ݶ
    • Ұͭͷؔ਺ఆٛͷΈͰɺಗ໊ؔ਺Λ࢖Θͣʹ
    ߦ͏ͱ͍͏੍ݶ
    • جຊతͳॲཧΛ೺Ѳ͠࠶ؼॲཧʹ΋׳ΕΔ͜
    ͱ͕Ͱ͖ͨ

    View Slide

  9. جૅ஁࿉͕େࣄ
    • ॳݟͰΑ͘Θ͔Βͳ͍֓೦͕ग़͖ͯͨΒ
    • ෼͔ΔൣғͰशಘΛਐΊͯɺ܁Γฦ͢ɻ
    • ͢Δͱɺͦͷ͏ͪཧղͰ͖ΔΑ͏ʹͳΔɻ

    View Slide

  10. ༨ஊ
    • ͦͷ࣌ͷ՝୊Ͱ࡞ͬͨ΋ͷ͸ΦηϩͷAI
    • min−max๏ɺα-β๏

    View Slide

  11. AI
    • 8*8ͷϚε໨ʹରͯ͠ɺͲͷϚε͕Ͳͷఔ౓ڧ͍͔ɺͱ
    ͍͏ධՁؔ਺Λ࡞੒ɻ
    • ͋ͱ͸ɺͲͷϚε໨ʹஔ͔Ε͔ͨΛ࠶ؼతʹνΣοΫ͠
    ͍͖ͯɺ࠷ऴతʹ࠷΋ධՁͷߴ͍ɺϚε໨Λબ୒͢Δ
    • ద౰ͳධՁؔ਺Ͱ΋͋Δఔ౓ਂ͘ಡΉͱউͯͳ͍
    • ίπ͸࢛۱ϚεͷධՁΛߴ͓ͯ͘͘͜͠ͱ

    View Slide

  12. View Slide

  13. Elixirͷ࢓ࣄ͔Βಘͨ΋ͷ

    View Slide

  14. ͦ΋ͦ΋Elixirͱ͸
    • ؆୯ʹݴ͏ͱElixir͸ErlangΛॻ͖΍ͨ͘͢͠
    ΋ͷ

    View Slide

  15. ͦ΋ͦ΋Elixirͱ͸
    • Elixir͸Erlangͱಉ͡VM্Ͱಈ࡞͢Δؔ਺ܕݴޠ
    • Erlang͸whatappͳͲͷϢʔβʔ਺͕ଟ͍ΞϓϦέʔ
    γϣϯͰಈ࡞͍ͯ͠Δ
    • Erlang͸Ұൠతʹॻ͖ਏ͍ɺशख़ʹ͕͔͔࣌ؒΔͱ͍
    ͏ଆ໘͕͋Δ
    • Elixir͸ॻ͖ਏ͍ͱ͍͏໰୊Λղܾͨ͠ݴޠ

    View Slide

  16. Elixir Conf
    • 2017/4/1
    • Elixir Conf 2017
    • Elixir/ErlangͷࣄྫΛͨ͘͞Μฉ͘
    • ߴෛՙʹڧ͍ݴޠͱͷ͜ͱͰɺϩϚϯΛײ͡Δɻ
    • ͍͔ͭElixirΛ࢖ͬͯ࢓ࣄͯ͠Έ͍ͨͳͱࢥͬͨ

    View Slide

  17. whaapp
    • ͪͳΈʹ
    • whatappͷϢʔβʔ਺͸9000ສ
    • ͦΕΛ50ਓͷΤϯδχΞͰࢧ͍͑ͯΔ
    • Ұਓ͋ͨΓ180ສϢʔβʔΛࢧ͑ΕΔܭࢉ
    • ਺ࣈ͸ 2015/09

    View Slide

  18. ࢓ࣄͰElixir
    • Ҋ֎ૣͦ͘ͷΑ͏ͳػձʹܙ·ΕΔ
    • 4,5݄͸ElixirͰ࢓ࣄ

    View Slide

  19. ࢓ࣄ্Ͱಘֶͨͼ
    • ࢓ࣄͰೋϲ݄͘Β͍ElixirΛ৮ͬͨͷͰͦͷ࣌
    ͷ஌ݟΛڞ༗

    View Slide

  20. mix
    • ElixirͷϏϧυπʔϧɺґଘ؅ཧπʔϧͱͯ͠ͷଆ໘΋͋Δ
    • ElixirٴͼErlangͷϥΠϒϥϦ΋ಡΈࠐΊΔɻ
    • ґଘղܾ࣌ʹϓϩδΣΫτͰ࢖༻͢ΔϥΠϒϥϦͷίʔυ
    ΛϓϩδΣΫτ௚Լʹclone͢Δ
    • ElixirͰ࢖͑ΔϥΠϒϥϦʹ͸໌Β͔ʹϝϯς͞Εͯͳ͍΋
    ͷ΋͋ͬͨΓ͢ΔͷͰ஫ҙ͕ඞཁɻ

    View Slide

  21. ࢓ࣄͰ࢖ͬͨϥΠϒϥϦ
    • Cowboy
    • Ecto

    View Slide

  22. Cowboy
    • ϚΠΫϩαʔϏεͷҰ෦ͱͯ͠ͷAPIαʔόΛ
    ͜ΕͰ࡞੒
    • Elixir੡ͷWebFWͱͯ͠Phoenix͕༗໊͕ͩɺ
    ࠓճ͸࠾༻ͤͣ
    • ୯७ͳAPIʹPhoenix͸େ͖͗͢ΔͨΊ

    View Slide

  23. Cowboy REST Flowchart
    • CowboyΛ࢖͏ͳΒ཈͓͑ͯ͘ͱศར
    • ֤ίʔϧόοΫ͕ͲͷΑ͏ʹݺ͹ΕΔ͔͕Θ
    ͔Δ
    • https://ninenines.eu/docs/en/cowboy/1.0/
    guide/rest_flowcharts/

    View Slide

  24. View Slide

  25. Ecto
    • DB wrapper
    • DB઀ଓʹ࢖༻͠ɺύϥϝʔλόϦσʔγϣϯʹ΋࢓༷
    • ֎෦APIΛୟ͘ࡍͷϦΫΤετߏ଄ମΛEctoͰఆٛ(DB
    ΁ͷอଘ͸ߦΘͳ͍)
    • ϦΫΤετΛ࣮ࡍʹߦ͏લʹɺߏ଄ମʹηοτ͞Ε͍ͯ
    Δ஋͕ຊ౰ʹਖ਼͍͔͠ͷόϦσʔγϣϯΛߦ͍͔ͨͬͨ

    View Slide

  26. ࠔͬͨ࣌ͷࢀরઌ
    • HexDocͱ࣮૷ΛݟΕ͹͍͍ͩͨ෼͔Δɻ

    View Slide

  27. HexDoc
    • ॳΊͯݟΔϥΠϒϥϦ΍ؔ਺Λ࢖͏࣌͸·ͣ
    ͜͜ΛݟΔɻ
    • Ұ௨Γ໨Λ௨͢ͱͳΜͱͳ͘࢖͍ํ͕Θ͔
    Δɻ

    View Slide

  28. ࣮૷
    • ͦΕͰ΋Α͘Θ͔Βͳ͍࣌͸࣮૷΋ݟΔ
    • ϓϩδΣΫτͰ࢖༻͢Δίʔυ͸deps௚Լʹ
    શͯೖͬͯ͘Δ
    • ͔ͩΒͦͷؾʹͳΕ͹શ෦ಡΊΔ

    View Slide

  29. Erlang੡ϥΠϒϥϦ
    • Elixir͸ErlangͰॻ͔ΕͨϥΠϒϥϦ΋࢖༻Մೳɻ
    • Erlang੡ͷϥΠϒϥϦ΋ಡΉඞཁ͕͋Δ
    • ElixirΛ৮Δʹ͸Erlangͷ஌ࣝ͸ඞਢ
    • ޙड़ͷύλʔϯϚονΛཧղ͍ͯ͠ΔͱɺErlang
    ͷίʔυ΋ͳΜͱͳ͘ಡΊΔΑ͏ʹͳΔɻ

    View Slide

  30. σόοά࣌ͷ͋Ε͜Ε
    • iexίϚϯυͰ্ཱ͕ͪΔREPL؀ڥ͕ศར
    • iex -S mixͰϓϩδΣΫτͷίʔυΛऔΓࠐΜ
    ͰREPL؀ڥΛ্ཱͪ͛Δ͜ͱ͕Ͱ͖Δɻ
    • ࣗ࡞ͷΫϥεΛݺͼग़ͯ͠ɺ௚઀ςετՄ
    ೳɻ

    View Slide

  31. σόοά࣌ʹΑ͘࢖͏΋ͷ
    • IO.inspects()
    • IEx.pry

    View Slide

  32. IO.inspects()
    • ΦϒδΣΫτΛೖΕΕ͹ͦΕʹؔ͢Δ৘ใΛ
    ు͖ग़ͯ͘͠ΕΔ
    • printσόοά࣌ʹଟ༻

    View Slide

  33. IEx.pry
    • σόοΨ
    • εςοϓ࣮ߦ͸Ͱ͖ͳ͍
    • Ұ࣌ఀࢭͨ͠ՕॴͰ೚ҙͷΦϒδΣΫτͷத
    ਎Λ֬ೝՄೳ

    View Slide

  34. ϨϏϡʔͰ਎ʹ͚ͭͨ΋ͷ
    • ύλʔϯϚονͷॏཁੑ
    • ύλʔϯϚονͷࢦఠΛͨ͘͞Μ௖͍ͨɻ

    View Slide

  35. ύλʔϯϚονͷछྨ
    • caseΛ࢖͏λΠϓ
    • ؔ਺ͷγάωνϟͰϚονͤ͞ΔλΠϓ
    • =୯ମͰϚονͤ͞ΔλΠϓ

    View Slide

  36. caseΛ࢖͏λΠϓ
    • ࠷΋Ұൠతʁ
    • ୯Ұͷؔ਺ͷதʹଘࡏ͢Δ

    View Slide


  37. View Slide

  38. ؔ਺ͷγάωνϟͰϚονͤ͞
    ΔλΠϓ
    • ෳ਺ͷؔ਺Λఆٛͯ͠ɺݺͼग़࣌͠ʹͦΕΒ
    ͕੾ΓସΘΔ

    View Slide


  39. View Slide

  40. =୯ମͰϚονͤ͞ΔλΠϓ
    • ୅ೖͰ͸ͳ͘ύλʔϯϚον

    View Slide


  41. View Slide

  42. Մಡੑͷ޲্
    • ύλʔϯϚονΛ͏·͘࢖͏ͱՄಡੑΛ্͛
    ΕΔ

    View Slide

  43. View Slide

  44. View Slide


  45. • ؔ਺ͷҾ਺ʹରͯ͠caseʹΑΔύλʔϯϚο
    νΛ͢Δͷ͸NG
    • ؔ਺ͷγάωνϟͰϚον͢Δ
    • ͜ΕʹΑͬͯݸʑͷؔ਺͕খ͘͞ͳΓɺՄಡ
    ੑ্͕͕Δ

    View Slide

  46. ༨ஊ
    • ύλʔϯϚον͕Θ͔ΔͱErlang΋Կͱͳ͘
    ಡΊΔΑ͏ʹͳΔ(ؾ͕͢Δ)
    • ύλʔϯϚονʹ͍ͭͯ৭ʑࢦಋΛड͚ͨ
    ޙɺhex಺ͷErlangϥΠϒϥϦΛಡΜͰΈΔ
    ͱɺલΑΓ΋ಡΊΔΑ͏ʹͳ͍ͬͯΔɻ

    View Slide

  47. ࠓ೔࿩ͨ͜͠ͱ
    • ؔ਺ܕݴޠͷ࠷ॳͷนΛಥഁ͢ΔͳΒ৭Μͳ
    ؔ਺Λ࠶࣮૷͢Δͱ৭ʑΘ͔ΔΑ͏ʹͳΔ
    • Elixir։ൃ࣌ͷ͋Ε͜Ε

    View Slide