Skip to content

Commit

Permalink
refactor: upgrade docs for PHP 8
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 3, 2022
1 parent 4a4a60b commit 1d9c134
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 172 deletions.
18 changes: 4 additions & 14 deletions docs/pages/code/gamma.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

use loophp\collection\Collection;

$addition = static function (float $value1, float $value2): float {
return $value1 + $value2;
};
$addition = static fn (float $value1, float $value2): float => $value1 + $value2;

$listInt = static function (int $init, callable $succ): Generator {
yield $init;
Expand All @@ -18,19 +16,11 @@
}
};

$N = $listInt(1, static function (int $n): int {
return $n + 1;
});
$N = $listInt(1, static fn (int $n): int => $n + 1);

$Y = static function (float $n): Closure {
return static function (int $x) use ($n): float {
return ($x ** ($n - 1)) * (\M_E ** (-$x));
};
};
$Y = static fn (float $n): Closure => static fn (int $x): float => ($x ** ($n - 1)) * (\M_E ** (-$x));

$e = static function (float $value): bool {
return 10 ** -12 > $value;
};
$e = static fn (float $value): bool => 10 ** -12 > $value;

// Find the factorial of this number. This is not bounded to integers!
// $number = 3; // 2 * 2 => 4
Expand Down
4 changes: 2 additions & 2 deletions docs/pages/code/monte-carlo.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
use loophp\collection\Collection;

