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

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

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

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

kaidouji85

June 04, 2016
Tweet

More Decks by kaidouji85

Other Decks in Technology

Transcript

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

    {action:'ϓϩੜͷൃදࢿྉΛ࡞Δ', isChanged: false}, {action:'ϑΝϯτϜϫʔϧυΛ؍Δ', isChanged: true}, {action:'ϋοΧυʔϧΛνΣοΫ͢Δ', isChanged: true}, {action:'ח૔ʹ͓Ֆݟʹߦ͘', isChanged: false}, {action:'य़ΞχϝͰԿΛݟΔ͔ܾΊΔ', isChanged: true}, ];
  2. let postData = []; for (let i in todo) {

    if(todo[i].isChanged) { postData.push(convertData(todo[i])); } } ίʔυʹىͯ͜͠Έ·͠ΐ͏ Α͋͘Δॻ͖ํͰ͕͢ɺ໰୊͕͋Γ·͢
  3. ؔ਺ܕͪͬ͘ʹॻ͍ͨ 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])); } }
  4. // FILTER มߋ͞Εͨ΋ͷ͚ͩ࢒͢ let chanegedList = R.filter(data=>data.isChanged, todo); // MAP

    API༻ʹσʔλม׵͢Δ let postData = R.map(convertData, chanegedList); ίʔυ΋̍ର̍ରԠ͍ͯ͠Δ
  5. ϥϜμ͕ࣜ௕͗͢Δྫ // ؔ਺ܕݴޠͰҰԠॻ͍ͨ 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])); } }
  6. ྫ୊ ҎԼͷΑ͏ͳσʔλม׵ॲཧΛߦ͏ //ݩͷσʔλߏ଄ [ { data:’YYYY/MM/DD’, weather:’ ఱؾ’ } ]

    // ม׵ޙͷσʔλߏ଄ [ { weather:’ఱؾ’, data:[ ‘YYYY/MM/DD’ ] } ]
  7. ౴͑ͷίʔυ 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
  8. গ͚ͩ͠ղઆ // άϧʔϓ͝ͱͷσʔλม׵ॲཧ 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);