-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c1110c0
commit 3b55c02
Showing
6 changed files
with
365 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Dash; | ||
|
||
/** | ||
* Returns a new function that performs right-to-left function composition. | ||
* | ||
* The last function can have any arity, but the rest must be unary. | ||
* | ||
* @see pipe() | ||
* | ||
* @param callable ...$fns | ||
* @return callable New function that composes `$fns` right-to-left | ||
* | ||
* @example | ||
$addOne = function ($v) { | ||
return $v + 1; | ||
}; | ||
$triple = function ($v) { | ||
return $v * 3; | ||
}; | ||
$square = function ($v) { | ||
return $v * $v; | ||
}; | ||
$composed = Dash\compose($square, $triple, $addOne); | ||
$composed(1); // === 36 | ||
* | ||
* @example Last function can accept multiple arguments | ||
$pow = function ($base, $exp) { | ||
return pow($base, $exp); | ||
}; | ||
$addOne = function ($v) { | ||
return $v + 1; | ||
}; | ||
$triple = function ($v) { | ||
return $v * 3; | ||
}; | ||
$composed = Dash\compose($triple, $addOne, $pow); | ||
$composed(2, 3); // === 27 | ||
*/ | ||
function compose(callable ...$fns) | ||
{ | ||
return function (...$args) use ($fns) { | ||
$fns = array_reverse($fns); | ||
$first = first($fns); | ||
$rest = array_slice($fns, 1); | ||
|
||
$result = call_user_func($first, ...$args); | ||
foreach ($rest as $fn) { | ||
$result = call_user_func($fn, $result); | ||
} | ||
|
||
return $result; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Dash; | ||
|
||
/** | ||
* Returns a new function that performs left-to-right function composition. | ||
* | ||
* The first function can have any arity, but the rest must be unary. | ||
* | ||
* @see compose() | ||
* | ||
* @param callable ...$fns | ||
* @return callable New function that composes `$fns` left-to-right | ||
* | ||
* @example | ||
$addOne = function ($v) { | ||
return $v + 1; | ||
}; | ||
$triple = function ($v) { | ||
return $v * 3; | ||
}; | ||
$square = function ($v) { | ||
return $v * $v; | ||
}; | ||
$piped = Dash\pipe($addOne, $triple, $square); | ||
$piped(1); // === 36 | ||
* | ||
* @example First function can accept multiple arguments | ||
$pow = function ($base, $exp) { | ||
return pow($base, $exp); | ||
}; | ||
$addOne = function ($v) { | ||
return $v + 1; | ||
}; | ||
$triple = function ($v) { | ||
return $v * 3; | ||
}; | ||
$piped = Dash\pipe($pow, $addOne, $triple); | ||
$piped(2, 3); // === 27 | ||
*/ | ||
function pipe(callable ...$fns) | ||
{ | ||
return function (...$args) use ($fns) { | ||
$first = first($fns); | ||
$rest = array_slice($fns, 1); | ||
|
||
$result = call_user_func($first, ...$args); | ||
foreach ($rest as $fn) { | ||
$result = call_user_func($fn, $result); | ||
} | ||
|
||
return $result; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/** | ||
* @covers Dash\compose | ||
*/ | ||
class composeTest extends PHPUnit_Framework_TestCase | ||
{ | ||
public function testCanComposeOneUnaryFunction() | ||
{ | ||
$addOne = function ($v) { | ||
return $v + 1; | ||
}; | ||
$composed = Dash\compose($addOne); | ||
|
||
$this->assertSame(2, $composed(1)); | ||
} | ||
|
||
public function testCanComposeOneNonUnaryFunction() | ||
{ | ||
$pow = function ($base, $exp) { | ||
return pow($base, $exp); | ||
}; | ||
$composed = Dash\compose($pow); | ||
|
||
$this->assertSame(8, $composed(2, 3)); | ||
} | ||
|
||
public function testCanComposeTwoFunctions() | ||
{ | ||
$addOne = function ($v) { | ||
return $v + 1; | ||
}; | ||
$triple = function ($v) { | ||
return $v * 3; | ||
}; | ||
$composed = Dash\compose($triple, $addOne); | ||
|
||
$this->assertSame(6, $composed(1)); | ||
} | ||
|
||
public function testCanComposeMoreThanTwoFunctions() | ||
{ | ||
$addOne = function ($v) { | ||
return $v + 1; | ||
}; | ||
$triple = function ($v) { | ||
return $v * 3; | ||
}; | ||
$square = function ($v) { | ||
return $v * $v; | ||
}; | ||
$composed = Dash\compose($square, $triple, $addOne); | ||
|
||
$this->assertSame(36, $composed(1)); | ||
} | ||
|
||
public function testFirstFunctionCanBeNonUnary() | ||
{ | ||
$pow = function ($base, $exp) { | ||
return pow($base, $exp); | ||
}; | ||
$addOne = function ($v) { | ||
return $v + 1; | ||
}; | ||
$triple = function ($v) { | ||
return $v * 3; | ||
}; | ||
$composed = Dash\compose($triple, $addOne, $pow); | ||
|
||
$this->assertSame(27, $composed(2, 3)); | ||
} | ||
} |
Oops, something went wrong.