Reduce any iterable collection using the callback. In case
of an array, array_reduce is used.

@param callable $f
@param $xs
@param null $initial
@return mixed|null
@throws \InvalidArgumentException
555335
public static function reduce(callable $f, $xs, $initial = null)
    {
        if (is_array($xs)) {
            $initial = $initial !== null ? $initial : array_shift($xs);
            return array_reduce($xs, $f, $initial);
        }
        if ($xs instanceof \Iterator) {
            $xs->rewind();
            if (!$xs->valid()) {
                return $initial;
            }
            $acc = $initial;
            if ($acc === null) {
                $acc = $xs->current();
                $xs->next();
            }
            for (; $xs->valid(); $xs->next()) {
                $acc = call_user_func($f, $acc, $xs->current());
            }
            return $acc;
        }
        throw new \InvalidArgumentException();
    }
769534
public function reduce($filter, $initial = false) {
		if (!$this->closed()) {
			while ($this->next()) {}
		}
		return parent::reduce($filter, $initial);
	}
410100
public static function uniq($xs, $f = null)
    {
        $f = self::createCallback($f);
        return new Iterator\UniqueIterator(self::wrapIterator($xs), $f);
    }
479527
final public static function every($xs, $f = null)
    {
        $f = static::createCallback($f);

        foreach ($xs as $k => $x) {
            if (!call_user_func($f, $x, $k, $xs)) {
                return false;
            }
        }

        return true;
    }
1130678
public function map(callable $f) : NestableResult
    {
        return new self(
            $this->outer->map($f),
            array_map($f, $this->inner)
        );
    }
349338
public static function group(array $keys, array $query, $initial = null, $reduce = null)
    {

        if (!$reduce) $reduce = new \MongoCode('function (doc, out) { out.object = doc }');
        if(!$initial) $initial = array('object'=>0);

        return self::connection()->group(self::collectionName(), $keys, $initial, $reduce, array('condition'=>$query));

    }
876041
static function instance(\hlin\archetype\Filesystem $fs, array $filemaps) {
		$i = new static;
		$i->fs = $fs;
		$i->filemaps = $filemaps;
		return $i;
	}
571643
public function reduce(callable $callback, $init = null) {
		return array_reduce(iterator_to_array($this->getIterator()), $callback, $init);
	}
134838
public function reduce(callable $callback, $initial = null)
    {
        foreach ($this->generator as $key => $value) {
            $initial = $callback($initial, $value, $key);
        }

        return $initial;
    }
1273503
public function reduceRight(callable $callback, $initialValue = null)
    {
        return $this->reverse(true)->reduce($callback, $initialValue);
    }
187537
public function foldRight(callable $function, $initial = null)
    {
        if ($this->isEmpty()) {
            return $initial;
        }

        return array_reduce(array_reverse($this->data, true), $function, $initial);
    }
549724
public function reduce(): void
    {
        $rule = $this->stateRow->reduceRule;
        if (!$rule) {
            throw new NoReduceException();
        }
        $reduce_count = count($rule->getDefinition());
        $total_count = count($this->items);
        if ($total_count < $reduce_count) {
            throw new InternalException('Not enough items in stack');
        }
        $nodes = [];
        $offset = null;
        $reduce_items = array_slice($this->items, -$reduce_count);
        foreach ($rule->getDefinition() as $i => $symbol) {
            $item = $reduce_items[$i];
            if ($item->node->getNodeName() !== $symbol->getName()) {
                throw new InternalException('Unexpected stack content');
            }

            if (!$symbol->isHidden()) {
                $nodes[] = $item->node;
            }

            if (null === $offset) {
                $offset = $item->node->getOffset();
            }
        }

        $base_state_index = ($total_count > $reduce_count)
            ? $this->items[$total_count - 1 - $reduce_count]->state
            : 0;
        $base_state_row = $this->table->getRow($base_state_index);

        $new_symbol_name = $rule->getSubject()->getName();

        $new_node = new NonTerminal($new_symbol_name, $nodes, $rule->getTag(), $offset);

        $goto = $base_state_row->gotoSwitches;
        if (!isset($goto[$new_symbol_name])) {
            throw new InternalException('No required state in GOTO table');
        }
        $next_state = $goto[$new_symbol_name];

        array_splice($this->items, -$reduce_count);
        $this->shift($new_node, $next_state);
    }
424400
function(arr, fn, initial) {
  if (typeof arr.reduce === 'function') return arr.reduce(fn, initial);
  var res = initial;
  for (var i = 0; i < arr.length; i++) res = fn(res, arr[i]);
  return res;
}
1361505
static function of ($src)
  {
    $x = new static;
    switch (true) {
      case is_array ($src):
        $x->A = $src;
        break;
      case $src instanceof IteratorAggregate:
        $x->A = iterator_to_array ($src->getIterator ());
        break;
      case $src instanceof Iterator:
        $x->A = iterator_to_array ($src);
        break;
      default:
        throw new InvalidArgumentException;
    }
    return $x;
  }
575821
public function setInitializer(QueryInitializer $initial)
    {
        $this->initial = $initial;
        for ($i = 0; $i < $this->length(); ++$i) {
            if ($this->offsetExists($i)) {
                $this->offsetGet($i)->setInitializer($this->initial);
            }
        }
    }
196788
public static function map($xs, $f)
    {
        foreach ($xs as $i => $x) {
            yield $i => call_user_func($f, $x, $i, $xs);
        }
    }
643602
public function foldlWithKey(callable $closure, $initial)
    {
        $accumulator = $initial;

        foreach ($this->value as $key => $value) {
            $accumulator = Std::call($closure, $accumulator, $value, $key);
        }

        return $accumulator;
    }