Edit report at https://bugs.php.net/bug.php?id=66368&edit=1
ID: 66368
User updated by: chriswarbo at gmail dot com
Reported by: chriswarbo at gmail dot com
Summary: Operators should also be functions
Status: Open
Type: Feature/Change Request
Package: *General Issues
Operating System: Irrelevant
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
It is sufficient to do:
$o = function($l, $r) {
return $l + $r;
};
But after defining such a function for the umpteenth time, I decided to raise this issue. I
don't think I'm alone in using such definitions, since it's a common-enough pattern
that we had array_sum built in to the language even before we had lambdas and array_reduce
(array_sum is just array_reduce curried with $o and 0).
One solution would be to build in functions like $o to prevent the need to define them over and over
in-line or in libraries, but I'd rather not be 'that guy' who wants his own
helper-functions built-in, since adding new functions bloats the language and, more importantly,
burdens developers with extra complexity ("Should I use + or $o?").
Instead, I think it's an opportunity to fix the treatment of operators, especially since
functions like $o are just eta-expansions of operators, and eta-expansion is always a useless
transformation:
function eta($f) {
return function ($x) {
return $f($x);
};
}
call_user_func(eta($x), $y) === call_user_func($x, $y)
Previous Comments:
------------------------------------------------------------------------
[2014-01-01 14:43:23] krakjoe@php.net
Ok, I understand a bit clearer what you're asking for ... but don't really see why; I
cannot imagine a time where:
$o = '+';
$o(10, 20);
Is required such that:
$o = function($l, $r) {
return $l + $r;
};
Does not suffice.
Maybe someone else will get it, I don't ...
------------------------------------------------------------------------
[2014-01-01 13:04:19] chriswarbo at gmail dot com
As for "[]" and "->" being functions (or function-like), consider the
following:
function subscript($array, $index) {
return $array[$index];
}
array_map('subscript', $my_arrays, range(0, 9));
function prop($object, $property) {
return $object->$property;
}
array_map('prop', $my_objects, $props);
Personally I don't use the above functions, but I make heavy use of a function
"lookup" which does both (based on the argument's type) plus a few bells and whistles
(recursively looking up an array of identifiers, exception and error handling for magic methods,
etc.).
------------------------------------------------------------------------
[2014-01-01 12:38:09] chriswarbo at gmail dot com
I don't think it's necessary to replace operators with functions; it's more about
allowing operators to be used in the same way as functions, regardless of how they're
implemented.
The crucial difference at the moment can be seen by PHP's distinction between calling named
functions directly and indirectly (via a string identifier):
// Direct
my_function(10, 20);
// Indirect
$f = 'my_function';
$f(10, 20);
This is a bit clunky (compared to "$f = my_function;" in most other languages), but it
gets the job done. Let's compare it to operators:
// Direct
10 + 20;
// Indirect
$o = '+';
$o(10, 20); // Fatal error: Call to undefined function +()
This is the essence of all the above examples: we can't pass around identifiers for operators
like we can for named functions. I have no objections to the distinction between operators and
functions, except for this advantage that functions currently have over operators. Solving this from
inside PHP requires wrapping every operator with a function, but solving it at the implementation
level gives us more options to play with.
In particular, we can use PHP's 'clunky' method of indirect calls to our advantage.
Since functions are identified by strings, and those strings can contain arbitrary runtime data, the
PHP interpreter is forced to use a lookup procedure to find the right function. This gives us an
opportunity to inject some extra logic, for example replacing the current fatal error trigger with a
switch statement for each operator, defaulting to the fatal error.
------------------------------------------------------------------------
[2014-01-01 09:10:06] krakjoe@php.net
I've read this, several times, last night and this morning. I cannot make good sense out of
your request, I don't know what you're asking for. Some of this just doesn't make
sense (why on earth should [] or -> be a function).
I'm sure it all makes sense to you in your head, but I'm having a hard time understanding
what it is you want to be implemented. By title alone; you do not want all operators and some
language constructs to be functions, that would destroy performance, obviously. You don't
appear to want operator overloading because this appears focused on primitive types.
What is it exactly that you want ?
Sorry if the explanation is there, I just don't see it ...
------------------------------------------------------------------------
[2013-12-31 10:41:33] chriswarbo at gmail dot com
I don't have many examples to hand, since I don't have access to my previous
employers' codebases and I've not been at my current job for very long. Here's what a
bit of grepping on my current employer's codebase turned up.
Here's some array comparison code. "$not_f" is just the composition of "!"
and "$f", but we can't compose them directly since "!" is not a function:
$sort = function($arr) { sort($arr); return $arr; };
foreach (array('is_null', 'is_bool', 'is_int',
'is_string') as $f) {
if (!array_equal($sort(array_filter($array1, $f)),
$sort(array_filter($array2, $f)))) return FALSE;
$not_f = function($x) use ($f) { return !($f($x)); };
$array1 = array_filter($array1, $not_f);
$array2 = array_filter($array2, $not_f);
}
Note that our codebase already contains the following composition function, but it's utility is
massively hampered by so much of PHP's functionality not being in functions:
function compose($f, $g) {
return function($x) use ($f, $g) {
return $f($g($x));
};
}
A test case for the compose function contains a few (admittedly artificial) workarounds for
PHP's distinction between operators and functions (closing over $a and $b isn't necessary,
it just saves us a line or two since PHP doesn't support currying):
protected function testComposeComposes() {
$a = mt_rand(-1000, 1000);
$b = mt_rand(-1000, 1000);
$x = mt_rand(-1000, 1000);
$f = function($x) use (&$a) { return $x * $a; };
$g = function($x) use (&$b) { return $x + $b; };
$fg = compose($f, $g);
$composed = $fg($x);
$manual = ($x + $b) * $a;
if (!$this->assertIdentical(
$composed,
$manual,
"Composed addition and multiplication $a $b $x $composed $manual")) {
$this->dump(array(
'$a' => $a,
'$b' => $b,
'$x' => $x,
'$composed' => $composed,
'$manual' => $manual
));
}
}
Here's a test method, which again just composes "!" with another function
(Drupal's "module_exists" function) but has to do so manually:
protected function ourDependenciesAreEnabledTest() {
$this->assertTrue(
count(array_filter($this->ourDependencies(),
function ($m) { return !module_exists($m); })) === 0,
'All of our dependencies are enabled');
}
Here's some code for loading all includes from a directory, with a lambda to work around the
fact that "require_once" isn't a function:
array_map(function($i) { require_once($i); },
find_includes(TRUE));
Here's a version of "array_reduce" which passes the keys to the folding function. The
first anonymous function just works around the fact that "array" isn't a function
(the second is just an uncurry function):
function array_reduce_keys($arr, $f, $i) {
return array_reduce(
// Pair up keys and values
array_map_keys(
function ($k, $v) { return array($k, $v); },
$arr),
// Pull apart each pair and send to $f
function ($result, $pair) use (&$f) {
return $f($result, $pair[0], $pair[1]);
},
// Start with $i
$i);
}
Here's a test case for the above function, which uses a lambda to work around the fact that
"+" isn't a function:
protected function testArrayReduceKeysIsGivenKeys() {
// Our keys are numeric, so try summing them
$arr = $this->randomArray();
$this->assertIdentical(
array_sum(array_keys($arr)),
array_reduce_keys($arr, function($x, $y, $_) { return $x + $y; }, 0),
'array_reduce_keys is given access to keys');
}
Here's a function from our payment system which tries to look up a user's billing
information, which is a workaround for "->" not being a function:
$default = function($n) use ($billing_info) {
return $billing_info->$n;
};
Of course, this is code which is in production *despite* PHP's asymmetric treatment of
operators and functions. Most of the time I'll refactor the algorithm to work around these
deficiencies (manually unrolling maps/folds/filters, hard-coding operators, copy/pasting functions
just to change an operator, etc.). Such things are more difficult to grep for than lambdas
("function[ ]*(").
Most of the use-cases I found weren't quite applicable, since one of the operator arguments
values was constant, for example:
function($node) { return 'nodes/' . $node; }
function($p, $q) {
db_query('UPDATE {quiz_question} SET previous = :p WHERE question_id = :q',
array(':p' => $p,
':q' => $q));
}
function($x) { return $x? 'right' : 'wrong'; }
function ($x) { return abs($x - 1) < 0.0001; }
$band_codes = array_map(function($b) { return $b['band']; }, $bands);
entity_get_controller('commerce_order')->resetCache(
array_map(function($o) { return $o->order_id; },
$orders));
function($key) use ($licensee) { return $licensee[$key]; }
function($p) { return product_display_nodes($p->product_id); }
With currying and/or partial-application of functions (like the "curry" function at http://chriswarbo.net/data_custom/prelude.txt
), these would become valid use-cases for this ticket, since they would need ".",
"array" and "=>", "?!", "<", "[]", and
"->" to be functions, respectively. Currying deserves a separate ticket though, and the
implementation I linked to isn't intended for production use (PHP doesn't have tail-call
optimisation, so I'd need to reign in its stack usage).
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=66368
--
Edit this bug report at https://bugs.php.net/bug.php?id=66368&edit=1