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.
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());
_::range(1,4)->map('$_ * 2')->dump();
// 2,4,6,8
$fn($_) == true
==> remain$fn($_) == false
==> remove
_::range(1,4)->filter('$_ % 2')->dump();
// 1,3
$fn($_) == true
==> remove$fn($_) == false
==> remain
_::range(1,4)->reject('$_ % 2')->dump();
// 2,4
_::from([1,1,2,1])->unique()->dump();
// 1
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]
not chainable
$max = _::from([1,2,3,4])->reduce('$_ > $_carry ? $_ : $_carry');
exchange value and key.
_::from(['a' => 1, 'b' => 2, 'c' => 3])->flip()->dump();
// [1 => 'a', 2 => 'b', 3 => 'c'];
export to variable
$val = _::from([1,2,3])->map('$_ * 2');
// equals
_::from([1,2,3])->map('$_ * 2')->assignTo($val);
$_->dump();
// equals
var_dump($_->toArray());