Skip to content

Instantly share code, notes, and snippets.

View drupol's full-sized avatar

Pol Dellaiera drupol

View GitHub Profile
@drupol
drupol / primes.php
Last active September 1, 2020 07:59
Minimalist Prime numbers generator in PHP (version >= 7.4) - Can you make it shorter?
<?php
/**
* Run this code with: "php -n <file.php>" to make sure no configuration will be used
* so xdebug will not be used either.
*/
declare(strict_types=1);
function primesGenerator(Iterator $iterator): Generator
@drupol
drupol / primes.php
Created October 8, 2020 20:30
primes.php with IteratorAggregate
<?php
/**
* Run this code with: "php -n <file.php>" to make sure no configuration will be used
* so xdebug will not be used either.
*/
declare(strict_types=1);
namespace App;
<?php
declare(strict_types=1);
namespace App;
use drupol\launcher\Launcher;
use loophp\phptree\Builder\Random;
use loophp\phptree\Exporter\Gv;
use loophp\phptree\Exporter\Image;
@drupol
drupol / factorial.php
Last active November 10, 2020 14:46
factorial.php
<?php
// Programmation imperative
$factorial = static function(int $x): int {
$factorial = 1;
for ($i = 2; $i <= $x; $i++) {
$factorial *= $i;
}
@drupol
drupol / fibonacci.php
Last active November 29, 2020 10:42
Fibonacci / Memoization / Solution
<?php
declare(strict_types=1);
namespace App;
use ArrayObject;
use Closure;
use Generator;
@drupol
drupol / stream-iterator.php
Last active December 9, 2020 13:01
Stream Iterator oddity
<?php
declare(strict_types=1);
namespace App;
use Generator;
use Iterator;
class ClosureIterator implements \Iterator
@drupol
drupol / wallis.php
Last active February 25, 2021 14:07
Find Pi using Wallis formula
<?php
/**
* Wallis formula
*
* 2 2 4 4 6 6 8 8
* pi = 2 x --- x --- x --- x --- x --- x --- x --- x --- x ...
* 1 3 3 5 5 7 7 9
*/
@drupol
drupol / primes.php
Last active December 30, 2020 17:56
Generating Prime Numbers with loophp/collection
<?php
declare(strict_types=1);
include __DIR__ . '/../vendor/autoload.php';
use loophp\collection\Collection;
$integers = Collection::unfold(static fn (int $n = 1): int => $n + 1);
@drupol
drupol / test.php
Created March 10, 2021 19:47
Trying to decode a token with a ES256 signature.
<?php
namespace App;
use Facile\JoseVerifier\JWK\JwksProviderBuilder;
use Facile\JoseVerifier\JWTVerifier;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\HttpClient\Psr18Client;
include __DIR__ . '/vendor/autoload.php';
<?php
// Factorial of a number
printf("\nype a number: ")
&& ($g = (int) trim(fgets(STDIN))) && (fn(int $g, $y) =>
printf($y(static fn (callable $function) => static fn ($n = 1) => (1 >= $n) ? 1 : $n * $function($n - 1))($g))
)(
$g,
static fn (callable $g): callable => (static fn ($f): callable => $f($f))(static fn ($f): callable => $g(static fn ($x) => $f($f)($x)))
);