diff --git a/src/main/php/util/data/Sequence.class.php b/src/main/php/util/data/Sequence.class.php index d695c07..eed8797 100755 --- a/src/main/php/util/data/Sequence.class.php +++ b/src/main/php/util/data/Sequence.class.php @@ -95,6 +95,22 @@ public static function generate($supplier) { return new self($f()); } + /** + * Creates a new stream with an enumeration of elements + * + * @param ?iterable|util.XPIterator|function(): iterable $enumerable + * @return self + * @throws lang.IllegalArgumentException if type of elements argument is incorrect + */ + public function concat($enumerable) { + $enumeration= Enumeration::of($enumerable); + $f= function() use($enumeration) { + yield from $this->elements; + yield from $enumeration; + }; + return new self($f()); + } + /** * Returns the first element of this stream, or an empty optional * diff --git a/src/test/php/util/data/unittest/SequenceCreationTest.class.php b/src/test/php/util/data/unittest/SequenceCreationTest.class.php index 764d2ba..734ec1b 100755 --- a/src/test/php/util/data/unittest/SequenceCreationTest.class.php +++ b/src/test/php/util/data/unittest/SequenceCreationTest.class.php @@ -73,4 +73,25 @@ public function passing_sequence_to_of_yields_itself() { public function multiple_arguments_supported_in_of(... $input) { $this->assertSequence([1, 2, 3, 4, 5, 6], Sequence::of(...$input)); } + + #[Test] + public function concat() { + $this->assertSequence( + [1, 2, 3, 4, 5, 6, 7, 8, 9], + Sequence::of([1, 2, 3])->concat([4, 5, 6])->concat([7, 8, 9]) + ); + } + + #[Test] + public function concat_after_mapping() { + $this->assertSequence( + [2, 4, 6, 'EOF'], + Sequence::of([1, 2, 3])->map(function($e) { return $e * 2; })->concat(['EOF']) + ); + } + + #[Test, Expect(IllegalArgumentException::class)] + public function cannot_concat_non_enumerable() { + Sequence::$EMPTY->concat('test'); + } } \ No newline at end of file