PHP implementation of generic, higher-order functions.

1.0.4 2016-06-27 18:22 UTC

This package is not auto-updated.

Last update: 2024-11-09 19:32:30 UTC


README

Many algorithms are generic. That is, the way the algorithm functions is independent of what the algorithm does to the underlying data. These generic algorithms can be wrapped in a reusable function.

This package provides reusable, generic functions implemented using the callable object pattern. You can nest these generic functions to solve more complex problems. See below and the docs.

Latest Stable Version Build Status Coverage Status Monthly Downloads

Installation

Install with composer: php composer.phar require haldayne/fox ^1.0

Requires PHP version 5.5.0 or higher. No other PHP extensions are required.

Select examples

Retry a function until it either succeeds or reaches the maximum number of attempts. Delay each attempt by an exponentially increasing time:

$retry = new \Haldayne\Fox\Retry(
    function ($src, $dst) { return copy($src, $dst); }
);
$retry->setAttempts(3);
$retry('ssh2.sftp://user:pass@host/the/file', 'thefile')
    or die("Copy failed after {$retry->getAttempts()} attempts");

Package contents and documentation

Extensive documentation is available at Read the Docs:

Contributing

Your contribution is welcome! Open an issue or create a gist showing how you're using fox. If you want to add a new algorithm to fox, please keep these in mind:

  • Please fork the repository and create a PR.
  • Use PSR-2.
  • Update tests so that you have at least 80% coverage.
  • Choose something useful. If the algorithm takes many arguments, or exposes state after calculating a result, it's a good candidate.

Miscellaneous

These algorithms can be composed together. The repository name fox is an abbreviation for f(x) said "f of x", which harks back to these functions' compositional ability.

Both Go and Java have implementations of the Retry function with exponential backoff.