bbujisic/functional

PHP功能原语的另一种实现方式

dev-master 2018-11-21 10:40 UTC

This package is auto-updated.

Last update: 2024-09-21 23:14:36 UTC


README

Build Status Coverage Status

没有特别之处。它只是为更易于理解的代码添加了一些语法糖。

示例

<?php

use bbujisic\functional\Collection;

$collection = new Collection([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);

$sumSquaresOfOddItems = $collection
  ->filter(function ($x) { return $x % 2 == 0; })
  ->map(function ($x) { return $x * $x; })
  ->reduce(function($x, $y) { return $x + $y; });

类似的可比命令式编程可能会稍微好一点,但可读性会稍微差一些。

<?php

$collection = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
$sumSquaresOfOddItems = 0;
foreach ($collection as $item) {
  if ($item % 2 == 0) {
    $sumSquaresOfOddItems += $item * $item;
  }
}