Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/main/php/util/data/Sequence.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
21 changes: 21 additions & 0 deletions src/test/php/util/data/unittest/SequenceCreationTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
Loading