Skip to content

Commit

Permalink
refactor: Rename Run into Pipe.
Browse files Browse the repository at this point in the history
BREAKING CHANGE: yes
  • Loading branch information
drupol committed Oct 7, 2020
1 parent f6828f0 commit e3b3261
Show file tree
Hide file tree
Showing 27 changed files with 252 additions and 210 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ This library has been inspired by:
// Using single operations.
$filter = Filter::of()($filterCallback);
$reverse = Reverse::of();
$compose = Compose::of()($reverse, $filter);
print_r(iterator_to_array($compose(new ArrayIterator($data)))); // ['baz', 'bar']
$pipe = Pipe::of()($reverse, $filter);

print_r(iterator_to_array($pipe(new ArrayIterator($data)))); // ['baz', 'bar']
```

More information about this in the [Brian Lonsdorf's conference][brian lonsdorf conference], even if
Expand All @@ -115,9 +116,9 @@ This library has been inspired by:
$userData = new ArrayIterator($input);

$flatMap = static fn (callable $callable) =>
Compose::of()(
Map::of()($callable),
Flatten::of()(1),
Pipe::of()(
Map::of()($callable),
Flatten::of()(1),
Normalize::of()
);

Expand Down
80 changes: 40 additions & 40 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,46 @@ public function it_can_permutate(): void
);
}

public function it_can_pipe(Operation $operation): void
{
$square = new class() extends AbstractOperation implements Operation {
public function __invoke(): Closure
{
return static function ($collection): Generator {
foreach ($collection as $item) {
yield $item ** 2;
}
};
}
};

$sqrt = new class() extends AbstractOperation implements Operation {
public function __invoke(): Closure
{
return static function ($collection) {
foreach ($collection as $item) {
yield $item ** .5;
}
};
}
};

$castToInt = new class() extends AbstractOperation implements Operation {
public function __invoke(): Closure
{
return static function ($collection) {
foreach ($collection as $item) {
yield (int) $item;
}
};
}
};

$this::fromIterable(range(1, 5))
->pipe($square(), $sqrt(), $castToInt())
->shouldIterateAs(range(1, 5));
}

public function it_can_pluck(): void
{
$six = new class() {
Expand Down Expand Up @@ -1720,46 +1760,6 @@ public function it_can_rsample(): void
->shouldNotHaveCount(10);
}

public function it_can_run_an_operation(Operation $operation): void
{
$square = new class() extends AbstractOperation implements Operation {
public function __invoke(): Closure
{
return static function ($collection): Generator {
foreach ($collection as $item) {
yield $item ** 2;
}
};
}
};

$sqrt = new class() extends AbstractOperation implements Operation {
public function __invoke(): Closure
{
return static function ($collection) {
foreach ($collection as $item) {
yield $item ** .5;
}
};
}
};

$castToInt = new class() extends AbstractOperation implements Operation {
public function __invoke(): Closure
{
return static function ($collection) {
foreach ($collection as $item) {
yield (int) $item;
}
};
}
};

$this::fromIterable(range(1, 5))
->run($square(), $sqrt(), $castToInt())
->shouldIterateAs(range(1, 5));
}

public function it_can_scale(): void
{
$input = [0, 2, 4, 6, 8, 10];
Expand Down

0 comments on commit e3b3261

Please sign in to comment.