We have seen the implementation of stacks in Chapter 4, Constructing Stacks and Queues. For simplicity, we won't discuss the whole stack operation again. We will jump right into the implementation of push, pop, and top operations using functional programming. Tarsana has lots of built-in functions for list operations. We will use their built-in functions to implement our functional operations of the stack. Here is the implementation:
require __DIR__ . '/vendor/autoload.php';
use Tarsana\Functional as F;
$stack = [];
$push = F\append(F\__(), F\__());
$top = F\last(F\__());
$pop = F\init(F\__());
$stack = $push(1, $stack);
$stack = $push(2, $stack);
$stack = $push(3, $stack);
echo "Stack is ".F\toString($stack)."\n";
$item = $top($stack);
$stack = $pop($stack);
echo "Pop-ed item: ".$item."\n"...