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

Ramdaで手軽に関数型言語を始めよう

 Ramdaで手軽に関数型言語を始めよう

初夏のJavaScript祭2016のLTスライドです。

E63c0cf4a77b1799e02f97689db83d91?s=128

kaidouji85

May 03, 2016
Tweet

More Decks by kaidouji85

Other Decks in Technology

Transcript

  1. RamdaͰखܰʹ
 ؔ਺ܕݴޠΛ࢝ΊΑ͏ ஛಺ɹ༎հ 2016/05/14 ॳՆͷJavaScriptࡇ

  2. ࣗݾ঺հ ஛಺༎հ ϓϩάϥϚʔ ؔ਺ܕݴޠΛษڧத ࠓظͷՇ͸ϠϞτ͞Μ ͸͍;Γͩͱ؋௕ਪ͠ twitter @pegass85 facebook yuusuke.takeuchi.96

  3. ૣ଎Ͱ͕͢Έͳ͞Μ javascriptͷϧʔϓॲཧʹ ࠔ͍ͬͯ·ͤΜ͔

  4. ͦͷ໰୊ɺؔ਺ܕݴޠͷߟ͑ํΛ ஌͍ͬͯΕ͹ղܾͰ͖Δ͔΋

  5. ࠓ೔ͷΞδΣϯμ खଓ͖ܕ -> ؔ਺ܕ΁ ؔ਺ܕݴޠͷجຊతͳߟ͑ํ ࣮ྫ υΩϡϝϯτྨͳͲ

  6. ࠓ೔ͷΞδΣϯμ खଓ͖ܕ -> ؔ਺ܕ΁ ؔ਺ܕݴޠͷجຊతͳߟ͑ํ ࣮ྫ υΩϡϝϯτྨͳͲ

  7. ͜ͷॲཧΛͲ͏ॻ͖·͔͢ʁ TODOϦετ͕͋ͬͨͱ͠·͢ ϦετͰมߋ͕͋ͬͨ΋ͷ͚ͩΛऔΓग़͢ ˢͷσʔλΛAPIʹ౤͛ΔͨΊʹมߋ͢Δ

  8. forΛ࢖ͬͨ΍Γํ let postData = []; for (let i in todo)

    { if(todo[i].isChanged) { postData.push(convertData(todo[i])); } }
  9. Ͳ͕͜໰୊ͳͷ͔ʁ ωετ͕ਂͯ͘ෳࡶ ॲཧΛશ෦ݟͳ͍ͱpostData͕Ͳ͏ͳ͔
 ෼͔Βͳ͍ ͜Ε͕30ߦ͘Β͍ʹͳͬͯɺωετ͕3ஈʹͳ Δͱखʹෛ͑ͳ͍ɾɾɾɾɾɾ

  10. ͦ͜ͰRamdaͷग़൪ʂʂ

  11. RamdaΛ࢖ͬͯಉ͡ॲཧΛॻ͘ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ωετ͕ਂ͘ͳͯ͘γϯϓϧʹ ͔͠΋ɺม਺ͷ஋͸୅ೖ͔ͯ͠Β
 มΘͬͯͳ͍
  12. ࠓ೔ͷΞδΣϯμ खଓ͖ܕ -> ؔ਺ܕ΁ ؔ਺ܕݴޠͷجຊతͳ໋ྩ ࣮ྫ υΩϡϝϯτྨͳͲ

  13. ؔ਺ܕݴޠͷେݪଇ ഑ྻͷશཁૉʹ ಉ͡ॲཧΛ͢Δ

  14. جຊͦͷ̍ શཁૉΛม׵͢Δ MAP

  15. جຊͦͷ2 ৚݅ʹ߹͏ཁૉ͚ͩ࢒͢ FILTER

  16. ͖ͬ͞ͷ՝୊Λؔ਺ܕͰߟ͑Δͱ FILTER มߋͨ͠Ϩίʔυ͚ͩ࢒͢ MAP ࢒ͬͨ΋ͷΛ API༻ʹσʔλม׵ ɾɾɾͱͳΔ

  17. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); filter มߋͨ͠ίʔυ͚ͩ࢒͢
  18. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ࢒͢৚݅͸ɺؔ਺ͱͯ͠ఆٛ͢Δ
  19. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ͜͏͍͏ͷΛؔ਺ܕݴޠͰ͸ϥϜμࣜͱ͍͏
  20. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ؔ਺ͷϑΥʔϚοτ͸ҎԼͷ௨Γ ୈҰҾ਺ɿɹ഑ྻཁૉͷ஋ ໭Γ஋ɹɿɹboolean(trueͰ࢒͢)
  21. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ͜ͷྫͰ͸isChanged͕trueͳΒ มߋ͞Εͨͱݟͳ͍ͯ͠Δ ɹˣ มߋ͞Εͨཁૉ͚ͩΛ࢒͢ͱ͍͏ҙຯʹͳΔ
  22. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ͜͜ʹFILTERର৅ͷ഑ྻΛࢦఆ͢Δ
  23. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); MAP API༻ʹσʔλม׵
  24. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ͜͜ʹσʔλม׵͢Δؔ਺Λࢦఆ͢Δ
  25. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ؔ਺ͷϑΥʔϚοτ͸ҎԼͷ௨Γ ୈҰҾ਺ɿɹ഑ྻཁૉͷ஋ ໭Γ஋ɹɿɹม׵ޙͷσʔλ
  26. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ͜ͷྫͰ͸convertDataͰ σʔλม׵͢ΔͷͰɺ ͜ΕΛͦͷ··ࢦఆ͢Δ
  27. ίʔυͱͷରԠ let chanegedList = R.filter(data=>data.isChanged, todo); let postData = R.map(convertData,

    chanegedList); ͜͜ʹMAPର৅ͷ഑ྻΛࢦఆ͢Δ
  28. ͜Μͳײ͡Ͱ MAPɺFILTERͳͲͷૢ࡞Λ ૊Έ߹ΘͤͯॲஔΛॻ͍͍ͯ͘ ωετΛਂ͘͢Δ ͱ͍͏ߟ͕͑ͳ͍

  29. ࠓ೔ͷΞδΣϯμ खଓ͖ܕ -> ؔ਺ܕ΁ ؔ਺ܕݴޠͷجຊతͳ໋ྩ ࣮ྫ υΩϡϝϯτྨͳͲ

  30. ͜ͷॲཧΛؔ਺ܕͰॻ͍ͯΈΑ͏ TODOϦετ͕͋Γ·ͨ͠ 1Ϩίʔυ୯ҐͰΤϥʔνΣοΫAPIΛݺͿ Τϥʔ͚ͩநग़ͯ͠ɺΤϥʔσʔλʹมԽ͢Δ

  31. ·ͣ͸Promise.allͰ·ͱΊͯ ΤϥʔνΣοΫ·Ͱ͸ࢥ͍ͭ͘ let promiseList = R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { }); ˞isError͸PromiseΛฦ͠·͢
  32. ·ͣ͸Promise.allͰ·ͱΊͯ ΤϥʔνΣοΫ·Ͱ͸ࢥ͍ͭ͘ let promiseList = R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { }); TODOͷ֤Ϩίʔυ͝ͱʹ ΤϥʔνΣοΫAPIΛ࣮ߦ ˞isError͸PromiseΛฦ͠·͢
  33. ·ͣ͸Promise.allͰ·ͱΊͯ ΤϥʔνΣοΫ·Ͱ͸ࢥ͍ͭ͘ let promiseList = R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { }); TODOϦετΛΤϥʔνΣοΫ APIͷPromiseϦετʹมߋ ˞isError͸PromiseΛฦ͠·͢
  34. ˞isError͸PromiseΛฦ͠·͢ ·ͣ͸Promise.allͰ·ͱΊͯ ΤϥʔνΣοΫ·Ͱ͸ࢥ͍ͭ͘ let promiseList = R.map(todo => isError(todo), todos);

    Promise.all(promiseList).then(resps => { }); ͍ͭ͜ΛPromise.allʹ ౤͛ͯฒྻ࣮ߦ͢Δ
  35. Ͱ΋ɺTODOຊମͱΤϥʔνΣοΫ͕ผʑͷ഑ྻ ͔ͩΒɺॲཧ͕൥ࡶʹͳΓͦ͏ let promiseList = R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { }); TODOຊମ ΤϥʔνΣοΫ݁Ռ
  36. ͜͜Ͱ৽͍͠෢ثΛ঺հ 2ͭͷ഑ྻΛ߹ମͤ͞Δ ZIP

  37. ZIPͷಈ࡞ྫ R.zip([1,2,3], ['a','b','c']) => [[1,'a'], [2, 'b'], [3, 'c']]

  38. ZIPͰTODOϦετͱ ΤϥʔνΣοΫͷ݁ՌΛ߹ମͤ͞Δ TODO1 TODO1ͷΤϥʔνΣοΫ݁Ռ TODO2 TODO2ͷΤϥʔνΣοΫ݁Ռ ͜͜·Ͱ͘Ε͹ޙ͸؆୯ʂʂ ɾɾɾ

  39. Promise.allͰ શϨίʔυͷΤϥʔAPIΛݺͿ zip todoϦετͱPromiseͷϨεϙϯεΛ߹ମ filter Τϥʔͷ΋ͷΛ࢒͢ map Τϥʔσʔλʹม׵͢Δ

  40. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); });
  41. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); R.pipeͰෳ਺ͷॲཧΛܨ͔͚͛ͯ·͢
  42. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); ͜͜ʹ࠷ॳͷॲཧʹ౉͢Ҿ਺Λࢦఆ
  43. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); ࠓճ͸TODOຊମͱ ΤϥʔνΣοΫ݁Ռ
  44. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); TODOຊମͱΤϥʔνΣοΫ݁ՌΛ߹ମ
  45. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); R.zipͷ݁Ռ͕͍ͭ͜ͷॲཧର৅ʹͳΔ
  46. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); item[0] : todoຊମ item[1] : ΤϥʔνΣοΫ݁Ռ
  47. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); ͜ͷ৚݅͸Τϥʔ͕͋Δ Ϩίʔυ͚ͩΛ࢒͢ͱ͍͏ҙຯ
  48. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); ΍ͬͺΓR.filterͷ݁Ռ͕ೖྗʹͳΔ
  49. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); ͜͜ͰΤϥʔσʔλʹม׵͢Δ
  50. ίʔυͰॻ͘ͱ͜͏ͳΓ·͢ let promiseList = 
 R.map(todo => isError(todo), todos); Promise.all(promiseList).then(resps

    => { let errorData = R.pipe( R.zip, R.filter(item => item[1].isError), R.map(item => getErrorData(item[0].id)) )(todos, resps); }); Τϥʔσʔλͷ഑ྻ͕֨ೲ͞ΕΔ
  51. ෳࡶͦ͏ͳॲཧͰ΋ ׂͱγϯϓϧʹ͔͚·ͨ͠ ͜ͷΑ͏ʹ

  52. ࠓ೔ͷΞδΣϯμ खଓ͖ܕ -> ؔ਺ܕ΁ ؔ਺ܕݴޠͷجຊతͳ໋ྩ ࣮ྫ υΩϡϝϯτྨͳͲ

  53. ެࣜαΠτ ϓϩδΣΫτϖʔδ
 http:/ /ramdajs.com/0.21.0/index.html υΩϡϝϯτ
 http:/ /ramdajs.com/0.21.0/docs/ Try Ramda (ϒϥ΢βͰRamda͕ࢼͤ·͢)


    http:/ /ramdajs.com/repl/?v=0.21.0

  54. ίʔυͷαϯϓϧ ࠷ॳʹղઆͨ͠FILTERɺMAPΛ࢖͏΋ͷ
 http:/ /goo.gl/AC4bfS ࠷ޙʹղઆͨ͠ZIPΛ࢖͏΍ͭ
 http:/ /goo.gl/eoI0u9
 TRY RamdaͰॻ͖·ͨ͠ ͙͢ʹಈ͖·͢ʂʂ

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