Skip to content

Commit

Permalink
docs: Move code examples into simple files.
Browse files Browse the repository at this point in the history
Signed-off-by: Pol Dellaiera <[email protected]>
  • Loading branch information
drupol committed Oct 11, 2020
1 parent 07b0868 commit 464ff15
Showing 14 changed files with 817 additions and 782 deletions.
23 changes: 23 additions & 0 deletions docs/pages/code/collatz-conjecture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;

// The Collatz conjecture (https://en.wikipedia.org/wiki/Collatz_conjecture)
$collatz = static function (int $value): int {
return 0 === $value % 2 ?
$value / 2 :
$value * 3 + 1;
};

$c = Collection::unfold($collatz, 25)
->until(
static function ($number): bool {
return 1 === $number;
}
);

print_r($c->all()); // [25, 76, 38, 19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
50 changes: 50 additions & 0 deletions docs/pages/code/e.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;

$multiplication = static function (float $value1, float $value2): float {
return $value1 * $value2;
};

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

$fact = static function (int $number) use ($multiplication): float {
return Collection::range(1, $number + 1)
->foldleft(
$multiplication,
1
)
->current();
};

$e = static function (int $value) use ($fact): float {
return $value / $fact($value);
};

$listInt = static function (int $init, callable $succ): Generator {
yield $init;

while (true) {
yield $init = $succ($init);
}
};

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

$number_e_approximation = Collection::fromIterable($naturals)
->map($e)
->until(static function (float $value): bool {
return 10 ** -12 > $value;
})
->foldleft($addition, 0)
->current();

var_dump($number_e_approximation); // 2.718281828459
17 changes: 17 additions & 0 deletions docs/pages/code/fibonacci.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;

$fibonacci = static function (int $a = 0, int $b = 1): array {
return [$b, $b + $a];
};

$c = Collection::unfold($fibonacci)
->pluck(0) // Get the first item of each result.
->limit(10); // Limit the amount of results to 10.

print_r($c->all()); // [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
46 changes: 46 additions & 0 deletions docs/pages/code/gamma.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;

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

$listInt = static function (int $init, callable $succ): Generator {
yield $init;

while (true) {
yield $init = $succ($init);
}
};

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

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

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

// Find the factorial of this number. This is not bounded to integers!
// $number = 3; // 2 * 2 => 4
// $number = 6; // 5 * 4 * 3 * 2 => 120
$number = 5.75; // 78.78

$gamma_factorial_approximation = Collection::fromIterable($)
->map($γ($number))
->until($ε)
->foldleft($addition, 0)
->current();

print_r($gamma_factorial_approximation);
35 changes: 35 additions & 0 deletions docs/pages/code/monte-carlo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;

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

if (1 >= (($randomNumber1 ** 2) + ($randomNumber2 ** 2))) {
++$in;
}

return ['in' => $in, 'total' => ++$total];
};

$pi_approximation = Collection::unfold($monteCarloMethod)
->map(
static function ($value) {
return 4 * $value['in'] / $value['total'];
}
)
->window(1)
->drop(1)
->until(
static function (array $value): bool {
return 0.00001 > abs($value[0] - $value[1]);
}
)
->last();

print_r($pi_approximation->all());
124 changes: 124 additions & 0 deletions docs/pages/code/parse-git-log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;
use loophp\collection\Contract\Collection as CollectionInterface;
use loophp\collection\Contract\Operation\Splitable;

$commandStream = static function (string $command): Generator {
$fh = popen($command, 'r');

while (false !== $line = fgets($fh)) {
yield $line;
}

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);

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);
}
)
->compact('', ' ', "\n")
->ifThenElse(...$buildIfThenElseCallbacks('commit'))
->ifThenElse(...$buildIfThenElseCallbacks('Date:'))
->ifThenElse(...$buildIfThenElseCallbacks('Author:'))
->ifThenElse(...$buildIfThenElseCallbacks('Merge:'))
->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;
}
)
->map(
static function (array $value): CollectionInterface {
return 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);

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

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;
}
);
}
)
->map(
static function (CollectionInterface $collection): array {
return $collection->all();
}
)
->limit(100);

print_r($c->all());
83 changes: 83 additions & 0 deletions docs/pages/code/primes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

declare(strict_types=1);

include __DIR__ . '/../../../vendor/autoload.php';

use loophp\collection\Collection;

$primesGenerator = static function (Iterator $iterator) use (&$primesGenerator): Generator {
yield $primeNumber = $iterator->current();

$iterator = new \CallbackFilterIterator(
$iterator,
static function (int $a) use ($primeNumber): bool {
return 0 !== $a % $primeNumber;
}
);

$iterator->next();

return $iterator->valid() ?
yield from $primesGenerator($iterator) :
null;
};

$integerGenerator = static function (int $init, callable $succ) use (&$integerGenerator): Generator {
yield $init;

return yield from $integerGenerator($succ($init), $succ);
};

$limit = 1000000;

$primes = $primesGenerator(
$integerGenerator(
2,
static function (int $n): int {
return $n + 1;
}
)
);

// Create a lazy collection of Prime numbers from 2 to infinity.
$lazyPrimeNumbersCollection = Collection::fromIterable(
$primesGenerator(
$integerGenerator(
2,
static function (int $n): int {
return $n + 1;
}
)
)
);

// Print out the first 1 million of prime numbers.
foreach ($lazyPrimeNumbersCollection->limit($limit) as $prime) {
var_dump($prime);
}

// Create a lazy collection of Prime numbers from 2 to infinity.
$lazyPrimeNumbersCollection = Collection::fromIterable(
$primesGenerator(
$integerGenerator(
2,
static function (int $n): int {
return $n + 1;
}
)
)
);

// Find out the Twin Prime numbers by filtering out unwanted values.
$lazyTwinPrimeNumbersCollection = Collection::fromIterable($lazyPrimeNumbersCollection)
->zip($lazyPrimeNumbersCollection->tail())
->filter(
static function (array $chunk): bool {
return 2 === $chunk[1] - $chunk[0];
}
);

foreach ($lazyTwinPrimeNumbersCollection->limit($limit) as $prime) {
var_dump($prime);
}
Loading

0 comments on commit 464ff15

Please sign in to comment.