Skip to content

Instantly share code, notes, and snippets.

<?php
function tea_and_biscuits($tea, $biscuit) {
return $tea + ' ' + $biscuit;
}
$tea = 'tea';
$tea_and_biscuits = partial($tea, (new _));
$biscuits = ['chocolate', 'plain', 'hobnob'];
$tea_and_biscuits = array_map($tea_and_biscuits, $biscuits);
<?php
function cond($condition, callable $true, $false = NULL) {
if ($condition) {
return $true();
}
if ($false !== NULL) {
if (is_callable($false)) {
return $false();
}
throw new InvalidArgumentException('False not callable');
<?php
namespace _;
/**
* Data type to signify a placeholder.
*/
class Placeholder {}
class_alias('Placeholder', '_');
/**
* Creates a callback at the element of $iterator->current()
<?php $counter = (int) ($_GET['counter'] ?? 0); ?>
<form>
<button name="counter" value="<?= $counter + 1 ?>">Number <?= $counter ?></button>
</form>
<?php
function recursive_filter($data, callable $selector_function, $iterator = NULL) {
$iterator = $iterator ?? new RecursiveIteratorIterator(
new RecursiveArrayIterator($data),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $key => $value) {
if ($selector_function($value, $key, $iterator)) {
$depth = $iterator->getDepth();
<?php
namespace _;
/**
* Data type to signify a placeholder.
*/
class Placeholder {}
class_alias('Placeholder', '_');
/**
* Creates a callback at the element of $iterator->current()
@slifin
slifin / _.php
Last active November 10, 2016 22:37
<?php
/**
* @file
* Collection of functional tools.
*/
namespace _;
/**
* Creates a callback at the element of $iterator->current()
<?php
class struct {
function __invoke(callable $callback) {
return $callback(...array_values($this->props));
}
}
class box extends struct {
function __construct($a, $b) {
$this->props = get_defined_vars();
}
<?php
class Struct {
private $args;
function __construct(array $args) {
$this->args = $args;
}
function __invoke(callable $signature) {
return $signature(...array_values($this->args));
}
@slifin
slifin / events.php
Created January 7, 2017 13:48
Playing around with event dispatch
<?php
// Event storage.
function event_storage(callable $callback) {
static $storage = array();
return $storage = $callback($storage);
}
// Retrieve events.
function event_get(callable $filter) {