Related to: Capturing abstractions in Lisp
I came across Yay today, which allows us to use macros in PHP. Cool right?
Start with
composer require yay/yay:dev-master
Then you can use
vendor/bin/yay
to pre-process your code with macros.
Now we can implement lazy evaluation (kinda) in PHP!
We start with:
<?php
macro {
__delay( ···expression )
} >> {
function() { return ···expression; }
}
function force( $x ) {
return $x();
}
function not_a_delay( $x ) {
return function() use ( $x ) {
return is_callable( $x ) ? $x() : "Not a callable function";
};
}
echo "__delay start\n";
$x = __delay( printf( "The time function returns: %d\n", time() ) );
echo "__delay end\n";
echo "\n----------\n\n";
echo "force start\n";
echo 'Retval: ' . force( $x ) . "\n"; // print here!
echo "force end\n";
echo "\n----------\n\n";
echo "not_a_delay start\n";
$x = not_a_delay( printf( "The time function returns: %d\n", time() ) ); // print here!
echo "not_a_delay end\n";
echo "force start\n";
echo 'Retval: ' . force( $x ) . "\n";
echo "force end\n";
So if we look at the output:
boro@bor0:~/Desktop/test$ vendor/bin/yay test.php | php __delay start __delay end ---------- force start The time function returns: 1494937031 Retval: 38 force end ---------- not_a_delay start The time function returns: 1494937031 not_a_delay end force start Retval: Not a callable function force end
The two big differences are line 19 and 31 of the code. As we can see from the output, line 19 got “delayed”, while line 31 got evaluated eagerly.