Enumerator
to generate Iterator
's; there were no useless collections, in fact the only advantage of which was the storage of objects in the keys; there was no call_user_func
... And the most important thing - there was no heap of low-grade code for generating iterators, which is impossible to understand. Stay foreach
and yield
. All this in the amount gave a sickly increase in speed. return new Enumerable(function () use ($self, $inner, $outerKeySelector, $innerKeySelector, $resultSelectorValue, $resultSelectorKey) { /** @var $self Enumerable */ /** @var $inner Enumerable */ /** @var $arrIn array */ $itOut = $self->getIterator(); $itOut->rewind(); $lookup = $inner->toLookup($innerKeySelector); $arrIn = null; $posIn = 0; $key = null; return new Enumerator(function ($yield) use ($itOut, $lookup, &$arrIn, &$posIn, &$key, $outerKeySelector, $resultSelectorValue, $resultSelectorKey) { /** @var $itOut \Iterator */ /** @var $lookup \YaLinqo\collections\Lookup */ while ($arrIn === null || $posIn >= count($arrIn)) { if ($arrIn !== null) $itOut->next(); if (!$itOut->valid()) return false; $key = call_user_func($outerKeySelector, $itOut->current(), $itOut->key()); $arrIn = $lookup[$key]; $posIn = 0; } $args = array($itOut->current(), $arrIn[$posIn], $key); $yield(call_user_func_array($resultSelectorValue, $args), call_user_func_array($resultSelectorKey, $args)); $posIn++; return true; }); });
return new Enumerable(function () use ($inner, $outerKeySelector, $innerKeySelector, $resultSelectorValue, $resultSelectorKey) { $lookup = $inner->toLookup($innerKeySelector); foreach ($this as $ok => $ov) { $key = $outerKeySelector($ov, $ok); if (isset($lookup[$key])) foreach ($lookup[$key] as $iv) yield $resultSelectorKey($ov, $iv, $key) => $resultSelectorValue($ov, $iv, $key); } });
composer.json
, so working with Composer should now cause less pain. $products = array( array('name' => 'Keyboard', 'catId' => 'hw', 'quantity' => 10, 'id' => 1), array('name' => 'Mouse', 'catId' => 'hw', 'quantity' => 20, 'id' => 2), array('name' => 'Monitor', 'catId' => 'hw', 'quantity' => 0, 'id' => 3), array('name' => 'Joystick', 'catId' => 'hw', 'quantity' => 15, 'id' => 4), array('name' => 'CPU', 'catId' => 'hw', 'quantity' => 15, 'id' => 5), array('name' => 'Motherboard', 'catId' => 'hw', 'quantity' => 11, 'id' => 6), array('name' => 'Windows', 'catId' => 'os', 'quantity' => 666, 'id' => 7), array('name' => 'Linux', 'catId' => 'os', 'quantity' => 666, 'id' => 8), array('name' => 'Mac', 'catId' => 'os', 'quantity' => 666, 'id' => 9), ); $categories = array( array('name' => 'Hardware', 'id' => 'hw'), array('name' => 'Operating systems', 'id' => 'os'), );
$result = from($categories) ->orderBy('$cat ==> $cat["name"]') ->groupJoin( from($products) ->where('$prod ==> $prod["quantity"] > 0') ->orderByDescending('$prod ==> $prod["quantity"]') ->thenBy('$prod ==> $prod["name"]'), '$cat ==> $cat["id"]', '$prod ==> $prod["catId"]', '($cat, $prods) ==> [ "name" => $cat["name"], "products" => $prods ]' );
$result = from($categories) ->orderBy($cat ==> $cat['name']) ->groupJoin( from($products) ->where($prod ==> $prod['quantity'] > 0) ->orderByDescending($prod ==> $prod['quantity']) ->thenBy($prod ==> $prod['name']), $cat ==> $cat['id'], $prod ==> $prod['catId'], ($cat, $prods) ==> [ 'name' => $cat['name'], 'products' => $prods, ] );
Array ( [hw] => Array ( [name] => Hardware [products] => Array ( [0] => Array ( [name] => Mouse [catId] => hw [quantity] => 20 [id] => 2 ) [1] => Array ( [name] => CPU [catId] => hw [quantity] => 15 [id] => 5 ) [2] => Array ( [name] => Joystick [catId] => hw [quantity] => 15 [id] => 4 ) [3] => Array ( [name] => Motherboard [catId] => hw [quantity] => 11 [id] => 6 ) [4] => Array ( [name] => Keyboard [catId] => hw [quantity] => 10 [id] => 1 ) ) ) [os] => Array ( [name] => Operating systems [products] => Array ( [0] => Array ( [name] => Linux [catId] => os [quantity] => 666 [id] => 8 ) [1] => Array ( [name] => Mac [catId] => os [quantity] => 666 [id] => 9 ) [2] => Array ( [name] => Windows [catId] => os [quantity] => 666 [id] => 7 ) ) ) )
'$prod ==> $prod["quantity"] > 0'
you can write function ($prod) { return $prod['quantity'] > 0; }
function ($prod) { return $prod['quantity'] > 0; }
. For wild obfuscators, it is possible to use the default argument names (v is the value, k is the key, etc.), that is, you can simply write '$v["quantity"] > 0'
(not recommended for complex nested queries).Source: https://habr.com/ru/post/229763/
All Articles