Group by a key (if a callable is used - return the value to group by)
@param  string|callable $iterator the key to group by
@return Collection
341607
public function groupBy($iterator) : Collection
    {
        $rslt = [];
        $temp = $this->toArray();
        foreach ($temp as $k => $v) {
            $rslt[is_string($iterator) ? (is_object($v) ? $v->{$iterator} : $v[$iterator]) : call_user_func($iterator, $v, $k)][] = $v;
        }
        return new static($rslt);
    }
1274251
public function map(array $collection, Closure $iterator)
    {
        foreach ($collection as $key => $node)
        {
            $collection[$key] = $iterator($key, $node);
        }

        return $collection;
    }
25630
public function group(callable $callback) {
		$array = array();
		foreach ($this->getIterator() as $index => $item) {
			$group = $callback($item);
			$array[$group][] = $item;
		}
		return new static($array);
	}
126478
public function map(callable $converter)
    {
        return new Enumerator(
            function () use($converter) {
                foreach ($this->each() as $k => $v) {
                    $v = call_user_func($converter, $v, $k);
                    yield $k => $v;
                }
            }
        );
    }
1287093
public function getIterator(): \Iterator
    {
        if ($this->generator instanceof ArrayyRewindableGenerator) {
            return $this->generator;
        }

        $iterator = $this->getIteratorClass();

        if ($iterator === ArrayyIterator::class) {
            return new $iterator($this->getArray(), 0, static::class, false);
        }

        return new $iterator($this->getArray());
    }
501750
public function collection(TableQueryIterator $iterator, Table $definition) : Collection
    {
        return Collection::from($iterator)
            ->map(function ($v) use ($definition) {
                return $this->entity($definition, $v);
            });
    }
993002
public static function sortBy($collection = null, $iterator = null, $sort_flag = null)
    {
        $results = array();
        foreach ($collection as $k => $item) {
            if (is_callable($iterator)) {
                $results[$k] = $iterator($item);
            }
            if (is_object($item)) {
                $results[$k] = $item->$iterator;
            } elseif (is_array($item)) {
                $results[$k] = $item[$iterator];
            }
        }
        $sort_flag = (is_null($sort_flag)) ? SORT_REGULAR : $sort_flag;
        asort($results, $sort_flag);
        foreach ($results as $k => $v) {
            $results[$k] = $collection[$k];
        }
        return $results;
    }
82440
public static <T> Set<T> iteratorToSet(Iterator<T> iterator) {
        Set<T> set = new HashSet<>();
        while (iterator.hasNext()) {
            set.add(iterator.next());
        }
        return set;
    }
1113335
public function getIterator() {
        $iterator = new AppendIterator;

        if($this->wrapped_object instanceof IteratorAggregate) {
            $iterator->append($element->getIterator());
        }
        else {
            $iterator->append(new ArrayIterator($this->wrapped_object));
        }
        $iterator->append(parent::getIterator());
        return $iterator;
    }
1041557
private static function AtomizeIterator( $iter )
	{
		/**
		 * @var XPathNavigator $item
		 */
		foreach ( $iter as $item )
		{
			if ( $item->getIsNode() )
			{
				if ( $item->getNodeType() == XPathNodeType::Comment )
				{
					continue;
				}

				if ( $item->getNodeType() == XPathNodeType::Element )
				{
					$child = $item->CloneInstance();
					$result = $child->MoveToFirstChild();

					$items = array();

					if ( $child->getNodeType() == XPathNodeType::Text )
					{
						$items[] = $item->getTypedValue();
					}
					else
					{
						while ( $result )
						{
							$data = ExtFuncs::GetData( XPath2NodeIterator::Create( $child->CloneInstance() ) );
							if ( $data->MoveNext() )
							{
								$items[] = $data->getCurrent(); // ->getValue();
							}

							$result = $child->MoveToNext();
						}
					}

					if ( ! count( $items ) )
					{
						yield Undefined::getValue();
					}
					else if ( count( $items ) == 1 )
					{
						yield $items[0];
					}
					else
					{
						yield XPath2Item::fromValueAndType( implode( "", $items ), XmlSchema::$String );
					}

					return false;
				}
				else
				if ( is_null( $item->getTypedValue() ) )
				{
			    	if ( $item instanceof XPathNavigator )
					{
						/**
						 * @var XPathNavigator $nav
						 */
						$nav = $item;
						if ( ! is_null( $nav->getSchemaInfo() ) &&
							 ! is_null( $nav->getSchemaInfo()->getSchemaType() ) &&
							 ! ( $nav->getSchemaInfo()->getSchemaType() instanceof XmlSchemaSimpleType ) )
						{
							throw XPath2Exception::withErrorCodeAndParam( "FOTY0012", Resources::FOTY0012, new XmlQualifiedName( $nav->LocalName, $nav->NamespaceURI, false ) );
						}
					}
				}
			}

		    yield $item instanceof XPath2Item
		    	? $item
		    	: XPath2Item::fromValue( $item->GetTypedValue() );
		}
	}
778423
protected function _calculateValue(Iterator $iterator)
    {
        $value = parent::_calculateValue($iterator);

        return $this->_dereferenceTokens($value);
    }
435911
public function when($value, callable $callback): CollectionInterface
    {
        if ($value) {
            $this->generator = $callback($this)->generator;
        }

        return $this;
    }
236663
public function getIterator(): Iterator
    {
        $map = $this->map;
        foreach ($map as $ordinal => $value) {
            yield ($this->enumeration)::byOrdinal($ordinal) => $value;
        }
    }
1212319
public function reject(array $collection, Closure $iterator)
    {
        foreach ($collection as $key => $node)
        {
            if ( ! $iterator($node))
            {
                unset($collection[$key]);
            }
        }

        return array_values($collection);
    }
947864
public static <E> Iterable<E> asIterable(final Iterator<E> iter) {
		return new Iterable<E>() {
			@Override
			public Iterator<E> iterator() {
				return iter;
			}
		};
	}
1121990
public function getIterator() : iterable
    {
        foreach ($this->bufferedChanges as $change) {
            yield $change;
        }

        foreach ($this->unBufferedChanges ?? [] as $change) {
            $this->bufferedChanges[] = $change;

            yield $change;
        }

        $this->unBufferedChanges = null;
    }
560017
public function apply(\Iterator $set)
    {
        $func = $this->function;
        $data = iterator_to_array($set);

        usort($data, function ($i1, $i2) use ($func) {

            $val1 = $func($i1);
            $val2 = $func($i2);

            if ($val1 === $val2) {
                return 0;
            }

            return $val1 > $val2 ? 1 : -1;
        });

        return new \ArrayIterator($data);
    }