Download the PHP package crell/fp without Composer

On this page you can find all versions of the php package crell/fp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package fp

Functional utilities

Latest Version on Packagist Total Downloads

This library contains functional utilities intended for use with PHP 8.1 and later. Its primary tool is the pipe() function, which takes a starting argument and then a series of callables to "pipe" that argument through. Most other functions are utilities that produce a closure that takes the return from a previous pipe() step as its only argument.

That provides a reasonably good experience for building multi-step functional pipelines and composition, at least until PHP itself gets a proper pipe operator. :-) It also provides a convenient point-free style experience.

Install

Via Composer

Usage

Pipes and composition

The most important function in this library is pipe(). It takes an arbitrary number of arguments. The first is whatever starting value you want to send through a sequence of functions. The rest are any unary callable (single-argument callable) that returns a value. pipe() will pass the first value to the first callable, then pass the result of that to the second callable, then pass the result of that to the third callable, and so on until the pipe ends. The final result will then be returned.

For a trivial example:

There is also a similar method compose(), which takes only an arbitrary number of callables and produces a function that will take one argument and pass it through all of them the same way. The difference is that compose() returns the resulting callable, while pipe() executes immediately. Technically it is trivial to implement either one in terms of the other, but for performance reasons they are separate.

Pipeable functions

As stated, pipe() works only with unary functions. PHP has numerous functions that are not unary, however, including many of the most useful array and string functions. For that reason, this library provides alternate, pipe-friendly versions of most common operations. All of them will take some number of arguments and return a Closure that has those arguments partially applied; that is, the provided arguments get "saved" and used when the returned function is invoked. Normally that would be within a pipe() chain, but they may be directly invoked as well if desired.

For example, the explode() function (which is namespaced to not conflict with the global function), takes a single argument, the delimiter. Its return value is a callable that will, when called with a string, call the built-in \explode() function with the provided string and the saved delimiter.

The upshot of this approach is that nearly all needle/haystack questions go away, as either the value to operate on is subsumed into the pipe itself or very clearly provided as a secondary argument list.

Most functions will simply wrap and fall back to standard-lib functions where possible.

String functions

All functions below are in the Crell\fp namespace.

explode(string $delimiter) - Explode a piped string using $delimiter.

implode(string $glue) - Implode a piped array using $glue.

replace(array|string $find, array|string $replace) - Does a find/replace in a piped string, using str_replace()

Object functions

All functions below are in the Crell\fp namespace.

prop(string $prop) - Returns the $prop public property of a piped object.

method(string $method, ...$args) - Invokes $method on a piped object using $args as arguments. (Both positional and named arguments are supported.)

typeIs(string $type) - Returns true if a piped value is of the specified type, false otherwise. Legal types are int, string, float, bool, array, resource, or a class/interface name. This will usually be the last function in a pipe.

Array functions

All functions below are in the Crell\fp namespace.

In many cases below, there are multiple versions of a function. There are two axes on which they vary: Whether they return an array or an iterable, and whether they operate on array keys as well.

PHP's built-in array functions do not accept iterables; Nearly all the functions below do. Those that begin with a will return an array, even if what was passed in is an iterator. Those that begin with it will return a Generator, which will produce values lazily.

Unless otherwise specified, functions will operate only on array values. Array keys will be explicitly ignored and not passed to the provided callback, but preserved. If a function has the suffix withKeys, then the key will be made available to the provided callback.

These must be separated due to the combination of three ill-interacting PHP features.

  1. All arrays are associative, but some are short-circuited to have list-like keys, rather than having lists and maps be two separate constructs.
  2. PHP supports optional arguments, which means some functions will misbehave if passed an array key as an optional second argument.
  3. PHP user-space functions will silently ignore excess arguments but bundled functions will fail if called with excess arguments.

The upshot of these design choices is that it is not possible to reliably build a function that applies a callable to an array without knowing if the keys are important. That distinction must be made by the developer. The non-key versions require a callback with a single argument only (the array value), while the withKeys version will pass the value and key as two separate arguments to the callback.

Deciding whether to use the greedy (array) or lazy (iterable) versions of functions depends on the tradeoffs appropriate to your use case. As a general rule, the greedy version will be faster but may use more memory, while the lazy version will use less memory but may be slower. How much the difference is will vary widely with the specific use case.

Mapping

Applies a provided callable to each entry in an iterable, producing a new iterable with the same keys as the source, but with the values replaced with the corresponding callback result.

Filtering

Produces a new array containing only those array entries where the callable returns true. Array keys are preserved. If no callback is provided, a default of "is truthy" is used, just like PHP's native array_filter().

Collecting

The collect() function will accept a piped iterable or array, and produce an array. It's really just a wrapper around iterator_to_array() that guards against passing it an array, which is not supported in PHP 8.1. In PHP 8.2 and later, this function is equivalent to just using iterator_to_array(...) directly in a pipe, as it now accepts arrays as well.

Reducing

Reducing, also known as fold or foldl in some languages, involves iteratively applying an operation across an array to produce a single final result. See array_reduce() for more details.

First, conditionally

Several functions provide a way to obtain the first value in a sequence that meets some criteria. In all cases, they return null if nothing does.

Miscellaneous functions

Utility functions

The following functions are not designed to be used with pipe(), but are more "traditional" functions. That said, they may be referenced as a first-class-closure.

Utility traits

Crell/fp also provides two traits to aid usage of objects in a functional environment.

Evolvable

The Evolvable trait provides a single method, with(), which accepts a variadic list of named arguments. It will produce a new copy of the same object, but with the provided values assigned to properties of the same name. Note that because it runs in private scope, visibility is ignored. This may be undesireable in some cases.

This trait is most useful with readonly classes:

Newable

Constructor calls in PHP are screwy, and cannot be easily chained or piped. The Newable trait provides a simple standard static method that wraps the constructor, making it possible to chain and pipe. Its arguments are variadic and passed to the constructor verbatim.

Examples

It may not be entirely obvious how all of the above fit together. To help make it clear, here's some examples of pipes and their functions in action.

The following example will take an input file name $inputFile, load its content into memory, trim whitespace, split it by lines into an array, call a custom pairUp() function on that array, filter the resulting array, and then count the remaining values. All of that in one simple statment.

This example uses a custom function to read lines from a file lazily. That produces a lazy generator, which is then passed to itmap() which will apply parse() to each item in the generator, but is itself a generator so is also lazy. parse() produces a Step object from each line of input. reduce() therefore receives an iterable of Step objects, and applies move() to each one in turn, from a starting point. Each time, move() returns a new Position.

In other words, these few lines of code constitute a complete script parsing engine, albeit a simple one. Because each step is lazy, only one Step is loaded into memory at once.

The alternate version below uses the greedy amap() instead. That will produce an already-computed array of Step objects rather than a generator that produces Step objects.

For more detailed examples, see the following articles that solved 10 days of Advent of Code 2021 using Crell/fp.

Change log

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CODE_OF_CONDUCT for details.

Security

If you discover any security related issues, please use the GitHub security reporting form rather than the issue queue.

Credits

License

The Lesser GPL version 3 or later. Please see License File for more information.


All versions of fp with dependencies

PHP Build Version
Package Version
Requires php Version ~8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package crell/fp contains the following files

Loading the files please wait ....