1. Go to this page and download the library: Download loophp/combinator library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use loophp\combinator\Combinators;
$l = Combinators::L();
$inspectType = fn(mixed $arg): string => "Argument is of type: " . gettype($arg);
$selfApplicable = fn(callable $c): string => "I am a function.";
// Equivalent to $inspectType($selfApplicable($selfApplicable))
echo $l($inspectType)($selfApplicable); // Outputs: Argument is of type: string
use loophp\combinator\Combinators;
$m = Combinators::M();
$describe = fn(callable $c): string => "This is a closure.";
// Applies the $describe function to itself.
echo $m($describe); // Outputs: This is a closure.
use loophp\combinator\Combinators;
$o = Combinators::O();
// A curried function to prepend to an array
$prepend = fn(mixed $item): callable => fn(array $list): array => [$item, ...$list];
$reverse = 'array_reverse';
// Equivalent to $reverse($prepend(3)([1, 2])) -> $reverse([3, 1, 2])
$result = $o($prepend)([1, 2])($reverse)(3);
print_r($result); // Outputs: Array ( [0] => 2 [1] => 1 [2] => 3 )
// This will cause a fatal error due to infinite recursion.
// $omega = Combinators::Omega();
// $omega(fn($x) => $x);
use loophp\combinator\Combinators;
$s = Combinators::S();
$add = fn(int $x): callable => fn(int $y): int => $x + $y;
$square = fn(int $x): int => $x * $x;
// Equivalent to $add($x)($square($x)) where $x is 3. So 3 + (3*3)
echo $s($add)($square)(3); // Outputs: 12
use loophp\combinator\Combinators;
$s_ = Combinators::S_();
$log = fn(string $result): callable => fn(string $original): string => "Input '$original' produced '$result'";
$transform = 'strtoupper';
// Equivalent to $log(strtoupper('hello'))('hello')
$logTransformation = $s_($log)($transform);
echo $logTransformation('hello'); // Outputs: Input 'hello' produced 'HELLO'
use loophp\combinator\Combinators;
$s2 = Combinators::S2();
$applyAndFormat = fn(callable $operation): callable => fn(int $value): string => "Final result is: " . $operation($value);
$multiply = fn(int $x): callable => fn(int $y): int => $x * $y;
$addOne = fn(int $x): int => $x + 1;
$d = 5;
// Equivalent to $applyAndFormat($multiply(5))($addOne(5)) -> 5 * 6
echo $s2($applyAndFormat)($multiply)($addOne)($d); // Outputs: Final result is: 30
use loophp\combinator\Combinators;
$t = Combinators::T();
$multiplyByTwo = fn(int $x): int => $x * 2;
// Instead of $multiplyByTwo(5)
echo $t(5)($multiplyByTwo); // Outputs: 10
use loophp\combinator\Combinators;
$u = Combinators::U();
// The step function threads self-application to enable recursion.
// $self($self)($g) is evaluated lazily (inside the closure body).
$factStep = fn(callable $self) => fn(callable $g): callable =>
fn(int $n): int => $n === 0 ? 1 : $n * $self($self)($g)($n - 1);
// Pass I (identity) as the seed: U(factStep)(I) = I(factStep(factStep)(I)) = factStep(factStep)(I)
$factorial = $u($factStep)(Combinators::I());
echo $factorial(5); // Outputs: 120
use loophp\combinator\Combinators;
$v = Combinators::V();
$str_replace = 'str_replace';
$search = 'foo';
$replace = 'bar';
// Creates a function that replaces 'foo' with 'bar' in a given subject string.
$replaceFoo = $v($search)($replace)($str_replace);
echo $replaceFoo('this is foo'); // Outputs: this is bar
use loophp\combinator\Combinators;
$w = Combinators::W();
$add = fn(int $x): callable => fn(int $y): int => $x + $y;
// Equivalent to $add(5)(5)
echo $w($add)(5); // Outputs: 10