Skip to content

Instantly share code, notes, and snippets.

@slifin
Last active September 17, 2016 14:45
Show Gist options
  • Select an option

  • Save slifin/ebfb3bb084e6633d94fb915e65202791 to your computer and use it in GitHub Desktop.

Select an option

Save slifin/ebfb3bb084e6633d94fb915e65202791 to your computer and use it in GitHub Desktop.
<?php
/**
* Data type to signify a placeholder.
*/
class Placeholder {}
class_alias('Placeholder', '_');
/**
* Implements partial application with placeholder support.
*
* Similar to http://underscorejs.org/#partial.
*
* @param Function|callable $fn
* The function to apply to.
* @param array $start
* The starting arguments to apply.
*
* @return callable
* Partially applied function.
*/
function partial(callable $fn, ...$start) {
return function (...$args) use ($fn, $start) {
return @$fn(...array_map(function ($v) use (&$args) {
return $v instanceof _ ? array_shift($args) : $v;
}, $start));
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment