CKTIS / PHP
Composição de funções
Ao compor funções passe o resultado completo da primeira como argumento da segunda.
Versão: PHP 8.4; repository runtime, production runtime not attested
Sintaxe
fn($x) => $after($before($x));Ao compor funções passe o resultado completo da primeira como argumento da segunda. A ordem pode mudar o resultado mesmo com funções puras. Os tipos têm de ser compatíveis. Uma exceção inicial impede as chamadas seguintes.
function then(callable $first,callable $next):Closure{return fn(int $x)=>$next($first($x));}
$add=fn(int $v)=>$v+2;$double=fn(int $v)=>2*$v;echo json_encode([then($add,$double)(3),then($double,$add)(3)]);
[10,8]
Exemplos
<?php
function then(callable $first,callable $next):Closure{return fn(int $x)=>$next($first($x));}
$add=fn(int $v)=>$v+2;$double=fn(int $v)=>2*$v;echo json_encode([then($add,$double)(3),then($double,$add)(3)]);
Resultado
[10,8]