yuyat/piper

帮助以面向对象风格进行函数式编程。

v0.0.1 2014-10-09 16:57 UTC

This package is auto-updated.

Last update: 2024-08-25 21:09:32 UTC


README

帮助以面向对象风格进行函数式编程。

建议将此库与其他函数式库(如 iter)结合使用。

用法

基本的Piper

<?php
use yuyat\Piper;

use function iter\fn\operator;
use function iter\range;

$result = Piper::from(range(1, 10))
    ->pipe('iter\map', [operator('*', 2)])
    ->pipe(function ($iter) { return reduce(operator('+'), $iter, 0); })
    ->get();

echo $result, PHP_EOL;
// => 110

您的自定义Piper

<?php
use yuyat\Piper;

use function iter\fn\operator;
use function iter\range;
use function iter\map;
use function iter\reduce;

class IterPiper extends Piper
{
    public function map($fn)
    {
        return new static(map($fn, $this->get()));
    }

    public function reduce($fn, $initial = null)
    {
        return new static(reduce($fn, $this->get(), $initial));
    }
}

$result = IterPiper::from(range(1, 10))
    ->map(operator('*', 2))
    ->reduce(operator('+'), 0)
    ->get();

echo $result, PHP_EOL;
// => 110

作者

Yuya Takeyama