-
Notifications
You must be signed in to change notification settings - Fork 0
Add bind() function #6
base: master
Are you sure you want to change the base?
Conversation
Having an immutable `Curry` class allows creating a curry from any callable. Having this as a base class will allow additional functionality such as `Partial` to be added with minimal effort.
Having all combinators curried by default provides much greater flexibility for point-free programming.
Would it make more sense to rename <?php
class Identity {
private $_val;
public function __construct($val) {
$this->val = $val;
}
public function map($f) {
return new Identity($f($this->val));
}
public function chain($f) {
return $f($this->val);
}
}
function bind($method_name, $obj) {
return function () use ($method_name) {
$params = func_get_args();
return call_user_func_array(array($obj, $method_name), $params);
}
}
$i1 = new Identity(1);
$add1 = function ($x) { return $x + 1; };
$times3 = function ($x) { return new Identity($x * 3); };
$mapOnI1 = bind('map', $i1);
$chainOnI1 = bind('chain', $i1);
$i2 = $mapOn1($add1); // Identity(2)
$i3 = $chainOn1($times3); // Identity(3) |
I'm not sure it makes sense - if you're going to write curried PHP, wouldn't you write them as nested functions anyway? (Genuine question - I haven't touched PHP in about 2 years now) |
My point was more about how the function is named. I think it should be called Whether or not you need a |
Actual changes are in 5d69102
Fixes #5
Refs #4