$monteCarloMethod = static function ($in = 0, $total = 1): array {
$randomNumber1 = mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax();
$randomNumber2 = mt_rand(0, mt_getrandmax() - 1) / mt_getrandmax();
$randomNumber1 = random_int(0, mt_getrandmax() - 1) / mt_getrandmax();
$randomNumber2 = random_int(0, mt_getrandmax() - 1) / mt_getrandmax();

if (1 >= (($randomNumber1 ** 2) + ($randomNumber2 ** 2))) {
++$in;
Expand Down
16 changes: 4 additions & 12 deletions docs/pages/code/operations/associate.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@

Collection::fromIterable($input)
->associate(
static function ($key, $value) {
return $key * 2;
},
static function ($value, $key) {
return $value * 3;
}
static fn ($key, $value) => $key * 2,
static fn ($value, $key) => $value * 3
);

// [
Expand All @@ -34,9 +30,7 @@ static function ($value, $key) {

Collection::fromIterable($input)
->associate(
static function ($key, $value) {
return $key * 2;
}
static fn ($key, $value) => $key * 2
);

// [
Expand All @@ -53,9 +47,7 @@ static function ($key, $value) {
Collection::fromIterable($input)
->associate(
null,
static function ($value, $key) {
return $value * 3;
}
static fn ($value, $key) => $value * 3
);

// [
Expand Down
15 changes: 3 additions & 12 deletions docs/pages/code/operations/distinct.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
// Example 2 -> Using a custom comparator callback, with object values
final class User
{
private string $name;

public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}

public function name(): string
Expand All @@ -44,11 +41,8 @@ public function name(): string
// Example 3 -> Using a custom accessor callback, with object values
final class Person
{
private string $name;

public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}

public function name(): string
Expand All @@ -73,11 +67,8 @@ public function name(): string
// Example 4 -> Using both accessor and comparator callbacks, with object values
final class Cat
{
private string $name;

public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}

public function name(): string
Expand Down
15 changes: 3 additions & 12 deletions docs/pages/code/operations/duplicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
// Example 2 -> Using a custom comparator callback, with object values
final class User
{
private string $name;

public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}

public function name(): string
Expand All @@ -44,11 +41,8 @@ public function name(): string
// Example 3 -> Using a custom accessor callback, with object values
final class Person
{
private string $name;

public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}

public function name(): string
Expand All @@ -73,11 +67,8 @@ public function name(): string
// Example 4 -> Using both accessor and comparator callbacks, with object values
final class Cat
{
private string $name;

public function __construct(string $name)
public function __construct(private string $name)
{
$this->name = $name;
}

public function name(): string
Expand Down
6 changes: 3 additions & 3 deletions docs/pages/code/operations/since.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
// Implode characters to create a line string
->map(static fn (array $characters): string => implode('', $characters))
// Skip items until the string "require-dev" is found.
->since(static fn ($line): bool => false !== strpos($line, 'require-dev'))
->since(static fn ($line): bool => str_contains($line, 'require-dev'))
// Skip items after the string "}" is found.
->until(static fn ($line): bool => false !== strpos($line, '}'))
->until(static fn ($line): bool => str_contains($line, '}'))
// Re-index the keys
->normalize()
// Filter out the first line and the last line.
->filter(
static fn ($line, $index): bool => 0 !== $index,
static fn ($line): bool => false === strpos($line, '}')
static fn ($line): bool => !str_contains($line, '}')
)
// Trim remaining results and explode the string on ':'.
->map(
Expand Down
4 changes: 1 addition & 3 deletions docs/pages/code/operations/squash.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,5 @@ static function (string $doc): string {
// If no exception, you can continue the processing...
$results = $results
->filter(
static function (string $document): bool {
return false !== strpos($document, 'foobar');
}
static fn (string $document): bool => str_contains($document, 'foobar')
);
128 changes: 52 additions & 76 deletions docs/pages/code/parse-git-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,24 @@
fclose($fh);
};

$buildIfThenElseCallbacks = static function (string $lineStart): array {
return [
static function ($line) use ($lineStart): bool {
return is_string($line) && 0 === mb_strpos($line, $lineStart);
},
static function ($line) use ($lineStart): array {
[, $line] = explode($lineStart, $line);
$buildIfThenElseCallbacks = static fn (string $lineStart): array => [
static fn ($line): bool => is_string($line) && 0 === mb_strpos($line, $lineStart),
static function ($line) use ($lineStart): array {
[, $line] = explode($lineStart, $line);

return [
sprintf(
'%s:%s',
mb_strtolower(str_replace(':', '', $lineStart)),
trim($line)
),
];
},
];
};
return [
sprintf(
'%s:%s',
mb_strtolower(str_replace(':', '', $lineStart)),
trim($line)
),
];
},
];

$c = Collection::fromIterable($commandStream('git log'))
->map(
static function (string $value): string {
return trim($value);
}
static fn (string $value): string => trim($value)
)
->compact('', ' ', "\n")
->ifThenElse(...$buildIfThenElseCallbacks('commit'))
Expand All @@ -52,73 +46,55 @@ static function (string $value): string {
->ifThenElse(...$buildIfThenElseCallbacks('Signed-off-by:'))
->split(
Splitable::BEFORE,
static function ($value): bool {
return is_array($value) ?
(1 === preg_match('/^commit:\b[0-9a-f]{5,40}\b/', $value[0])) :
false;
}
static fn ($value): bool => is_array($value) ?
(1 === preg_match('/^commit:\b[0-9a-f]{5,40}\b/', $value[0])) :
false
)
->map(
static function (array $value): CollectionInterface {
return Collection::fromIterable($value);
}
static fn (array $value): CollectionInterface => Collection::fromIterable($value)
)
->map(
static function (CollectionInterface $collection): CollectionInterface {
return $collection
->groupBy(
static function ($value): ?string {
return is_array($value) ? 'headers' : null;
}
)
->groupBy(
static function ($value): ?string {
return is_string($value) ? 'log' : null;
}
)
->ifThenElse(
static function ($value, $key): bool {
return 'headers' === $key;
},
static function ($value, $key): array {
return Collection::fromIterable($value)
->unwrap()
->associate(
static function ($carry, $key, string $value): string {
[$key, $line] = explode(':', $value, 2);
static fn (CollectionInterface $collection): CollectionInterface => $collection
->groupBy(
static fn ($value): ?string => is_array($value) ? 'headers' : null
)
->groupBy(
static fn ($value): ?string => is_string($value) ? 'log' : null
)
->ifThenElse(
static fn ($value, $key): bool => 'headers' === $key,
static fn ($value, $key): array => Collection::fromIterable($value)
->unwrap()
->associate(
static function ($carry, $key, string $value): string {
[$key, $line] = explode(':', $value, 2);

return $key;
},
static function ($carry, $key, string $value): string {
[$key, $line] = explode(':', $value, 2);
return $key;
},
static function ($carry, $key, string $value): string {
[$key, $line] = explode(':', $value, 2);

return trim($line);
}
)
->all();
}
);
}
return trim($line);
}
)
->all()
)
)
->map(
static function (CollectionInterface $collection): CollectionInterface {
return $collection
->flatten()
->groupBy(
static function ($value, $key): ?string {
if (is_numeric($key)) {
return 'log';
}

return null;
static fn (CollectionInterface $collection): CollectionInterface => $collection
->flatten()
->groupBy(
static function ($value, $key): ?string {
if (is_numeric($key)) {
return 'log';
}
);
}

return null;
}
)
)
->map(
static function (CollectionInterface $collection): array {
return $collection->all();
}
static fn (CollectionInterface $collection): array => $collection->all()
)
->limit(100);

Expand Down
Loading

0 comments on commit 1d9c134

Please sign in to comment.