Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
* develop:
  specify next release
  add Sequence::prepend()
  • Loading branch information
Baptouuuu committed Jun 25, 2024
2 parents 2ec917c + 46e5733 commit d861ab3
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 5.7.0 - 2024-06-25

### Added

- `Innmind\Immutable\Sequence::prepend()`

## 5.6.0 - 2024-06-15

### Added
Expand Down
9 changes: 9 additions & 0 deletions docs/structures/sequence.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ $sequence = Sequence::ints(1, 2)->append(Sequence::ints(3, 4));
$sequence->equals(Sequence::ints(1, 2, 3, 4)); // true
```

### `->prepend()`

This is similar to `->append()` except the order is switched.

!!! success ""
The main advantage of this method is when using lazy sequences. If you want to add elements at the beginning of a sequence but the rest may be lazy then you need to create a lazy sequence with your values and then append the other lazy sequence; but this reveals the underlying lazyness of the call and you need to be aware that it could be lazy.

Instead by using this method you no longer have to be aware that the other sequence is lazy or not.

## Access values

### `->size()` :material-memory-arrow-down:
Expand Down
39 changes: 39 additions & 0 deletions proofs/sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
declare(strict_types = 1);

use Innmind\Immutable\Sequence;
use Innmind\BlackBox\Set;

return static function() {
yield test(
Expand All @@ -15,4 +16,42 @@ static function($assert) {
);
},
);

yield proof(
'Sequence::prepend()',
given(
Set\Sequence::of(Set\Type::any()),
Set\Sequence::of(Set\Type::any()),
),
static function($assert, $first, $second) {
$assert->same(
[...$first, ...$second],
Sequence::of(...$second)
->prepend(Sequence::of(...$first))
->toList(),
);

$assert->same(
[...$first, ...$second],
Sequence::defer((static function() use ($second) {
yield from $second;
})())
->prepend(Sequence::defer((static function() use ($first) {
yield from $first;
})()))
->toList(),
);

$assert->same(
[...$first, ...$second],
Sequence::lazy(static function() use ($second) {
yield from $second;
})
->prepend(Sequence::lazy(static function() use ($first) {
yield from $first;
}))
->toList(),
);
},
);
};
14 changes: 14 additions & 0 deletions src/Sequence.php
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,20 @@ public function append(self $sequence): self
));
}

/**
* Prepend the given sequence to the current one
*
* @param self<T> $sequence
*
* @return self<T>
*/
public function prepend(self $sequence): self
{
return new self($this->implementation->prepend(
$sequence->implementation,
));
}

/**
* Return a sequence with all elements from the current one that exist
* in the given one
Expand Down
23 changes: 23 additions & 0 deletions src/Sequence/Defer.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,29 @@ public function append(Implementation $sequence): Implementation
);
}

/**
* @param Implementation<T> $sequence
*
* @return Implementation<T>
*/
public function prepend(Implementation $sequence): Implementation
{
/** @psalm-suppress ImpureFunctionCall */
return new self(
(static function(\Iterator $values, Implementation $sequence): \Generator {
/** @var T $value */
foreach ($sequence->iterator() as $value) {
yield $value;
}

/** @var T $value */
foreach ($values as $value) {
yield $value;
}
})($this->values, $sequence),
);
}

/**
* @param Implementation<T> $sequence
*
Expand Down
9 changes: 9 additions & 0 deletions src/Sequence/Implementation.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ public function takeEnd(int $size): self;
*/
public function append(self $sequence): self;

/**
* Prepend the given sequence to the current one
*
* @param self<T> $sequence
*
* @return self<T>
*/
public function prepend(self $sequence): self;

/**
* Return a sequence with all elements from the current one that exist
* in the given one
Expand Down
25 changes: 25 additions & 0 deletions src/Sequence/Lazy.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,31 @@ static function(RegisterCleanup $registerCleanup) use ($values, $sequence): \Gen
);
}

/**
* @param Implementation<T> $sequence
*
* @return Implementation<T>
*/
public function prepend(Implementation $sequence): Implementation
{
$values = $this->values;

return new self(
static function(RegisterCleanup $registerCleanup) use ($values, $sequence): \Generator {
/** @var \Iterator<int, T> */
$generator = self::open($sequence, $registerCleanup);

foreach ($generator as $value) {
yield $value;
}

foreach ($values($registerCleanup) as $value) {
yield $value;
}
},
);
}

/**
* @param Implementation<T> $sequence
*
Expand Down
16 changes: 16 additions & 0 deletions src/Sequence/Primitive.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,22 @@ public function append(Implementation $sequence): self
return new self(\array_merge($this->values, $other));
}

/**
* @param Implementation<T> $sequence
*
* @return self<T>
*/
public function prepend(Implementation $sequence): self
{
$other = [];

foreach ($sequence->iterator() as $value) {
$other[] = $value;
}

return new self(\array_merge($other, $this->values));
}

/**
* @param Implementation<T> $sequence
*
Expand Down

0 comments on commit d861ab3

Please sign in to comment.