1.æåã®ã¹ããã
2.æ©èœãçµã¿åããã
3.éšå䜿çšïŒã«ãªãŒåïŒ
4.宣èšåããã°ã©ãã³ã°
5. Quintessentialè¡šèš
6.äžå€æ§ãšãªããžã§ã¯ã
7.äžå€æ§ãšé å
8.ã¬ã³ãº
9.çµè«
第2éšã§ã¯ããã€ãã©ã€ã³ã¢ãŒãã§äžé£ã®é¢æ°ã䜿çšã§ããããã«ãããé¢æ°ãããŸããŸãªæ¹æ³ã§çµã¿åãããæåŸã«æ§æé¢æ°ãšãã€ãé¢æ°ã§çµããããšã«ã€ããŠèª¬æããŸããã
ãã®æçš¿ã§ã¯ãåŒæ°ã1ã€ã ããšãåçŽãªé¢æ°ãã€ãã©ã€ã³ãåãäžããŸããã ããããè€æ°ã®åŒæ°ãåãé¢æ°ã䜿çšããå Žåã¯ã©ãã§ããããïŒ
ããšãã°ãæžç±ãªããžã§ã¯ãã®ã³ã¬ã¯ã·ã§ã³ããããç¹å®ã®å¹Žã«åºçããããã¹ãŠã®æžç±ã®ååãæ€çŽ¢ãããšããŸãã Ramdaå埩é¢æ°ã®ã¿ã䜿çšããŠãã®åé¡ã解決ããŸãããã
const publishedInYear = (book, year) => book.year === year const titlesForYear = (books, year) => { const selected = filter(book => publishedInYear(book, year), books) return map(book => book.title, selected) }
ãã£ã«ã¿ãŒãšããããçµã¿åãããŠãã€ãã©ã€ã³ã«ãããšè¯ãã§ããããããã£ã«ã¿ãŒãšãããã¯2ã€ã®åŒæ°ãåãããããããè¡ãæ¹æ³ã¯ããããŸããã
ãŸãããã£ã«ã¿ãŒã§ç¢å°é¢æ°ã䜿çšããå¿ èŠããªãå Žåã«ã圹ç«ã¡ãŸãã æåã«ãã®åé¡ã解決ããŸããããããã«ããããã€ãã©ã€ã³ãäœæãããšãã«äœ¿çšã§ããããšãããããŸãã
é«éé¢æ°
ãã®ã·ãªãŒãºã®æåã®éšåã§ã¯ãé¢æ°ãäžæµã®æ§é ãšããŠèª¬æããŸããã åæ§ã®é¢æ°ããã©ã¡ãŒã¿ãŒãšããŠä»ã®é¢æ°ã«æž¡ããä»ã®é¢æ°ã®çµæãšããŠè¿ãããšãã§ããŸãã åè ã®å€ããè¡ããŸãããããŸã åŸè ãé©çšããŠããŸããã
ä»ã®é¢æ°ãååŸãŸãã¯è¿ãé¢æ°ã¯ããé«éé¢æ°ããšãåŒã°ããŸãã
äžèšã®äŸã§ã¯ãç¢å°é¢æ°ããã£ã«ã¿ãŒã«æž¡ããŸãïŒbook => publishedInYearïŒbookãyearïŒããããåé€ãããšããã§ãããã ãããè¡ãã«ã¯ãæ¬ãåãåãããã®æ¬ãé©åãªå¹Žã«çºè¡ãããå Žåã«trueãè¿ãé¢æ°ãå¿ èŠã§ãã ãã ãã幎æ°ãæž¡ããŠæè»ã«ããå¿ èŠããããŸãã
ãã®åé¡ã解決ããæ¹æ³ã¯ãå¥ã®é¢æ°ãè¿ãé¢æ°ãäœæããããšã§ãã ããã§äœãèµ·ãã£ãŠããã®ããç解ã§ããããã«ãéåžžã®é¢æ°æ§æã§ãããèšè¿°ããŸãããç¢å°æ§æã䜿çšããçãããŒãžã§ã³ã«é²ã¿ãŸãã
// function publishedInYear(year) { return function(book) { return book.year === year } } // : const publishedInYear = year => book => book.year === year
ããã¯publishedInYearé¢æ°ã®æ°ããããŒãžã§ã³ã§ããç¢å°é¢æ°ãé€å€ããããšã§ãã£ã«ã¿ãŒåŒã³åºããæžãæããããšãã§ããŸãã
const publishedInYear = year => book => book.year === year const titlesForYear = (books, year) => { const selected = filter(publishedInYear(year), books) return map(book => book.title, selected) }
ããŠãfilterãåŒã³åºããšãpublishedInYearïŒyearïŒãããã«åŒã³åºãããæ¬ãååŸããé¢æ°ãè¿ããŸããããã¯ããŸãã«ãã£ã«ã¿ãŒã«å¿ èŠãªãã®ã§ãã
éšåçã«é©çšå¯èœãªæ©èœ
å¿ èŠã«å¿ããŠããã®æ¹æ³ã§ããã€ãã®åŒæ°ã䜿çšããŠé¢æ°ãæžãæããããšãã§ããŸããããã¹ãŠã®é¢æ°ããã®ããã«åäœããããã§ã¯ãããŸããã ãŸããéåžžã®æ¹æ³ã§è€æ°ã®åŒæ°ãæã€é¢æ°ã䜿çšããããšãã§ããŸãã
ããšãã°ãç¹å®ã®å¹Žã«ãã®æ¬ãåºçãããããšã確èªãããã ãã®ä»ã®ã³ãŒããããå ŽåãpublishedInYearïŒbookã2012ïŒã®ããã«æžãããã®ã§ããããã®ããã«æžãããšã¯ã§ããŸããã 代ããã«ãpublishedInYearïŒ2012ïŒïŒbookïŒã®ããã«å°ãæžããŸãã èªã¿ã«ãããç ©ãããã§ãã
幞ããªããšã«ã Ramdaã¯ãããæ¯æŽãã2ã€ã®é¢æ°ã partialããã³partialRightãæäŸããŸãã
ãããã®2ã€ã®é¢æ°ã䜿çšãããšãå¿ èŠãªåŒæ°ãå°ãªãå¥ã®é¢æ°ãåŒã³åºãããšãã§ããŸãã ãããã¯äž¡æ¹ãšããæ®ãã®åŒæ°ãåãæ°ããé¢æ°ãè¿ãããã¹ãŠã®åŒæ°ãæäŸãããå Žåã§ãå ã®é¢æ°ãåŒã³åºããŸãã
partialãšpartialRightã®éãã¯ãæå®ããåŒæ°ãå ã®é¢æ°ã«å¿ èŠãªå·Šç«¯ãŸãã¯å³ç«¯ã®åŒæ°ã«çœ®ãæããããããšã§ãã
å ã®äŸã«æ»ã£ãŠãpublishedInYearãæžãæãã代ããã«ãããã®é¢æ°ã䜿çšããŠã¿ãŸãããã 幎ãæå®ããã ãã§ããããããå³ç«¯ã®åŒæ°ãªã®ã§ãpartialRightã䜿çšããå¿ èŠããããŸãã
const publishedInYear = (book, year) => book.year === year const titlesForYear = (books, year) => { const selected = filter(partialRight(publishedInYear, [year]), books) return map(book => book.title, selected) }
publishedInYearã«ïŒbookãyearïŒã§ã¯ãªãïŒyearãbookïŒãæå®ããŠèšè¿°ããå ŽåãpartialRightã§ã¯ãªãpartialã䜿çšããŸãã
partialãšpartialRightã«æž¡ãåŒæ°ã¯ããã®ãã¡ã®1ã€ã ããæž¡ãå Žåã§ããåžžã«é åå ã«ããå¿ èŠãããããšã«æ³šæããŠãã ããã ãããäœåå¿ããŠçŽãããããšã©ãŒã¡ãã»ãŒãžãåãåã£ããã¯èšããŸããã
First argument to _arity must be a non-negative integer no greater than ten
ã«ã¬ãŒ
ã©ãã§ãpartialãšpartialRightã䜿çšããå¿ èŠããããããåé·æ§ãšç²åŽãçããŸãã ããããäžé£ã®åäžåŒæ°é¢æ°ãšããŠå€ãã®åŒæ°ãæã€é¢æ°ãåŒã³åºãå¿ èŠæ§ã¯é·ãéæªãã§ãã
幞ããªããšã«ãRamdaã¯ã«ãªãŒåãšãããœãªã¥ãŒã·ã§ã³ãæäŸããŸãã
ã«ã¬ãŒã¯ãé¢æ°åããã°ã©ãã³ã°ã®ãã1ã€ã®åºæ¬æŠå¿µã§ãã æè¡çã«ã¯ãã«ãªãŒåãããé¢æ°ã¯åžžã«ãå ã»ã©ç³ãäžããäžé£ã®åäžåŒæ°é¢æ°ã§ãã çŽç²ãªé¢æ°åèšèªã§ã¯ãæ§æã¯ãããã€ãã®åŒæ°ã䜿çšããŠé¢æ°ãåŒã³åºãå Žåãšéãããªãããã«èŠããŸãã
ããããRamdaã¯JavaScriptã©ã€ãã©ãªã§ãããJavaScriptã«ã¯äžé£ã®åäžåŒæ°é¢æ°ãåŒã³åºãããã®åªããæ§æããªããããèè ã¯ã«ãªãŒåã®åŸæ¥ã®å®çŸ©ããããã«ç·©åããŸããã
Ramdaã§ã¯ãã«ãªãŒåãããé¢æ°ã¯åŒæ°ã®ãµãã»ããã䜿çšããŠåŒã³åºãããšãã§ããæ®ãã®åŒæ°ãåä¿¡ãããã®ãåŸ ã€æ°ããé¢æ°ãè¿ããŸãã ãã¹ãŠã®åŒæ°ã§ã«ãªãŒåãããé¢æ°ãåŒã³åºããšãéåžžã®é¢æ°åŒã³åºããããªã¬ãŒãããŸãã
ã«ãªãŒåãããé¢æ°ã¯ã2ã€ã®äžçã®ãã¹ããšèããããšãã§ããŸãããã¹ãŠã®åŒæ°ã§åŒã³åºãããšãã§ãããããã¯æ©èœããŸãã ãŸãã¯ãåŒæ°ã®äžéšã§ããããåŒã³åºãããšãã§ããéšåçãªã¢ããªã±ãŒã·ã§ã³ã¢ãŒãã§åäœããŸãã
ã«ã¬ãŒã§ã¯é¢æ°ã®åŒã³åºãæ¹æ³ã決å®ãã次ã«äœãè¡ãå¿ èŠããããã決å®ããå¿ èŠãããããããã®æè»æ§ã¯ããã©ãŒãã³ã¹ã«ããããªåœ±é¿ãäžããããšã«æ³šæããŠãã ããã äžè¬ã«ãè€æ°ã®å Žæã§éšåãã€ã³ãã£ã³ã°ã䜿çšããå¿ èŠãããããšãããã£ãå Žåãé¢æ°ã«ãªãŒåã䜿çšããŸãã
ã«ãªããªã³ã°æ©èœãpublishedInYearé¢æ°ã«é©çšããŠã¿ãŸãããã ã«ãªãŒåã¯åžžã«ããŒã·ã£ã«é¢æ°ã䜿çšããŠãããã®ããã«æ©èœããpartialRightã®ãããªããŒãžã§ã³ã䜿çšããæ¹æ³ã¯ãªãããšã«æ³šæããŠãã ããã 次ã«ããã®ãããã¯ã«ã€ããŠããå°ã説æããŸãããããã§ã¯ã1幎ãå§ãŸãããã«ãpublishedInYearã®åŒæ°ãéé ã«å€æŽããŸãã
const publishedInYear = curry((year, book) => book.year === year) const titlesForYear = (books, year) => { const selected = filter(publishedInYear(year), books) return map(book => book.title, selected) }
ããã§ãpublishedInYearã幎ã«1åã ãåŒã³åºããŠãæ¬ãååŸããŠå ã®é¢æ°ãåŒã³åºãé¢æ°ãååŸã§ããŸãã ãã ããããããããããšãªããpublishedInYearïŒ2012ãbookïŒãéåžžã®æ¹æ³ã§åŒã³åºãããšãã§ããŸãïŒæ§æã2ã€ã®äžçã®ãã¹ãïŒ
åŒæ°ã®é åº
ã«ãªãŒåãæ©èœãããã«ã¯ãåŒæ°ã®é åºãéã«ããå¿ èŠãããããšã«æ³šæããŠãã ããã ããã¯é¢æ°åããã°ã©ãã³ã°ã§ã¯éåžžã«äžè¬çã§ãããããã»ãŒãã¹ãŠã®Ramdaé¢æ°ã¯ãæçµçã«åäœããããŒã¿ãåŒæ°ãªã¹ãã®æåŸã«ãªãããã«èšè¿°ãããŠããŸãã
æåã®ãã©ã¡ãŒã¿ãŒã¯ãæäœã®æ§æãšèããããšãã§ããŸãã ãããã£ãŠãpublishedInYearã®å Žåãyearãã©ã¡ãŒã¿ãŒã¯æ§æïŒäœãæ¢ããŠããŸããïŒïŒã§ãããbookãã©ã¡ãŒã¿ãŒã¯ããŒã¿ïŒã©ããæ¢ããŠããŸããïŒã§ãã
å埩é¢æ°ã䜿çšãããã®äŸã¯æ¢ã«èŠãŸããã ãã®ããã°ã©ãã³ã°ã¹ã¿ã€ã«ãç°¡åã«ãªããããå šå¡ãã³ã¬ã¯ã·ã§ã³ãæåŸã®åŒæ°ãšããŠäœ¿çšããŸããã
ééã£ãé åºã®åŒæ°
publishedInYearé¢æ°ã®åŒæ°ã®é åºãå€æŽããªãå Žåã¯ã©ããªããŸããïŒ ã«ã¬ãŒã®æ§è³ªããã©ã®ããã«å©çãåŸãããšãã§ããŸããïŒ
Ramdaã¯ããã€ãã®ãªãã·ã§ã³ãæäŸããŸãã
ããªãã
æåã®ãªãã·ã§ã³ã¯ããªããã§ãã Flipã¯ã2ã€ä»¥äžã®åŒæ°ãæã€é¢æ°ãåããåãåŒæ°ãåãæ°ããé¢æ°ãè¿ããŸãããæåã®2ã€ã®åŒæ°ã亀æããŸãã ã»ãšãã©ã®å Žåã2ã€ã®åŒæ°ãæã€é¢æ°ã§äœ¿çšãããŸãããã¢ããªã±ãŒã·ã§ã³ã§ã¯ããäžè¬çã§ãã
ããªããã䜿çšããŠãpublishedInYearã®å ã®åŒæ°ã®é åºã«æ»ãããšãã§ããŸãã
const publishedInYear = curry((book, year) => book.year === year) const titlesForYear = (books, year) => { const selected = filter(flip(publishedInYear)(year), books) return map(book => book.title, selected) }
ã»ãšãã©ã®å Žåããã䟿å©ãªåŒæ°ã®é åºã䜿çšããããšããå§ãããŸãããããšãã°ãå¶åŸ¡ããŠããªãé¢æ°ã䜿çšããå¿ èŠãããå Žåãããªããã¯äŸ¿å©ãªãªãã·ã§ã³ã§ãã
ãã¬ãŒã¹ãã«ããŒ
ããäžè¬çãªãªãã·ã§ã³ã¯ããã¬ãŒã¹ãã«ããŒåŒæ°ïŒ__ïŒã§ãã
3ã€ã®åŒæ°ãæã€ã«ãªãŒåãããé¢æ°ããããæåãšæåŸã®åŒæ°ãæž¡ããå°æ¥ã®ããã«äžéã®åŒæ°ãæ®ãããå Žåã¯ã©ããªããŸããïŒ äžéã®åŒæ°ã«ãã¬ãŒã¹ãã«ããŒã䜿çšã§ããŸãïŒ
const threeArgs = curry((a, b, c) => { /* ... */ }) const middleArgumentLater = threeArgs('value for a', __, 'value for c')
ãŸããåŒã³åºãã§ä»£æ¿ãè€æ°å䜿çšããããšãã§ããŸãã ããšãã°ãäžéã®åŒæ°ã®ã¿ãæž¡ãããå Žåã¯ã©ãã§ããããïŒ
const threeArgs = curry((a, b, c) => { /* ... */ }) const middleArgumentOnly = threeArgs(__, 'value for b', __)
å¿ èŠã«å¿ããŠãããªããã®ä»£ããã«ãã¬ãŒã¹ãã«ããŒã¹ã¿ã€ã«ã䜿çšã§ããŸãã
const publishedInYear = curry((book, year) => book.year === year) const titlesForYear = (books, year) => { const selected = filter(publishedInYear(__, year), books) return map(book => book.title, selected) }
ãã®ããŒãžã§ã³ã®æ¹ãèªã¿ããããšæããŸãããpublishedInYearã®ãå転ãããŒãžã§ã³ãç¹°ãè¿ã䜿çšããå¿ èŠãããå Žåã¯ãflipã䜿çšããŠè¿œå ã®é¢æ°ãå®çŸ©ããã©ãã§ãåŒãç¶ã䜿çšã§ããŸãã ä»åŸã®æçš¿ã§ããã€ãã®äŸãèŠããããããŸããã
__ã¯ãä»»æã®é¢æ°ã§partialãpartialRightããã³flipãæ©èœããå Žåã«ã®ã¿ãã«ãªãŒåãããé¢æ°ã§æ©èœããããšã«æ³šæããŠãã ããã éåžžã®é¢æ°ã§__ã䜿çšããå¿ èŠãããå Žåã¯ããããè¡ãåã«ãã€ã§ãã«ã¬ãŒã§ã©ããã§ããŸãã
ã³ã³ãã¢ããããŸããã
åŒã³åºãã移åããŠããã€ãã©ã€ã³å ã§ãã£ã«ã¿ãŒåŠçãšãããã³ã°ãè¡ãæ¹æ³ãèŠãŠã¿ãŸãããã ããã¯ã³ãŒãã®çŸåšã®ç¶æ ã§ãpublishedInYearã®äŸ¿å©ãªåŒæ°ã®é åºããããŸãã
const publishedInYear = curry((year, book) => book.year === year) const titlesForYear = (books, year) => { const selected = filter(publishedInYear(year), books) return map(book => book.title, selected) }
以åã®æçš¿ã§ãã€ããšäœæ²ã«ã€ããŠåŠã³ãŸãããããã®ç 究ããæ倧éã®å©çãåŸãããã«ã¯ãå¥ã®æ å ±ãåŠã¶å¿ èŠããããŸãã
æåŸã®æ å ±ã¯æ¬¡ã®ãšããã§ããã»ãŒãã¹ãŠã®Ramdaé¢æ°ã¯ããã©ã«ãã§ã«ãªãŒåãããŠããŸãã ããã«ã¯ãã£ã«ã¿ãŒãšããããå«ãŸããŸãã ãããã£ãŠããã£ã«ã¿ãŒïŒpublishedInYearïŒyearïŒïŒã¯å®å šã«é©åããmapïŒbook => book.titleïŒã®ããã«ããã®åŸæžç±ãæž¡ãããã®ãåŸ ã€ã ãã®æ°ããé¢æ°ãè¿ããŸãã
ãããŠä»ããã€ãã©ã€ã³ãæžãããšãã§ããŸãïŒ
const publishedInYear = curry((year, book) => book.year === year) const titlesForYear = (books, year) => pipe( filter(publishedInYear(year)), map(book => book.title) )(books)
äžæ©åé²ããŠãtitlesForYearã®åŒæ°ãå転ãããŠãæåŸã«RamdaããŒã¿å¥çŽã«æºæ ããŠã¿ãŸãããã é¢æ°ãã«ãªãŒåããŠãåŸç¶ã®ãã€ãã©ã€ã³ã§äœ¿çšã§ããããã«ããããšãã§ããŸãã
const publishedInYear = curry((year, book) => book.year === year) const titlesForYear = curry((year, books) => pipe( filter(publishedInYear(year)), map(book => book.title) )(books) )
ãããã«
ãã®æçš¿ã¯ããããããã®äžé£ã®èšäºã®æãæ·±ãéšåã§ãã éšåçãªäœ¿çšãšã«ã¬ãŒã¯ãé ã«åãŸãã®ã«æéãããããŸãã ãã ããäžåºŠåŠç¿ãããšãæ©èœçãªã¹ã¿ã€ã«ã§ããŒã¿ãå€æããéåžžã«åŒ·åãªæ¹æ³ã玹ä»ããŸãã
圌ãã¯ãå°ããªåçŽãªãã«ãã£ã³ã°ãããã¯ã§æ§æãããã³ã³ãã€ã«åºã¥ããŠå€æãè¡ãããšã匷å¶ããŸãã
次ãž
æ©èœçãªã¹ã¿ã€ã«ã§æžãå§ããã«ã¯ããåœä»€çãã§ã¯ãªãã宣èšçã«ãèãå§ããå¿ èŠããããŸãã ãããè¡ãã«ã¯ãæ©èœçãªã¹ã¿ã€ã«ã§åœä»€æ§é ãè¡šçŸããæ¹æ³ãèŠã€ããå¿ èŠããããŸãã 宣èšåããã°ã©ãã³ã°ã«é¢ããèšäºã§ã¯ããããã®ã¢ã€ãã¢ã«ã€ããŠèª¬æããŸãã