imunhatep/collection

PHP 通用集合库

2.0.1 2023-01-23 16:17 UTC

README

SensioLabsInsight Build Status

PHP 集合库。

基于 J. M. Schmitt 的工作,重构和更新 - 现在需要 PHP 8.0。添加了几个有用的方法如 ::flatMap()、::map()、::foldLeft()、::exists()、::headOption()、::lastOption()、::tail() 等。灵感来源于 Scala 集合。增加了类型。

安装

PHP 集合可以通过 composer(正在处理)轻松安装

composer require imunhatep/collection

或将其添加到您的 composer.json 文件中。

注意

集合可以看作是更专业的数组,其中保证了一定的合同。

支持的集合

  • 序列

    • 键:数值,顺序递增,无间隔
    • 值:任何内容,允许重复
    • 类:SequenceSortedSequence
  • 映射

    • 键:字符串或对象,不允许重复键
    • 值:任何内容,允许重复
    • 类:MapLRUMap
    • 键:无意义
    • 值:对象,或标量,每个值都保证是唯一的(下面详细说明 Set 的使用情况)
    • 类:Set

一般特征

  • 集合是可变的(可以添加新元素,修改或删除现有元素)。将来可能会添加专门的不可变版本。
  • 元素之间的相等比较始终使用浅比较运算符(===)执行。
  • 排序算法是不稳定的,这意味着相等元素的顺序是未定义的(默认行为,也是 PHP 的唯一行为)。

使用

集合类提供了一个丰富的 API。

序列

    // Read Operations
    $seq = new Sequence([0, 2, 3, 2]);
    $seq->get(2); // int(3)
    $seq->all(); // [0, 2, 3, 2]

    $seq->head(); // Some(0)
    $seq->tail(); // Some(2)

    // Write Operations
    $seq = new Sequence([1, 5]);
    $seq->get(0); // int(1)
    
    $seq
      ->update(0, 4);
      ->get(0); // int(4)
    
    $seq
      ->remove(0)
      ->get(0); // int(5)

    $seq = new Sequence([1, 4]);
    $seq
      ->add(2)
      ->all(); // [1, 4, 2]
    
    $seq
      ->addAll([4, 5, 2])
      ->all(); // [1, 4, 2, 4, 5, 2]

    // Sort
    $seq = new Sequence([0, 5, 4, 2]);
    $seq
      ->sortWith(fn($a, $b) => ($a - $b));
      ->all(); // [0, 2, 4, 5]

映射

    // Read Operations
    $map = new Map(['foo' => 'bar', 'baz' => 'boo']);
    $map->get('foo'); // Some('bar')
    $map->get('foo')->get(); // string('bar')
    $map->keys(); // ['foo', 'baz']
    $map->values(); // ['bar', 'boo']

    $map->headOption(); // Some(['key', 'value'])
    $map->head();       // null | ['key', 'value']
    $map->lastOption(); // Some(['key', 'value'])
    $map->last();       // null | ['key', 'value']
    
    $map->headOption()->getOrElse([]); // ['foo', 'bar']
    $map->lastOption()->getOrElse([]); // ['baz', 'boo']
    
    $map->tail();            // ['baz' => 'boo']

    iterator_to_array($map); // ['foo' => 'bar', 'baz' => 'boo']
    $map->all()              // ['foo' => 'bar', 'baz' => 'boo']

    // Write Operations
    $map = new Map();
    $map->set('foo', 'bar');
    $map->setAll(['bar' => 'baz', 'baz' => 'boo']);
    $map->remove('foo');

    // Sort
    $map->sortWith('strcmp');

    // Transformation
    $map->map(fn($k,$v) => ($value * 2));
    
    $map->flatMap(fn($k,$v) => (new Map())->set($k, $v * 2) );
    
    $map->foldLeft(SomeObject $s, fn($s, $k, $v) => $s->add([$k, $v * 2]))

在 Set 中,每个值都是唯一的。Set 类支持对象和标量作为值。相等性通过以下步骤确定。

标量的相等性

Scalar are considered equal if ``$a === $b`` is true.
    class DateTimeHashable extends \DateTime implements HashableInterface
    {
        public function hash(): string
        {
            return $this->format("YmdP");
        }
    }
    
    $set = new Set();
    $set->add(new DateTimeHashable('today'));
    $set->add(new DateTimeHashable('today'));

    var_dump(count($set)); // int(1) -> the same date is not added twice

    foreach ($set as $date) {
        var_dump($date);
    }

    $set->all();
    $set->addSet($otherSet);
    $set->addAll($someElements);

    // Traverse
    $set->map(function($x) { return $x*2; });
    $set->flatMap(function($x) { return [$x*2, $x*4]; });