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

今更聞けない関数型プログラミングの基本

 今更聞けない関数型プログラミングの基本

プログラミング生放送勉強会 第41回@サイボウズ株式会社 松山オフィスの発表資料です。

E63c0cf4a77b1799e02f97689db83d91?s=128

kaidouji85

June 04, 2016
Tweet

More Decks by kaidouji85

Other Decks in Technology

Transcript

  1. ࠓߋฉ͚ͳ͍ؔ਺ܕ ϓϩάϥϛϯάͷجຊ 2016/06/18 ϓϩੜษڧձ@দࢁ ஛಺ɹ༎հ

  2. ࣗݾ঺հ ஛಺༎հ ࢓ࣄͰ͸javaͱjavascript࢖ͬ ͯ·͢ झຯͷήʔϜ࡞੒Ͱ΋ javascript࢖ͬͯ·͢ React + Redux࠷ߴ

  3. ϓϩੜͪΌΜϑΝϯΫϥϒʹೖΓ·ͨ͠

  4. ϓϩੜͪΌΜ੠༏ͷ্ࡔ͢ΈΕ͞Μͷ ϋϚϦ໾ͱ͍͑͹෣ઌഐͰ͢ΑͶ ʢҟ࿦͸ೝΊΔʣ

  5. ࢲͷ৬৔ʹ΋ɺ͍͢͝ઌഐ͕͍·͢ ͦͷਓ͔Βؔ਺ܕΛୟ͖ࠐ·Ε·ͨ͠

  6. ࠓ೔͸ઌഐ͔Βڭ͑ͯ΋Βͬͨ ؔ਺ܕϓϩάϥϛϯά ʹ͍ͭͯ࿩͠·͢

  7. ΞδΣϯμ 1. ؔ਺ܕϓϩάϥϛϯάͱ͸ʁ 2. TODOϦετΛؔ਺ܕͰ࡞ͬͯΈΔ 3. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ 4. ϥΠϒϓϩάϥϛϯά 5.

    ؔ਺ܕΛ͸͡ΊΔʹ͸
  8. ΞδΣϯμ 1. ؔ਺ܕϓϩάϥϛϯάͱ͸ʁ 2. TODOϦετΛؔ਺ܕͰ࡞ͬͯΈΔ 3. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ 4. ϥΠϒϓϩάϥϛϯά 5.

    ؔ਺ܕΛ͸͡ΊΔʹ͸
  9. ؔ਺ܕϓϩάϥϛϯάͬͯ ԿͳΜͰ͠ΐ͏͔ʁ

  10. ༗໊Ͳ͜Ζͩͱɺ͍ͭ͜Β

  11. JavaͩͱStream API͕༗໊Ͱ͢

  12. ؔ਺ܕݴޠ ԿΛ΋ͬͯؔ਺ܕϓϩάϥϛϯάͱ͢Δ͔ɺؔ਺ܕϓϩ άϥϛϯάΛߦ͍ͬͯΔίϛϡχςΟ಺Ͱ΋ਖ਼֬ͳఆٛ ΍߹ҙͱ͍͏΋ͷ͸ଘࡏ͠ͳ͍͕ɺҰൠతʹ͸ɺखଓ͖ ܕϓϩάϥϛϯά͕ίϚϯυ࣮ߦͷྻͱͯ͠ϓϩάϥϜ Λهड़͍ͯ͘͠ͷʹର͠ɺؔ਺ܕϓϩάϥϛϯά͸ෳ਺ ͷࣜΛؔ਺ͷద༻ʹΑͬͯ૊Έ߹Θ͍ͤͯ͘ϓϩάϥϛ ϯάελΠϧͰ͋Δɺͱ͍͏͜ͱ͸޿͘ೝΊΒΕ͍ͯΔɻ WikipediaΑΓ https:/

    /ja.wikipedia.org/wiki/ؔ਺ܕݴޠ
  13. ؔ਺ܕͱ͸ ͲΜͳσʔλ͕ཉ͍͠ͷ͔ɺ໨తΛத৺ʹॻ͘ ϓϩάϥϛϯάελΠϧ Ͳ͏΍ͬͯ໨తͱͳΔσʔλΛ࡞Δͷ͔ɺ
 ͱ͍͏͜ͱ͸جຊతʹॻ͔ͳ͍

  14. ؔ਺ܕͷϝϦοτ ෳࡶͳॲཧ͕୹͍ߦ਺Ͱॻ͚Δ ίʔυͷՄಡੑ΋্͕Δ Α͘෼͔Βͳ͍όά΋ݮΔ

  15. ΞδΣϯμ 1. ؔ਺ܕϓϩάϥϛϯάͱ͸ʁ 2. TODOϦετΛؔ਺ܕͰ࡞ͬͯΈΔ 3. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ 4. ϥΠϒϓϩάϥϛϯά 5.

    ؔ਺ܕΛ͸͡ΊΔʹ͸
  16. ྫ୊ มߋ͕͋ͬͨϨίʔυ͚ͩநग़͢Δ ϨίʔυΛอଘ༻ʹม׵͢Δ TODOϦετͷอଘػೳΛ࡞Δͱ͠·͢ɻ ཁ݅͸ҎԼͷ௨ΓͰ͢

  17. લఏ৚݅ ݴޠ͸javascript Ϩίʔυ͕มߋ͞Εͨ৔߹͸isChanged͕trueʹͳΔ σʔλม׵ʹ͸convertDataΛ࢖͏ // TODOϦετͷσʔλߏ଄ let todo = [

    {action:'ϓϩੜͷൃදࢿྉΛ࡞Δ', isChanged: false}, {action:'ϑΝϯτϜϫʔϧυΛ؍Δ', isChanged: true}, {action:'ϋοΧυʔϧΛνΣοΫ͢Δ', isChanged: true}, {action:'ח૔ʹ͓Ֆݟʹߦ͘', isChanged: false}, {action:'य़ΞχϝͰԿΛݟΔ͔ܾΊΔ', isChanged: true}, ];
  18. TODOϦετΛϧʔϓͰճ͢ API༻ʹσʔλม׵ มߋ͞Ε͔ͨ Ϧετʹ௥Ճ YES NO

  19. let postData = []; for (let i in todo) {

    if(todo[i].isChanged) { postData.push(convertData(todo[i])); } } ίʔυʹىͯ͜͠Έ·͠ΐ͏ Α͋͘Δॻ͖ํͰ͕͢ɺ໰୊͕͋Γ·͢
  20. Ͳ͕͜໰୊ͳͷ͔ •ແବʹωετ͕ਂ͍ •ม਺ͷ஋͕Կճ΋มΘΔ

  21. ؔ਺ܕͪͬ͘ʹॻ͍ͨ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); let postData = []; for (let i in todo) { if(todo[i].isChanged) { postData.push(convertData(todo[i])); } }
  22. ؔ਺ܕϓϩάϥϛϯάͷجຊతͳߟ͑ํ ഑ྻͷશཁૉʹରͯ͠ ಉ͡ૢ࡞Λ͢Δ

  23. ࠓճ࢖ͬͨૢ࡞ FILTER MAP

  24. FILTER ͋Δ৚݅Λຬͨ͢ཁૉ͚ͩΛ࢒͢

  25. MAP શͯͷཁૉΛม׵͢Δ

  26. ͜ΕͰTODOϦετͷॲཧΛॻ͘ͱ FILTER มߋ͞Εͨ΋ͷ͚ͩ࢒͢ MAP API༻ʹσʔλม׵͢Δ

  27. // FILTER มߋ͞Εͨ΋ͷ͚ͩ࢒͢ let chanegedList = R.filter(data=>data.isChanged, todo); // MAP

    API༻ʹσʔλม׵͢Δ let postData = R.map(convertData, chanegedList); ίʔυ΋̍ର̍ରԠ͍ͯ͠Δ
  28. R.filter(data=>data.isChanged, todo); FILETRͷୈҰҾ਺ʹ ݟ׳Εͳ͍΋ͷ͕͋Γ·ͤΜ͔ ͱ͜ΖͰɺ

  29. EcmaScript2015Ͱ௥Ճ͞Εͨ Ξϩʔؔ਺Ͱ͢ // Լͷೋͭ͸ಉ͡ҙຯ function(data) { return data.isChanged; } data

    => data.isChanged;
  30. FILTERͰཁૉΛ࢒͢৚݅Λࢦఆ͢Δ ୈҰҾ਺ɿཁૉͷ஋ ໭Γ஋ɿ࢒͔͢൱͔ͷ൑ఆ݁Ռ(boolean)
 true -> ࢒͢
 ɹɹɹɹfalse -> ࢒͞ͳ͍ ҎԼ࢓༷ͷؔ਺Λఆٛ͢Δ

  31. FILTERͷΠϝʔδ FILTER

  32. ؔ਺ܕϓϩάϥϛϯάͰ͸ ࡉ͔͍৚݅ɺσʔλม׵نଇΛ ؔ਺ͱͯ͠ఆٛ͢Δ

  33. let chanegedList = R.filter(data=>data.isChanged, todo); ͜͏͍͏ͷΛϥϜμࣜͱ͍͏

  34. ͪͳΈʹMAPͷϥϜμࣜͷ࢓༷ ୈҰҾ਺ɿཁૉͷ஋ ໭Γ஋ɿม׵ޙͷσʔλ

  35. MAPͷΠϝʔδ MAP

  36. ͖ͬ͞ͷྫͩͱ σʔλม׵༻ؔ਺͕༻ҙ͞Ε͍ͯΔͷͰ ͦͷ··ࢦఆ let postData = R.map(convertData, chanegedList);

  37. આ໌ʹ࢖ͬͨίʔυ͸ ϒϥ΢β্Ͱಈ͔ͤ·͢ http:/ /goo.gl/AC4bfS

  38. ΞδΣϯμ 1. ؔ਺ܕϓϩάϥϛϯάͱ͸ʁ 2. TODOϦετΛؔ਺ܕͰ࡞ͬͯΈΔ 3. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ 4. ϥΠϒϓϩάϥϛϯά 5.

    ؔ਺ܕΛ͸͡ΊΔʹ͸
  39. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ ϧʔϓɺ৚݅෼ذͷ୅ΘΓʹmapɺfilterͳͲΛ࢖͏ ϥϜμࣜ͸1ʙ2ߦఔ౓Ͱॻ͘ ม਺΁ͷ࠶୅ೖ͸ݪଇېࢭ

  40. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ ϧʔϓɺ৚݅෼ذͷ୅ΘΓʹmapɺfilterͳͲΛ࢖͏ ϥϜμࣜ͸1ʙ2ߦఔ౓Ͱॻ͘ ม਺΁ͷ࠶୅ೖ͸ݪଇېࢭ

  41. ϧʔϓͷ୅ΘΓʹmapɺfilter Λ࢖͑ͱ͸ݴ͏͚Ͳ ଞʹͲΜͳͷ͕͋Δͷʁ

  42. ଞʹ΋RamdaͷެࣜαΠτʹ৭ʑॻ͍ͯ͋Γ·͢
 http:/ /ramdajs.com/0.21.0/docs/ reduce ഑ྻΛ·ͱΊͯҰͭʹ͢Δ groupBy ഑ྻͷཁૉΛάϧʔϓʹ෼͚Δ zip ̎ͭͷ഑ྻΛ߹੒͢Δ

  43. ͜ΕΒΛ૊Έ߹ΘͤͯϩδοΫΛ࡞Δͱ ҎԼͷΑ͏ͳߟ͑ํʹͳͬͯ͘Δ ू߹͔ΒͲͷσʔλΛऔΔ͔ σʔλΛͲ͏άϧʔϓ෼͚ɺ߹੒͢Δ͔ σʔλΛͲ͏Ճ޻͢Δ͔

  44. ϓϩάϥϜͱ͍͏ΑΓ΋ SQLΛॻ͍͍ͯΔײ֮ʹ͍ۙ એݴత

  45. એݴతϓϩάϥϛϯά ୈ1ͷఆٛʹΑΕ͹ɺ͋Δग़ྗΛಘΔʹ͋ͨͬͯͦΕΛ ࡞੒͢Δํ๏Ͱ͸ͳ͘ɺग़ྗͷੑ࣭Λهड़͢Δ͜ͱΛʮએ ݴܕʯͱশ͢ΔɻҰྫΛڍ͛ΔͳΒ͹ɺʢϓϩάϥϛϯ άݴޠͰ͸ͳ͍͕ʣSQLͷΫΤϦ͸ʮͲͷΑ͏ͳσʔλ ͕ཉ͍͔͠ʯΛهड़͢Δ΋ͷͰ͋Γɺྫ͑͹۩ମతͳB ໦ͷૢ࡞ͳͲͱ͍ͬͨʮ͍͔ʹͯ͠σʔλϕʔεʹΞΫ ηε͢Δ͔ʯͱ͍ͬͨ͜ͱʹ͸ؔ༩͠ͳ͍ɻ WikipediaΑΓ https:/

    /ja.wikipedia.org/wiki/એݴܕϓϩάϥϛϯά
  46. खଓ͖ܕͰू߹ʹର͢Δ ૢ࡞Λ࣮૷͢Δ forɺifͷ૊Έ߹Θͤ ෳ਺ͷঢ়ଶม਺ ΧΦείʔυʹ

  47. ؔ਺ܕͳΒ1ߦఔ౓ͰࡁΉ ͔͠΋൚༻త ෳࡶͳॲཧΛগͳ͍ߦ਺Ͱॻ͚Δ

  48. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ ϧʔϓɺ৚݅෼ذͷ୅ΘΓʹmapɺfilterͳͲΛ࢖͏ ϥϜμࣜ͸1ʙ2ߦఔ౓Ͱॻ͘ ม਺΁ͷ࠶୅ೖ͸ݪଇېࢭ

  49. ϥϜμ͕ࣜ௕͗͢Δྫ // ؔ਺ܕݴޠͰҰԠॻ͍ͨ R.forEach(item => { if (item.isChanged) { postData.push(convertData(item))

    } }, todo); // ࠷ॳʹࣔͨ͠ѱ͍ྫ for (let i in todo) { if(todo[i].isChanged) { postData.push(convertData(todo[i])); } }
  50. ௕͍ϥϜμࣜ͸ forɺifΛ࢖ͬͯΔͷͱมΘΒͳ͍

  51. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ ϧʔϓɺ৚݅෼ذͷ୅ΘΓʹmapɺfilterͳͲΛ࢖͏ ϥϜμࣜ͸1ʙ2ߦఔ౓Ͱॻ͘ ม਺΁ͷ࠶୅ೖ͸ݪଇېࢭ

  52. mapɺfilterΛ࢖͑͹Ұ࣌ม਺Λ࢖͏ඞཁ͕ͳ͍ ม਺΁ͷ࠶୅ೖΛېࢭ͢Ε͹ঢ়ଶɺ෭࡞༻͕ݮΔ ͷͰόά͕গͳ͘ͳΔ ม਺΁ͷ࠶୅ೖېࢭͷ͜͜Ζ

  53. ཧ૝తͳؔ਺
 ೖྗ͕ಉ͡ͳΒɺ͍ͭͰ΋ಉ͡ग़ྗ ঢ়ଶ͕͋Δؔ਺
 ೖྗ͕ಉ͡Ͱ΋ɺ಺෦ঢ়ଶʹΑΓग़ྗ͕ҟͳΔ ෭࡞༻͕͋Δؔ਺
 ೖྗσʔλ͕ؔ਺಺෦Ͱॻ͖׵ΘΔ ঢ়ଶɺ෭࡞༻ʹ͍ͭͯ

  54. ঢ়ଶ: ঢ়ଶม਺ʹ࠶୅ೖ͢Δ ෭࡞༻ɿҾ਺ʹ࠶୅ೖ͢Δ ม਺΁ͷ࠶୅ೖېࢭͰ ঢ়ଶɺ෭࡞༻͕๾໓Ͱ͖Δ

  55. ঢ়ଶɺ෭࡞༻͕ͳ͍ϓϩάϥϜ͸ Α͘෼͔Βͳ͍όά͕ग़ʹ͍͘

  56. ΞδΣϯμ 1. ؔ਺ܕϓϩάϥϛϯάͱ͸ʁ 2. TODOϦετΛؔ਺ܕͰ࡞ͬͯΈΔ 3. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ 4. ϥΠϒϓϩάϥϛϯά 5.

    ؔ਺ܕΛ͸͡ΊΔʹ͸
  57. ྫ୊ ҎԼͷΑ͏ͳσʔλม׵ॲཧΛߦ͏ //ݩͷσʔλߏ଄ [ { data:’YYYY/MM/DD’, weather:’ ఱؾ’ } ]

    // ม׵ޙͷσʔλߏ଄ [ { weather:’ఱؾ’, data:[ ‘YYYY/MM/DD’ ] } ]
  58. ϩδοΫΛߟ͑Δͱ groupBy ఱؾ͝ͱʹάϧʔϓ෼͚͢Δ MAP άϧʔϓ͝ͱʹσʔλม׵͢Δ

  59. ͔͜͜Β ϥΠϒϓϩάϥϛϯά ͷελʔτʂʂ http:/ /goo.gl/C7W0fW

  60. ౴͑ͷίʔυ let createGroupData = (val, key)=>({ weather: key, date: R.map(item

    => item.date, val) }); let convertData = R.pipe( R.groupBy(item => item.weather), R.mapObjIndexed(createGroupData), R.values ); convertData(data); ಈ࡞֬ೝ͸͜͜Ͱʂ http:/ /goo.gl/qdiY2u
  61. গ͚ͩ͠ղઆ // άϧʔϓ͝ͱͷσʔλม׵ॲཧ let createGroupData = (val, key)=>({ weather: key,

    date: R.map(item => item.date, val) }); let convertData = R.pipe( // ఱؾͰάϧʔϓ෼͚ R.groupBy(item => item.weather), // άϧʔϓ͝ͱʹσʔλม׵ R.mapObjIndexed(createGroupData), // ࿈૝഑ྻ͔Βී௨ͷ഑ྻʹม׵ R.values ); convertData(data);
  62. ΞδΣϯμ 1. ؔ਺ܕϓϩάϥϛϯάͱ͸ʁ 2. TODOϦετΛؔ਺ܕͰ࡞ͬͯΈΔ 3. ؔ਺ܕϓϩάϥϛϯάࡾݪଇ 4. ϥΠϒϓϩάϥϛϯά 5.

    ؔ਺ܕΛ͸͡ΊΔʹ͸
  63. ͝ΊΜͳ͍͞ javaͱjavascript ͔͠෼͔Βͳ͍Ͱ͢

  64. Javaͷ৔߹ Java8ʹόʔδϣϯΞοϓͯ͠ StreamAPIΛ࢖͍·͠ΐ͏

  65. JavaScriptͷ৔߹ Ramda͕௒͓͢͢ΊͰ͢ http:/ /ramdajs.com/0.21.0/index.html

  66. Try RamdaͰ͙͢ʹࢼͤ·͢ http:/ /ramdajs.com/repl/?v=0.21.0 ͜ͷεϥΠυͷίʔυ΋͜ΕͰಈ͔ͤ·͢

  67. javaɺjavascriptҎ֎Ͱ΋ؔ਺ܕ ϥΠϒϥϦΛ୳ͤ͹͍͍ͱࢥ͏Α

  68. ଞݴޠ΋গ͠ௐ΂ͯΈͨ PHP
 array_mapɺarray_filterͱ͔͕ඪ४Ͱ͋Δ Ruby
 Array͕ඪ४Ͱmapɺselectͱ͔Λ࣋ͬͯΔ C++
 C++ Streamͯͷ͕͋ΔΒ͍͠
 http:/ /jscheiny.github.io/Streams/

  69. ࠓ೔ͷ·ͱΊ ؔ਺ܕϓϩάϥϜ͸ɺSQLͷΑ͏ʹએݴతʹ
 ϩδοΫΛߟ͑Δ ؔ਺ܕϓϩάϥϜ͸ϧʔϓɺ෼ذͷ୅ΘΓʹ filterɺmapͳͲΛ࢖͏ ϓϩੜͪΌΜͱ෣ઌഐ͸࠷ߴ

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