ããã«ã¡ãã¡ãã
以åã«ä¾¿å©ãªVimã®Operatorããã¹ã¿ã¼ãããæ¸ãã¾ããemonkakã§ãã
ä»åã¯ç§ãå人çã«éçºãé²ãã¦ãã¦ä»¥åã«ç¤¾å
åå¼·ä¼ã§çºè¡¨ãã¾ãããPHPã®ã³ã¬ã¯ã·ã§ã³å¦çã©ã¤ãã©ãªã®Underbar.phpã®ç´¹ä»ããããã¨æãã¾ãã
PHPã®arrayãªãã¨ãé¢æ°ã¯ä½¿ãåæãæªã
PHPã«ã¯é
åå¦çã®ããã®é¢æ°ãããããããã¾ãããarray_map()
ã®å¼æ°ã®é çªãå¤ã ã£ããarray
ããããããªãã¸ã§ã¯ãã§ã¯ãªãã®ã§ã¡ã½ãããã§ã¤ã³ãã§ããªãã£ãã使ãåæãè¯ãããã¾ããã
$xs = range(1, 10); $twice = function($n) { return $n * 2; }; array_slice(array_map($twice, $xs), 0, 3); // [2, 4, 6]
ã¾ãããããã®é¢æ°ã¯array
å°ç¨ãªã®ã§ãIterator
ã®ãããªTraversable
ãªãªãã¸ã§ã¯ããå¼æ°ã«åããã¨ã¯ã§ãã¾ããããå¦çãé
延ãã¦Iterator
ãè¿ããªãã¦ãã¨ãå½ç¶ã§ãã¾ããã
ããã§array
ã¨Traversable
ãªãªãã¸ã§ã¯ããåãããã«æ±ãã¦ãIterator
ãå©ç¨ãã¦é
延ãªã¹ããè¿ããã¨ãã§ããã©ã¤ãã©ãªã欲ããã§ãããã
ããã«PHP5.5ããå°å
¥ãããGenerator
ã使ããã¨ããã§ããï¼
é 延ãªã¹ãã®æ©æµ
ããã§ä¸åº¦ãé
延ãªã¹ãã®æ©æµã«ã¤ãã¦èãã¦ã¿ã¾ãã
0ããã®é£çªãããããè¿ãIterator
ã®$ys
ãããã¾ãã
ããããå¶æ°ã®è¦ç´ ãäºä¹ãã¦5ã¤ã ãåãåºãããã¨ãã¾ãã
foreach
ã使ã£ã¦èªåã§ã«ã¼ããã¦break
ããã®ãå¹ççãªã®ã¯å½ç¶ã§ãããããã§ã¯é«éé¢æ°ã使ã£ã¦æ¸ãã¦ã¿ã¾ãã
$even = function($n) { return $n % 2 === 0 }; $square = function($n) { return $n * $n; }; array_slice(array_map($square, array_filter(iterator_to_array($ys, false), $even)), 0, 5); // [0, 4, 16, 36, 64]
ãã®ã³ã¼ãã«ã¯å¹çä¸ã®åé¡ã2ã¤ããã¾ãã
- è¦ç´ 5ã¤åã®ã¡ã¢ãªç©ºéã§ååãªã®ã«
Iterator
ã®é·ãåã ã空éãå¿ è¦ã¨ãã - ä»åå¿
è¦ã¨ããªã5åç®ä»¥éã®è¦ç´ ã«ã¤ãã¦ã
$square
ãå¼ã³ã ããã¦ãã¾ã
èªåã§ã«ã¼ããæ¸ãã¦å¦çããã°ããã®ã§ããé¢åã§ããã
ãããªã以ä¸ã®ãããªæãã§Iterator
ããã§ã¤ã³ãã¦é
延ãªã¹ãã¨ãã¦å¦çãã¦ãã¾ãã¾ãããã
$xs = new TakeIterator(new MapIterator(new FilterIterator($ys, $even), $square), 5); iterator_to_array($xs); // [0, 4, 16, 36, 64]
ããã§ããããã«ã¼ããæ¸ããªãã¦ãæ¸ã¿ã¾ãããå¹çä¸ã®åé¡ã解決ãã¾ããã
Underbar.phpã®å®è£
ãã¾ãã«ãã®ããã«ãªã£ã¦ãã¾ãã
Underbar.phpã¨ã¯
Underbar.phpã¯ããã¾ã§ã«è¿°ã¹ãåé¡ã解決ããããã«éçºãããPHPã®ã³ã¬ã¯ã·ã§ã³å¦çã®ããã®ã©ã¤ãã©ãªã§ãã
ãã®åã®éãUnderscore.jsã«ã¤ã³ã¹ãã¤ã¢ãããã©ã¤ãã©ãªã§ããã移æ¤ã¨ããããã§ã¯ãªãã®ã§ããã»ã©äºææ§ã¯éè¦è¦ãã¦ãã¾ããã
詳ãã使ãæ¹ã¯å
¬å¼ããã¥ã¡ã³ããèªãã§é ãã¨ãã¦ã
é
延ãªã¹ããå©ç¨ãã¦ãã£ããããæ°åã10ååãåºãä¾ãå¼ç¨ãã¦ã¿ã¾ãã
use Underbar\IteratorImpl as _; // Fibonacci sequence echo _::chain([1, 1]) ->iterate(function($pair) { return [$pair[1], _::sum($pair)]; }) ->map(function($pair) { return $pair[0]; }) ->take(10) ->join(); // 1,1,2,3,5,8,13,21,34,55
ä»åã®ä¾ã§ã¯Iterator
ãè¿ãIteratorImpl
ã使ã£ã¦ãã¾ãããä»ã«ArrayImpl
ã¨GeneratorImpl
ãããã¾ãã
ãã®ä»ã®PHPã®ã³ã¬ã¯ã·ã§ã³å¦çã©ã¤ãã©ãªé
PHPã®array
ãæ±ãã¥ããåé¡ã¯PHPerã®å
±éè¦è§£ã®ããã§Underbar.php以å¤ã«ãããã¤ãã³ã¬ã¯ã·ã§ã³å¦çã©ã¤ãã©ãªãéçºããã¦ãã¾ãã
大ããUnderscore.jsç³»ã¨LINQç³»ã«åãããã¨ãã§ãã¾ãã
Underscore.jsç³»
èªåã1ã¤å¢ããã¦ãã¾ã£ãããã§ãããã¡ãã£ã¨ä¹±ç«æ°å³ã§ããã
æ¬æ¥ãããã£ãæ©è½ã¯è¨èªã®çµã¿è¾¼ã¿ã«ãããªããä½ãçµ±ä¸ãå³ãã¹ããªã®ããããã¾ããã
ã¾ãPHPãªã®ã§ãããããªãã§ããï¼
ãã®ä¸ã ã¨æ¬å®¶ã®Underscore.jsãå³æå®è¡ããå®è£
ãªã®ããã£ã¦ãé
延å®è¡ã§ããå®è£
ã¯ããã¾ããã
ãããUnderbar.phpã®æåã®éçºã®åæ©ã§ããã
LINQç³»
LINQç³»ã®ã©ã¤ãã©ãªãçµæ§æ°ãããã¾ãããæå¾çºã®Ginqãè¨è¨ãæ´ç·´ããã¦ãã¦é常ã«è¯ãã§ãã¦ãã¾ãããã®ã¹ã©ã¤ãã§å¤§ä½ã®é°å²æ°ãæ´ãããã¨æãã¾ãã
LINQ好ãã¯Ginq使ã£ã¦ããã°ééããªãã§ããã
Underbar.phpã«ã¯ãªãjoin
ã«ãããã¼ã¿ã®çµåãã½ã¼ãã®åæãªã©ãããã®ã§ããããã§ãã
ã¾ã¨ã
PHPã®é
åãæ±ãé¢æ°ã¯é常ã«ä½¿ãåæãæªãã®ã§ãæ§ã
ãªã³ã¬ã¯ã·ã§ã³å¦çã®ããã®ã©ã¤ãã©ãªãéçºããã¦ãã¾ãã
ããããä¸ã§ãé
延ãªã¹ããæ±ãããã®ã¯å°ãªãã®ã§ããããã£ãæå³ã§ã¯Underbar.phpãé¸æè¢ã®ä¸ã¤ã«ãªãããã¨æãã¾ãã
Underbar.phpã¯ä»å¾ç¤¾å
ã§ãå©ç¨ãããã¨ããåããããã¾ãããã¾ã èªçããã°ããã®ã©ã¤ãã©ãªãªã®ã§ä¸å
·åãè¦æçããã°pull requestãIssuesã®æ¹ã«ãé¡ããã¾ãã
PHPã®ã³ã¬ã¯ã·ã§ã³å¦çã©ã¤ãã©ãªUnderbar.phpã®ç´¹ä»
æå¾ã«ç¤¾å
åå¼·ä¼ã§ã®Underbar.phpçºè¡¨ã®æ§åã¨ã¹ã©ã¤ãã§ãã