ãã®æ¬ã¯ãã¿ã€ãã³ã°ã䜿çšããŠãå®å šã§ã¹ã ãŒãºã«åäœããã ãã§ãªããä¿å®ãç°¡åãªãœãããŠã§ã¢ãäœæããæ¹æ³ã瀺ããŠããŸãã
TypeScriptã§èšè¿°ãããåé¡è§£æ±ºã®äŸã¯ãåçŽãªããŒã¿åãããã¡ã³ã¯ã¿ãŒãã¢ãããªã©ã®ããè€éãªæŠå¿µãŸã§ãåãæäœããã¹ãã«ã䌞ã°ãã®ã«åœ¹ç«ã¡ãŸãã
ãã®æ¬ã®äžã§ïŒ
- åçŽãªåãé åãããã³åç §ã«åºã¥ãããŒã¿æ§é ã®äœæã
- ãªããžã§ã¯ãæåããã°ã©ãã³ã°ãšã¿ã€ãã
- ãžã§ããªãã¯ã¹ãšé«æ¬¡ã¿ã€ãã®äœ¿çšã
ãã®æ¬ã«ã¯ãTypeScriptãJavaãJavaScriptãCïŒãC ++ãªã©ã®äž»æµã®ããã°ã©ãã³ã°èšèªã®1ã€ã«é¢ããçµéšãå¿ èŠã§ãã
ãã®æ¬ã¯èª°ã®ããã®ãã®ã§ãã
ãã®æ¬ã¯ãããã°ã©ããŒã®ç·Žç¿ãç®çãšããŠããŸããèªè ã¯ãJavaãCïŒãC ++ãJavaScript / TypeScriptãªã©ã®ããã°ã©ãã³ã°èšèªã®ããããã§ã³ãŒããæžãã®ãåŸæã§ããå¿ èŠããããŸããã³ãŒãäŸã¯TypeScriptã«ãããŸãããã»ãšãã©ã®è³æã¯ãã¹ãŠã®ããã°ã©ãã³ã°èšèªã«é©çšã§ããŸããå®éãäŸã§ã¯å¿ ãããå žåçãªTypeScriptã䜿çšããŠããããã§ã¯ãããŸãããå¯èœãªéããä»ã®ããã°ã©ãã³ã°èšèªã®ããã°ã©ããŒãç解ã§ããããã«é©å¿ããŠããŸããã³ãŒãäŸã®çµã¿ç«ãŠã¯ä»é²Aã§èª¬æãããŠãããçãTypeScriptããŒãã·ãŒãã¯ä»é²Bã§èª¬æãããŠããŸãã
ä»äºã®ããã®ãªããžã§ã¯ãæåã³ãŒãéçºã«æºãã£ãŠããå Žåã¯ã代æ°çããŒã¿åïŒADTïŒãã©ã ãåŒããžã§ããªãã¯ããã¡ã³ã¯ã¿ãŒãã¢ããã«ã€ããŠèããããšããããããããŸããã ã
ãã®æ¬ã§ã¯ãããã°ã©ãã³ã°èšèªã®åã·ã¹ãã ã䜿çšããŠããšã©ãŒãçºçãã«ãããã¢ãžã¥ãŒã«åããããç解ããããã³ãŒããèšèšããæ¹æ³ã説æããŸããã·ã¹ãã å šäœãã¯ã©ãã·ã¥ãããå¯èœæ§ã®ããã©ã³ã¿ã€ã ãšã©ãŒãã³ã³ãã€ã«ãšã©ãŒã«å€æããåé¡ãçºçããåã«ãããããã£ããããæ¹æ³ã説æããŸãã
åã·ã¹ãã ã«é¢ããæç®ã®å€§éšåã¯é«åºŠã«åœ¢åŒåãããŠããŸãããã®æ¬ã¯ãåã·ã¹ãã ã®å®çšçãªã¢ããªã±ãŒã·ã§ã³ã«çŠç¹ãåœãŠãŠããŸãããããã£ãŠãæ°åŠã¯ã»ãšãã©ãããŸããããã ããé¢æ°ãéåãªã©ã®åºæ¬çãªä»£æ°ã®æŠå¿µãç解ããŠããããšããå§ãããŸããããã¯ãå¿ èŠãªæŠå¿µã®ããã€ããæ確ã«ããããã«å¿ èŠã§ãã
äžè¬åãããã¢ã«ãŽãªãºã ãšã€ã³ã¿ãŒã¬ãŒã¿ãŒ
ãã®ç« ã§ã¯
- mapïŒïŒãfilterïŒïŒãreduceïŒïŒæäœã®äœ¿çšã¯ãé åã ãã§ã¯ãããŸããã
- äžé£ã®äžè¬çãªã¢ã«ãŽãªãºã ã䜿çšããŠãããŸããŸãªåé¡ã解決ããŸãã
- ç®çã®ã³ã³ãã©ã¯ãã®æ±çšããŒã¿åãµããŒããæäŸããŸãã
- ããŸããŸãªã«ããŽãªã®ã€ãã¬ãŒã¿ã䜿çšããããŸããŸãªã¢ã«ãŽãªãºã ã®å®è£ ã
- é©å¿ã¢ã«ãŽãªãºã ã®å®è£ ã
ãã®ç« ã§ã¯ãããŸããŸãªããŒã¿åãšæ§é ã«é©ãããäžè¬çã§åå©çšå¯èœãªã¢ã«ãŽãªãºã ã«å®å šã«çŠç¹ãåœãŠãŠããŸãã
é«éé¢æ°ã«ã€ããŠèª¬æãããšãã第5ç« ã§ãmapïŒïŒãfilterïŒïŒãreduceïŒïŒã®åæäœã®1ã€ã®ããŒãžã§ã³ã確èªããŸããããããã®é¢æ°ã¯é åã§æ©èœããŸãããåã®ç« ã§èŠãããã«ãã€ãã¬ãŒã¿ãŒã¯ä»»æã®ããŒã¿æ§é ãæäœããããã®åªããæœè±¡åã§ããã€ãã¬ãŒã¿ã§åäœããäžèšã®3ã€ã®ã¢ã«ãŽãªãºã ã®æ±çšããŒãžã§ã³ãå®è£ ããããšããå§ããŸãããããã£ãŠããã€ããªããªãŒããªã¹ããé åãããã³ãã®ä»ã®å埩å¯èœãªããŒã¿æ§é ã®åŠçã«é©çšã§ããŸãã
mapïŒïŒãfilterïŒïŒãreduceïŒïŒã®æäœã¯äžæã§ã¯ãããŸãããææ°ã®ããã°ã©ãã³ã°èšèªã§å©çšã§ããä»ã®äžè¬çãªã¢ã«ãŽãªãºã ãšã¢ã«ãŽãªãºã ã©ã€ãã©ãªã«ã€ããŠèª¬æããŸããã»ãšãã©ã®ã«ãŒããã©ã€ãã©ãªã¢ã«ãŽãªãºã ã®åŒã³åºãã«çœ®ãæããæ¹ãããçç±ãããããŸãããŸããæµåçãªAPIãšãŠãŒã¶ãŒãã¬ã³ããªãŒãªã¢ã«ãŽãªãºã ã€ã³ã¿ãŒãã§ã€ã¹ã«ã€ããŠãå°ã説æããŸãã
次ã«ããã©ã¡ãŒã¿ãŒåã®å¶çŽã«ã€ããŠèª¬æããŸãããžã§ããªãã¯ããŒã¿æ§é ãšã¢ã«ãŽãªãºã ã¯ããã©ã¡ãŒã¿ãŒã¿ã€ãã«ååšããªããã°ãªããªãæ©èœãæå®ã§ããŸãããã®ç¹æ®åã«ãããã©ãã§ã䜿çšã§ããããã§ã¯ãªããæ®éæ§ã®äœãããŒã¿æ§é ãšã¢ã«ãŽãªãºã ãçºçããŸãã
ã€ãã¬ãŒã¿ã«ã€ããŠè©³ãã説æããã€ãã¬ãŒã¿ã®ããŸããŸãªã«ããŽãªã«ã€ããŠèª¬æããŸããã€ãã¬ãŒã¿ãããå°éåãããŠããã»ã©ããã®åå ã«ããå¹ççãªã¢ã«ãŽãªãºã ãå¯èœã«ãªããŸãããã ãããã¹ãŠã®ããŒã¿æ§é ãç¹æ®ãªã€ãã¬ãŒã¿ããµããŒãã§ããããã§ã¯ãããŸããã
æåŸã«ãé©å¿ã¢ã«ãŽãªãºã ã«ã€ããŠç°¡åã«èª¬æããŸãããããã¯ãããå°ãªãæ©èœãåããã€ãã¬ãŒã¿ã®ããã®ããçšéãåºããå¹çã®äœãå®è£ ãããã³ããå€ãã®æ©èœãåããã€ãã¬ãŒã¿ã®ããã®ããå¹ççã§ãããããçšéã®çãå®è£ ãå¯èœã«ããã
10.1ãmapïŒïŒãfilterïŒïŒãreduceïŒïŒæäœã®æ¹å
第5ç« ã§ã¯ãmapïŒïŒãfilterïŒïŒãreduceïŒïŒã®åæäœã«ã€ããŠèª¬æããããããã®å¯èœãªå®è£ ã®1ã€ã確èªããŸããããããã®ã¢ã«ãŽãªãºã ã¯ãå¥ã®é¢æ°ãåŒæ°ãšããŠåãããããäžé£ã®ããŒã¿ã«é©çšãããããé«éé¢æ°ã§ãã
mapïŒïŒæäœã¯ãã·ãŒã±ã³ã¹å ã®åèŠçŽ ã«é¢æ°ãé©çšããçµæãè¿ããŸãã filterïŒïŒæäœã¯ãåèŠçŽ ã«ãã£ã«ã¿ãŒé¢æ°ãé©çšãããã®é¢æ°ãtrueãè¿ãèŠçŽ ã®ã¿ãè¿ããŸãã reduceïŒïŒæäœã¯ãé¢æ°ã䜿çšããŠã·ãŒã±ã³ã¹å ã®ãã¹ãŠã®å€ãã°ã«ãŒãåããçµæãšããŠåäžã®å€ãè¿ããŸãã
第5ç« ã®å®è£ ã§ã¯ãæ±çšãã©ã¡ãŒã¿ãŒã¿ã€ãTã䜿çšããã·ãŒã±ã³ã¹ãTã¿ã€ãèŠçŽ ã®é åãšããŠè¡šããŸããã
10.1.1ããããïŒïŒæäœ
mapïŒïŒæäœãã©ã®ããã«å®è£ ããããæãåºããŠãã ãããTãšUã®2ã€ã®ã¿ã€ããã©ã¡ãŒã¿ããããŸããããã®é¢æ°ã¯ãåŒæ°ãšããŠã¿ã€ãTã®å€ã®é åãåãã2çªç®ã®åŒæ°ãšããŠTããUã«å€æããé¢æ°ãåããŸãããªã¹ã10.1ã«ç€ºãããã«ãUå€ã®é åãè¿ããŸãã
ããã§ãã€ãã¬ãŒã¿ãŒãšãžã§ãã¬ãŒã¿ãŒã®ç¥èãããã°ããªã¹ã10.2ã§ãé åã ãã§ãªãä»»æã®Iterable <T>ã§æ©èœããããã«mapïŒïŒãå®è£ ããæ¹æ³ãèŠãŠã¿ãŸãããã
å ã®å®è£ ã¯é åã«éå®ãããŠããŸããããããã¯ã€ãã¬ãŒã¿ãŒãæäŸããä»»æã®ããŒã¿æ§é ã§æ©èœããŸããããã«ãã³ãŒãã¯ã¯ããã«ã³ã³ãã¯ãã«ãªããŸããã
10.1.2ããã£ã«ã¿ïŒïŒæäœ
filterïŒïŒã§ãåãããšãããŸãããïŒãªã¹ã10.3ïŒãå ã®å®è£ ã§ã¯ãå ¥åãšããŠã¿ã€ãTã®èŠçŽ ã®é åãšè¿°èªãå¿ èŠã§ãããè¿°èªã¯ãããã¿ã€ãã®1ã€ã®èŠçŽ ãåãåããããŒã«å€ãè¿ãé¢æ°ã§ããããšãæãåºããŠãã ãã ãæå®ãããé¢æ°ãæž¡ãããå€ã«å¯ŸããŠtrueãè¿ãå Žåããã®å€ã¯è¿°èªãæºãããšèšãããŸãã
mapïŒïŒæäœãšåæ§ã«ãé åã®ä»£ããã«Iterable <T>ã䜿çšãããªã¹ã10.4ã«ç€ºãããã«ãè¿°èªãæºããå€ãçæãããžã§ãã¬ãŒã¿ãŒãšããŠãã®Iterableãå®è£ ããŸãã
ç¹°ãè¿ãã«ãªããŸãããé åã ãã§ãªãæ©èœãããããç°¡æœãªå®è£ ãå€æããŸãããæåŸã«ãreduceïŒïŒé¢æ°ãå€æŽããŸãã
10.1.3ãåæžïŒïŒæäœ
å ã®reduceïŒïŒå®è£ ã§ã¯ãã¿ã€ãTã®èŠçŽ ã®é åãã¿ã€ãTã®åæå€ïŒé åã空ã®å ŽåïŒãããã³opïŒïŒæäœãå¿ èŠã§ããããã®æäœã¯ãã¿ã€ãTã®2ã€ã®å€ãåŒæ°ãšããŠåãåããã¿ã€ãTã®1ã€ã®å€ãè¿ãé¢æ°ã§ããreduceïŒïŒæäœã¯ãæäœãåæå€ã«é©çšããé åã®æåã®èŠçŽ ã¯çµæãæ ŒçŽããçµæãšé åã®æ¬¡ã®èŠçŽ ãªã©ã«å¯ŸããåãæäœïŒãªã¹ã10.5ïŒ
ãªã¹ã10.6ã«ç€ºãããã«ããã®é¢æ°ãæžãçŽããŠãé åã®ä»£ããã«Iterable <T>ã䜿çšããä»»æã®ã·ãŒã±ã³ã¹ã§äœæ¥ããããšãã§ããŸãããã®å Žåããžã§ãã¬ãŒã¿ãŒã¯å¿ èŠãããŸãããåã®2ã€ã®é¢æ°ãšã¯ç°ãªããreduceïŒïŒã¯èŠçŽ ã®ã·ãŒã±ã³ã¹ãè¿ããŸããããåäžã®å€ãè¿ããŸãã
æ®ãã®å®è£ ã¯å€æŽãããŠããŸããã
10.1.4ãFilterïŒïŒ/ reduceïŒïŒãã€ãã©ã€ã³
ãããã®ã¢ã«ãŽãªãºã ãçµã¿åãããŠãäºåæšããå¶æ°ã®ã¿ãéžæããŠåèšãããã€ãã©ã€ã³ã«ããæ¹æ³ãèŠãŠã¿ãŸãããã第9ç« ã®BinaryTreeNode <T>ã¯ã©ã¹ãäžå¿ããªãŒãã©ããŒãµã«ã§äœ¿çšããå¶æ°ãã£ã«ã¿ãŒãšãæŒç®ãšããŠå ç®ã䜿çšããreduceïŒïŒé¢æ°ã§é£çµããŠã¿ãŸãããïŒãªã¹ã10.7ïŒã
ãã®äŸã¯ããžã§ããªãã¯åã®æå¹æ§ã®çãã蚌æ ã§ããäºåæšããã©ããŒã¹ããŠå¶æ°ãåèšããããã®æ°ããé¢æ°ãå®è£ ãã代ããã«ãç®çã®ã·ããªãªå°çšã®åŠçãã€ãã©ã€ã³ã圢æããã ãã§ãã
10.1.5ãæŒç¿
- æåååã®å埩å¯èœãªããžã§ã¯ããåŠçãããã€ãã©ã€ã³ãäœæããŸãã空ã§ãªããã¹ãŠã®æååãé£çµããŸãã
- ã¿ã€ãçªå·ã®å埩å¯èœãªããžã§ã¯ããåŠçãããã€ãã©ã€ã³ãäœæããŸãããã¹ãŠã®å¥æ°ãéžæãããããã2ä¹ããŸãã
10.2ãäžè¬çãªã¢ã«ãŽãªãºã
mapïŒïŒãfilterïŒïŒãreduceïŒïŒã¢ã«ãŽãªãºã ã«ã€ããŠèª¬æãã第9ç« ã§takeïŒïŒã«ã€ããŠã説æããŸããããã€ãã©ã€ã³ã§ã¯ä»ã®å€ãã®ã¢ã«ãŽãªãºã ããã䜿çšãããŸãããããã®ããã€ãããªã¹ãããŸããå®è£ ã«ã€ããŠã¯èª¬æããŸãããïŒIterable以å€ã®ïŒã©ã®åŒæ°ãåãåããã©ã®ããã«ããŒã¿ãåŠçãããã«ã€ããŠèª¬æããŸããããã«ããããã®ã¢ã«ãŽãªãºã ãåç §ã§ããããŸããŸãªååããªã¹ãããŸãã
- mapïŒïŒã¯ãã¿ã€ãTã®å€ã®ã·ãŒã±ã³ã¹ãšé¢æ°ïŒå€ïŒTïŒ=> Uãå ¥åãšããŠåãåãããã®é¢æ°ãå ¥åã®ãã¹ãŠã®å€ã«é©çšããåŸãã¿ã€ãUã®å€ã®ã·ãŒã±ã³ã¹ãè¿ããŸããã·ãŒã±ã³ã¹ããŸããfmapïŒïŒãselectïŒïŒ;ãšããååã§è¡šç€ºãããŸãã
- filter() T (value: T) => boolean, T, , true. where();
- reduce() T T (x: T, y: T) => T. reduce() T. fold(), collect(), accumulate(), aggregate();
- any() T (value: T) => boolean. true, ;
- all() T (value: T) => boolean. true, ;
- none() T (value: T) => boolean. true, ;
- take() T n. , n . limit();
- drop() T n. , , n. skip();
- zip() T U, , T U, , .
ã·ãŒã±ã³ã¹ã®äžŠã¹æ¿ããå転ãåå²ãããã³é£çµã«ã¯ãä»ã«ãå€ãã®ã¢ã«ãŽãªãºã ããããŸãã幞ãããããã®ã¢ã«ãŽãªãºã ã¯éåžžã«äŸ¿å©ã§å€ãã®åéã«é©çšã§ãããããèªåã§å®è£ ããå¿ èŠã¯ãããŸãããã»ãšãã©ã®ããã°ã©ãã³ã°èšèªã«ã¯ãæ¢æã®å®è£ ãåããã©ã€ãã©ãªããããŸããJavaScriptã«ã¯underscore.jsãšlodashã®ããã±ãŒãžããããããããã«å€ãã®åæ§ã®ã¢ã«ãŽãªãºã ããããŸããïŒãã®èšäºã®å·çæç¹ã§ã¯ããããã®ã©ã€ãã©ãªã¯ã€ãã¬ãŒã¿ããµããŒãããŠããŸãããçµã¿èŸŒã¿ã®JavaScripté åãšãªããžã§ã¯ãã¿ã€ãã®ã¿ããµããŒãããŠããŸããïŒJavaã§ã¯ããããã¯java.util.streamããã±ãŒãžã«ãããŸããCïŒã§ã¯ããããã¯System.Linqåå空éã«ãããŸããC ++ã§ã¯ãæšæºã©ã€ãã©ãª<algrithm>ã®ããããŒãã¡ã€ã«ã
10.2.1ãã«ãŒãã®ä»£ããã«ã¢ã«ãŽãªãºã
é©ããããããããŸããããçµéšåããããŸããã¢ã«ãŽãªãºã ãäœæãããšãã¯åžžã«ãã¿ã¹ã¯ã®ã©ã€ãã©ãªã¢ã«ãŽãªãºã ãŸãã¯ãã€ãã©ã€ã³ããããã©ããã確èªããŠãã ãããã«ãŒãã¯éåžžãã·ãŒã±ã³ã¹ãåŠçããããã«äœæãããŸãâãŸãã«äžèšã®ã¢ã«ãŽãªãºã ãè¡ãããšã§ãã
ãšã©ãŒã®å¯èœæ§ãå°ãªããããã«ã¹ã¿ã ã«ãŒããããã©ã€ãã©ãªã¢ã«ãŽãªãºã ãåªå ãããŸããã©ã€ãã©ãªã¢ã«ãŽãªãºã ã¯å®èšŒæžã¿ã§å¹ççã«å®è£ ãããŠãããæäœãæ瀺çã«æå®ããããšã§ã³ãŒããèªã¿ãããããããã«äœ¿çšã§ããŸãã
ãã®æ¬ã§ã¯ãå éšãããããç解ããããã«ããã€ãã®å®è£ ãèŠãŠããŸããããã¢ã«ãŽãªãºã ãèªåã§å®è£ ããå¿ èŠã¯ãã£ãã«ãããŸãããæ¢åã®ã¢ã«ãŽãªãºã ã®èœåãè¶ ããã¿ã¹ã¯ã«ééããå Žåã¯ã1åéãã®ç¹æ®ãªå®è£ ããããæ±çšçã§åå©çšå¯èœãªå®è£ ãäœæããããšããå§ãããŸãã
10.2.2ãæµäœãã€ãã©ã€ã³ã®å®è£
ã»ãšãã©ã®ã©ã€ãã©ãªã¯ããã€ãã©ã€ã³ã¢ã«ãŽãªãºã çšã®æµåçãªAPIãæäŸããŸããFluid APIã¯ãã³ãŒããã¯ããã«èªã¿ããããããã§ãŒã³APIã§ããäŸãšããŠã»ã¯ã·ã§ã³10.1.4ïŒãªã¹ã10.8ïŒã®ãã£ã«ã¿ãŒ/ç³ã¿èŸŒã¿ãã€ãã©ã€ã³ã䜿çšããŠãæµäœAPIãšéæµäœAPIã®éããèŠãŠã¿ãŸãããã
ãããŠãæåã«filterïŒïŒæäœãé©çšãã次ã«çµæãreduceïŒïŒæäœã«æž¡ããŸãããã³ãŒããå·Šããå³ã«èªã¿åããšãfilterïŒïŒã®åã«reduceïŒïŒã衚瀺ãããŸãããŸããã©ã®åŒæ°ããã€ãã©ã€ã³å ã®ç¹å®ã®é¢æ°ãåç §ããŠããããææ¡ããããšãéåžžã«å°é£ã§ããæµåçãªAPIã«ãããã³ãŒããã¯ããã«èªã¿ããããªããŸãã
çŸåšããã¹ãŠã®ã¢ã«ãŽãªãºã ã¯æåã®åŒæ°ãšããŠå埩å¯èœãªããžã§ã¯ããåããå埩åãè¿ããŸãããªããžã§ã¯ãæåããã°ã©ãã³ã°ã¯APIãæ¹åããŸãããã¹ãŠã®ã¢ã«ãŽãªãºã ãå埩å¯èœãªã©ãããŒã¯ã©ã¹ã«åéããããšãã§ããŸãããããŠãæåã®åŒæ°ãšããŠæ瀺çã«æå®ããã«ãä»»æã®iterableãåŒã³åºããŸããããã¯ãiterableãã¯ã©ã¹ã®ã¡ã³ããŒã§ããããã§ãããªã¹ã10.9ã«ç€ºãããã«ãmapïŒïŒãfilterïŒïŒãreduceïŒïŒã«å¯ŸããŠãããè¡ããIterableãªããžã§ã¯ããã©ããããæ°ããFluentIterable <T>ã¯ã©ã¹ã«ã°ã«ãŒãåããŸãã
Iterable <T>ã«åºã¥ããŠãFluentIterable <T>ãäœæã§ããããããã£ã«ã¿ãŒãæžãçŽãããããã€ãã©ã€ã³ãæžãããŠæµåæ§ãé«ãããããããšãã§ããŸãããªã¹ã10.10ã«ç€ºãããã«ãFluentIterable <T>ãªããžã§ã¯ããäœæãããã®ãªããžã§ã¯ãã§filterïŒïŒãåŒã³åºãããã®çµæã«åºã¥ããŠæ°ããFluentIterable <T>ãªããžã§ã¯ããäœæããreduceïŒïŒãåŒã³åºããŸãã
ããã§ãfilterïŒïŒãreduceïŒïŒã®åã«æ¥ãããã«ãªããåŒæ°ããã®é¢æ°ãåç §ããŠããããšã¯æããã§ããå¯äžã®åé¡ã¯ãåé¢æ°åŒã³åºãã®åŸã«æ°ããFluentIterable <T>ãäœæããå¿ èŠãããããšã§ãããã®APIãæ¹åããã«ã¯ãmapïŒïŒé¢æ°ãšfilterïŒïŒé¢æ°ãæžãçŽããŠãIterableIterator <T>ã§ã¯ãªãFluentIterable <T>ãè¿ããŸãã reduceïŒïŒã¯å埩å¯èœã§ã¯ãªãã¿ã€ãTã®åäžã®å€ãè¿ããããreduceïŒïŒã¡ãœãããå€æŽããå¿ èŠããªãããšã«æ³šæããŠãã ããã
ãã ãããžã§ãã¬ãŒã¿ãŒã䜿çšããŠããããããªã¿ãŒã³ã¿ã€ããåçŽã«å€æŽããããšã¯ã§ããŸããããžã§ãã¬ãŒã¿ãŒã®ååšçç±ã¯ãé¢æ°ã«äŸ¿å©ãªæ§æãæäŸããããšã§ãããåžžã«IterableIterator <T>ãè¿ããŸãã代ããã«ããªã¹ã10.11ã«ç€ºãããã«ãå®è£ ã2ã€ã®ãã©ã€ããŒãã¡ãœããmapImplïŒïŒãšfilterImplïŒïŒã«ç§»åãããããªãã¯mapïŒïŒã¡ãœãããšfilterïŒïŒã¡ãœããã§IterableIterator <T>ããFluentIterable <T>ã«å€æã§ããŸãã
ãªã¹ã10.12ã«ç€ºãããã«ããã¹ãŠã®ã¢ã«ãŽãªãºã ãã¡ãœãããšããŠèšè¿°ãããŠããFluentIterableãè¿ãããã«ãªã£ãããããã®æ¹åãããå®è£ ã«ãããã¢ã«ãŽãªãºã ã®ãã§ãŒã³ã容æã«ãªããŸãã
çŸåšããã®çã«æµåçãªããŒãžã§ã³ã§ã¯ãã³ãŒãã¯å·Šããå³ã«èªã¿ãããããã€ãã©ã€ã³ãæ§æããä»»æã®æ°ã®ã¢ã«ãŽãªãºã ãé£çµããããã®æ§æã¯éåžžã«èªç¶ã«èŠããŸããåæ§ã®ã¢ãããŒããã»ãšãã©ã®ã¢ã«ãŽãªãºã ã©ã€ãã©ãªã§äœ¿çšãããŠãããè€æ°ã®ã¢ã«ãŽãªãºã ãå¯èœãªéãç°¡åã«é£éãããããšãã§ããŸãã
ããã°ã©ãã³ã°èšèªã«ãã£ãŠã¯ãFluent APIã®æ¬ ç¹ã®1ã€ã¯ãFluentIterableã¯ã©ã¹ããã¹ãŠã®ã¢ã«ãŽãªãºã ã®ã¡ãœããã§ä¹±éã«ãªããæ¡åŒµãå°é£ã«ãªãããšã§ããã©ã€ãã©ãªã«å«ãŸããŠããå ŽåãåŒã³åºãå ã®ã³ãŒãã«ã¯ãã¯ã©ã¹ãå€æŽããã«æ°ããã¢ã«ãŽãªãºã ãè¿œå ããæ¹æ³ããããŸãããCïŒã«ã¯ãã³ãŒããå€æŽããã«ã¯ã©ã¹ãŸãã¯ã€ã³ã¿ãŒãã§ã€ã¹ã«ã¡ãœãããè¿œå ã§ãããããããæ¡åŒµã¡ãœããããããŸãããã ãããã¹ãŠã®èšèªã«ãã®ãããªæ©èœãããããã§ã¯ãããŸãããããã§ããã»ãšãã©ã®å Žåãæ°ããã¢ã«ãŽãªãºã ãæåããå®è£ ããããããæ¢åã®ã¢ã«ãŽãªãºã ã®ã©ã€ãã©ãªã䜿çšããæ¹ãé©åã§ãã
10.2.3ãæŒç¿
- FluentIterableã¯ã©ã¹ãæ¡åŒµããŠãã€ãã¬ãŒã¿ãŒããæåã®nåã®èŠçŽ ãè¿ãtakeïŒïŒã¢ã«ãŽãªãºã ãå«ããŸãã
- FluentIterableã¯ã©ã¹ãæ¡åŒµããŠãã€ãã¬ãŒã¿ãŒããæåã®nåã®èŠçŽ ãã¹ãããããæ®ãããã¹ãŠè¿ãdropïŒïŒã¢ã«ãŽãªãºã ãå«ããŸãã
10.3ããã©ã¡ãŒã¿ã¿ã€ãã®å¶é
ãžã§ããªãã¯ããŒã¿æ§é ãç¹å®ã®ã¿ã€ããã©ã¡ãŒã¿ãŒTã«äŸåããªãããŒã¿ã®åœ¢åŒãã©ã®ããã«å®çŸ©ãããã«ã€ããŠã¯ãã§ã«èŠãŠããŸããããŸããã©ã®ã¿ã€ãã«é¢ä¿ãªããã€ãã¬ãŒã¿ãŒã䜿çšããŠã¿ã€ãTã®å€ã®ã·ãŒã±ã³ã¹ãåŠçããäžé£ã®ã¢ã«ãŽãªãºã ã«ã€ããŠã調ã¹ãŸãããã§ããããã§ããªã¹ã10.13ã§ãç¹å®ã®ããŒã¿åãéèŠã§ããã·ããªãªãèããŠã¿ãŸããããIterable<T>ãåŒæ°ãšããŠåããã€ãã¬ãŒã¿ãŒã«ãã£ãŠè¿ãããåèŠçŽ ã§renderïŒïŒãåŒã³åºããžã§ããªãã¯é¢æ°renderAllïŒïŒã§ãã
ãã®é¢æ°ãã³ã³ãã€ã«ããããšãããšã次ã®ãšã©ãŒã¡ãã»ãŒãžã衚瀺ãããŸãããããã㣠'render'ã¯ã¿ã€ã 'T'ã«ååšããŸããïŒã¿ã€ã 'T'ã«ã¯ 'render'ããããã£ããããŸããïŒã
ãžã§ããªãã¯åTã®renderïŒïŒã¡ãœãããåŒã³åºãããšããŠããŸããããã®ãããªã¡ãœããããªãå¯èœæ§ããããŸãããã®ãããªã·ããªãªã§ã¯ãã¿ã€ãTãrenderïŒïŒã¡ãœãããå«ãç¹å®ã®ã€ã³ã«ããŒã·ã§ã³ã®ã¿ã«å¶éããå¿ èŠããããŸã ã
ãã©ã¡ãŒã¿ã¿ã€ãã®å¶é
å¶çŽã¯ãåŒæ°ã¿ã€ããæã€å¿ èŠã®ããæ©èœã«ã€ããŠã³ã³ãã€ã©ãŒã«éç¥ããŸããå¶éãªãã§ãåŒæ°ã®åã¯äœã§ãããŸããŸããããžã§ããªãã¯ããŒã¿åã«ç¹å®ã®ã¯ã©ã¹ã¡ã³ããŒãå¿ èŠãªå Žåã¯ãå¶çŽãå©çšããŠãèš±å¯ãããåã®ã»ããããããã®å¿ é ã¡ã³ããŒãæã€ãã®ã«æžãããŸãã
ãã®äŸã§ã¯ããªã¹ã10.14ã«ç€ºãããã«ãrenderïŒïŒã¡ãœããã宣èšããIRenderableã€ã³ã¿ãŒãã§ãŒã¹ãå®çŸ©ã§ããŸãã次ã«ãextendsããŒã¯ãŒãã䜿çšããŠãTã®åã«å¶çŽãè¿œå ããIRenderableãå ·äœåããåŒæ°åã®ã¿ãèš±å¯ãããããšãã³ã³ãã€ã©ãŒã«éç¥ã§ããŸãã
10.3.1ãå¶éãããäžè¬çãªããŒã¿æ§é
ã»ãšãã©ã®äžè¬çãªããŒã¿æ§é ã¯ããã©ã¡ãŒã¿ãŒã¿ã€ãã®å¶çŽãå¿ èŠãšããŸããããªã³ã¯ãªã¹ããããªãŒãããã³é åã«ã¯ãä»»æã®ã¿ã€ãã®å€ãæ ŒçŽã§ããŸãããã ããããã€ãã®äŸå€ãç¹ã«ããã·ã¥ã»ããããããŸãã
ã»ããã®ããŒã¿æ§é ã¯æ°åŠçãªæå³ã§ã»ãããã¢ãã«åãããããäžæã®å€ãã»ããã«ä¿åãããéè€ã¯ç Žæ£ãããŸããã»ããããŒã¿æ§é ã«ã¯éåžžãä»ã®ã»ãããçµåã亀差ãããã³æžç®ããããã®ã¡ãœãããå«ãŸããç¹å®ã®å€ãã»ããã«å«ãŸããŠãããã©ããã確èªããããšãã§ããŸãããã®ãã§ãã¯ãå®è¡ããã«ã¯ããã®å€ãã»ããã®åèŠçŽ ãšæ¯èŒã§ããŸãããããã¯ããŸãå¹ççãªæ¹æ³ã§ã¯ãããŸãããææªã®å Žåãã»ããã®åèŠçŽ ãšæ¯èŒããã«ã¯ãã»ããå šäœããã©ããŒã¹ããå¿ èŠããããŸãããã®ãããªãã©ããŒãµã«ã¯ãç·åœ¢æéOïŒnïŒã§å®è¡ãããŸãã以äžã®ãµã€ãããŒãBigONotationãã§ããããã®è¡šèšæ³ããã©ãã·ã¥ã¢ããã§ããŸãã
æãå¹ççãªå®è£ ã¯ããã¹ãŠã®å€ãããã·ã¥ããããã·ã¥ããããèŸæžãªã©ã®ããŒå€ããŒã¿æ§é ã«æ ŒçŽããããšã§ãããããã®ãããªæ§é ã¯ããå¹ççã§ãããäžå®æéã§å€ãååŸã§ããŸã ãOïŒ1ïŒãããã·ã¥ã»ããã¯ããã·ã¥ããããã©ããããèŠçŽ ãå«ãŸããŠãããã©ãããå¹ççã«ãã§ãã¯ããŸãããã ããå¶éããããŸããã¿ã€ãTã¯ãã¿ã€ãTã®å€ãåããã¿ã€ãçªå·ã®ããã·ã¥å€ãè¿ãããã·ã¥é¢æ°ãæäŸããå¿ èŠããããŸãã
äžéšã®ããã°ã©ãã³ã°èšèªã§ã¯ãããé«ãã¿ã€ãã§ããã·ã¥ã¡ãœãããèšè¿°ããããšã«ããããã¹ãŠã®å€ã®ããã·ã¥ãå¯èœã§ããããšãã°ãäžäœã®Javaãªããžã§ã¯ãã¿ã€ãã«ã¯hashCodeïŒïŒã¡ãœãããå«ãŸããäžäœã®CïŒãªããžã§ã¯ãã¿ã€ãã«ã¯GetHashCodeïŒïŒã¡ãœãããå«ãŸããŸããããããèšèªããã®ãããªå¯èœæ§ãæäŸããªãå Žåã¯ãããã·ã¥å¯èœãªåã®ã¿ãããŒã¿æ§é ã«æ ŒçŽã§ããããã«ãåå¶çŽãå¿ èŠã§ããããšãã°ãIHashableã€ã³ã¿ãŒãã§ã€ã¹ãå®çŸ©ãããããæ±çšããã·ã¥ããããŸãã¯ãã£ã¯ã·ã§ããªã®ããŒã¿ã€ãã®åå¶çŽãšããŠäœ¿çšã§ããŸãã
èè ã«ã€ããŠ
Vlad Rishkutsiaã¯ãMicrosoftã®ãœãããŠã§ã¢éçºã¹ãã·ã£ãªã¹ãã§ããã10幎以äžã®çµéšããããŸãããã®éã圌ã¯ããã€ãã®äž»èŠãªãœãããŠã§ã¢ãããžã§ã¯ãã管çããå€ãã®è¥ãå°é家ãèšç·ŽããŸããã
ãã®æ¬ã«ã€ããŠã®è©³çŽ°ã¯ãäžã§èŠã€ããããšãã§ãã åºç瀟ã®ãŠã§ããµã€ã
» ç®æ¬¡
» æç²
ã«ã€ããŠã¯å± äœè ã¯ãŒãã³ã§25ïŒ å²åŒ- 掻å
æ¬ã®çŽã®ããŒãžã§ã³ã®æ¯æãã®éãé»åæžç±ã¯ãE-ã«ãã£ãŠéä¿¡ãããŸãéµäŸ¿ç©ã