dev-master
9999999-dev
MIT
The Development Requires
Provides implementations and contracts for dealing with data structures., (*1)
The data type of the first element added to any collection implementation then becomes the only supported type for that collection instance., (*2)
Example using scalar types:, (*3)
$stack = new Meek\Collection\UnboundedStack(); $stack->push('1'); // All future push() operations will only accept the string data type. $stack->push(2); // Throws an InvalidArgumentException -> must be a string
Example using objects:, (*4)
class MyClass extends stdClass {} $stack = new Meek\Collection\UnboundedStack(); $stack->push(new stdClass()); // All future push() operations will only accept instances of `stdClass`. $stack->push(new MyClass()); // Works fine as `MyClass` is sub-classed from `stdClass` $stack->push(new class {}); // Throws an InvalidArgumentException -> must be an instance of "stdClass"
Reversing a string using an UnboundedStack
:, (*5)
$wordToReverse = 'dog'; $stack = new Meek\Collection\UnboundedStack(...str_split($wordToReverse)); $reversedWord = ''; while ($stack->size() > 0) { $reversedWord .= $stack->pop(); } print $reversedWord; // prints 'god'
Keeping track of history using a BoundedStack
:, (*6)
$numberOfActionsToKeepTrackOf = 3; $stack = new Meek\Collection\BoundedStack($numberOfActionsToKeepTrackOf); $perform = function ($action) use ($stack) { $stack->push($action); echo sprintf('perform: %s', $action); }; $undo = function () use ($stack) { $action = $stack->pop(); echo sprintf('undo: %s', $action); }; $perform('select all'); // prints 'perform: select all' $perform('copy'); // prints 'perform: copy' $perform('paste'); // prints 'perform: paste' $undo(); // prints 'undo: paste' $perform('paste'); // prints 'perform: paste' try { $perform('select all'); } catch (OverflowException $e) { // clear history and keep going or notify user that history is full }
MIT