/** * @return RecursiveSequence */ public function getChildren() { $x = $this->current(); if ($this->canGoDeeper()) { return RecursiveSequence::make($x)->setMaxDepth($this->depth - 1); } else { return Sequence::make($x); } }
/** * Flatten a Sequence into a new Sequence. * * @param int $depth * @return static */ public function flatten($depth = -1) { $recursiveIterator = new RecursiveIteratorIterator(RecursiveSequence::make($this)->setMaxDepth($depth)); // Simulate array_merge by sequencing numeric keys but do not touch string keys. return static::make(IterationTraits::sequenceNumericKeys(Sequence::make($recursiveIterator))); }
/** * This is like the RX flatMap and concatMap, but does not flatten promises. * See: http://reactivex.io/documentation/operators/flatmap.html * Note: item order and keys are preserved * if there are duplicate keys, it is necessary to call ->values() to start the keys over from 0 before * converting to an array to avoid overwriting values. * * @param callable|null $fnMap($value, $key) -- optional map function, flattening is applied after the map * @return Sequence */ public function concatMap($fnMap = null) { $rawSeq = is_callable($fnMap) ? $this->map($fnMap) : $this; // Filter out anything that cannot be traversed. $seq = $rawSeq->filter(function ($value) { return is_array($value) || $value instanceof Traversable; }); return Sequence::make(new RecursiveIteratorIterator(RecursiveSequence::make($seq)->setMaxDepth(1))); }