spindle/collection

The fastest collection(array_map, array_reduce, ..) wrapper

0.0.3 2017-06-25 10:34 UTC

This package is auto-updated.

Last update: 2024-07-04 14:24:49 UTC


README

spindle/collection

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version Total Downloads Latest Unstable Version License

The fastest collection library

  • construct code and run with eval()
  • very quick & low memory usage
  • You must not pass user's input to this library. It might causes code injection vulnerability.

Usage

composer require 'spindle/collection'
<?php
require_once 'vendor/autoload.php';

use Spindle\Collection\Collection as _;

_::range(1, 100)
    ->filter('$_ % 2')
    ->map('$_ * 2')
    ->assignTo($val);

var_dump($val->toArray());

Methods

map($fn)

_::range(1,4)->map('$_ * 2')->dump();
// 2,4,6,8

filter($fn)

  • $fn($_) == true ==> remain
  • $fn($_) == false ==> remove
_::range(1,4)->filter('$_ % 2')->dump();
// 1,3

reject($fn)

  • $fn($_) == true ==> remove
  • $fn($_) == false ==> remain
_::range(1,4)->reject('$_ % 2')->dump();
// 2,4

unique()

_::from([1,1,2,1])->unique()->dump();
// 1

column(array|string $columns)

Inspired by array_column.

$a = ['a' => 1, 'b' => 2, 'c' => 3];

_::from([$a, $a])->column(['a', 'c'])->dump();
// ['a' => 1, 'c' => 3], ['a' => 1, 'c' => 3];

_::from([$a, $a])->column('a')->dump();
// 1, 1;

_::from([$a, $a])->column(['a'])->dump();
// ['a' => 1], ['a' => 1]

slice($offset, $length = null)

chunk($size)

reduce($fn, $initial = null)

not chainable

$max = _::from([1,2,3,4])->reduce('$_ > $_carry ? $_ : $_carry');

sum()

product()

flip()

exchange value and key.

_::from(['a' => 1, 'b' => 2, 'c' => 3])->flip()->dump();
// [1 => 'a', 2 => 'b', 3 => 'c'];

sort($sort_flags = \SORT_REGULAR)

rsort($sort_flags = \SORT_REGULAR)

usort(callable $cmp)

assignTo(&$val)

export to variable

$val = _::from([1,2,3])->map('$_ * 2');
// equals
_::from([1,2,3])->map('$_ * 2')->assignTo($val);

dump()

$_->dump();
// equals
var_dump($_->toArray());