This repository was archived by the owner on May 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Curry all combinators #4
Open
shadowhand
wants to merge
11
commits into
php-fp:master
Choose a base branch
from
shadowhand:feature/curried
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
e21a95d
Add curry() function
shadowhand 7a0fcb3
Switch to internal curry() function
shadowhand 9d94745
Add a changelog
shadowhand 01b6e47
Add k() function
shadowhand e0a5ff1
Add compose() function
shadowhand aae02b2
Add flip() function
shadowhand b25527a
Add id() function
shadowhand a8c7c22
Add if_else() function
shadowhand b0d8019
Add on() function
shadowhand b70cbfe
Remove combinators class
shadowhand 140dcf9
Make combinators curried by default
shadowhand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,23 @@ | ||
# Change Log | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/) | ||
and this project adheres to [Semantic Versioning](http://semver.org/). | ||
|
||
## [2.0.0] | ||
|
||
### Added | ||
|
||
- Add curried combinators as functions | ||
|
||
### Removed | ||
|
||
- `Combinators` class has been removed in favor of function constructors | ||
|
||
### Changed | ||
|
||
- Switched to a custom implementation of `curry()` | ||
|
||
## [1.0.0] | ||
|
||
- Initial release |
This file contains hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,77 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace PhpFp\Combinators; | ||
|
||
use ReflectionFunction; | ||
use ReflectionMethod; | ||
use Reflector; | ||
|
||
final class Curry | ||
{ | ||
/** | ||
* @var callable | ||
*/ | ||
private $fn; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $arity; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $args; | ||
|
||
/** | ||
* Wrap a callable in a curry. | ||
*/ | ||
public function __construct(callable $fn, $arity = null) | ||
{ | ||
$this->fn = $fn; | ||
$this->arity = $arity ?? $this->numberOfParameters($fn); | ||
$this->args = []; | ||
} | ||
|
||
/** | ||
* Apply the curry with some parameters. | ||
*/ | ||
public function __invoke(...$args) | ||
{ | ||
$args = array_merge($this->args, $args); | ||
|
||
if (count($args) >= $this->arity) { | ||
// All parameters have been defined, execute the function | ||
return call_user_func_array($this->fn, $args); | ||
} | ||
|
||
// Additional parameters are needed, return another curry | ||
$copy = clone $this; | ||
$copy->args = $args; | ||
|
||
return $copy; | ||
} | ||
|
||
private function reflect(callable $fn): Reflector | ||
{ | ||
if (is_array($fn)) { | ||
return new ReflectionMethod($fn[0], $fn[1]); | ||
} | ||
|
||
if (is_string($fn) && strpos($fn, '::')) { | ||
return new ReflectionMethod($fn); | ||
} | ||
|
||
if (is_object($fn)) { | ||
return new ReflectionMethod($fn, '__invoke'); | ||
} | ||
|
||
return new ReflectionFunction($fn); | ||
} | ||
|
||
private function numberOfParameters(callable $fn): int | ||
{ | ||
return $this->reflect($fn)->getNumberOfRequiredParameters(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should both be exposed? The "uncurried" ones aren't all totally uncurried (thinking of
compose
, in particular), so it might be more helpful to hide the "original" functions and move them out of sight and mind.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They have to be imported somehow, or PHP won't find them. However, both are contained in namespaces so they won't pollute the global namespace.