Download the PHP package linepogl/impartial-pipes without Composer
On this page you can find all versions of the php package linepogl/impartial-pipes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download linepogl/impartial-pipes
More information about linepogl/impartial-pipes
Files in linepogl/impartial-pipes
Package impartial-pipes
Short Description A PHP library with partial functions suitable for the pipe operator
License MIT
Homepage https://github.com/linepogl/impartial-pipes
Informations about the package impartial-pipes
Impartial Pipes
A PHP library providing partial functions suitable for the pipe operator.
Features
- All the produced partial functions have exactly one argument, as the pipe operator expects.
- Type checking with phpstan and generics.
- Immutability and lazy evaluation.
- Iterating with rewindable generators without creating copies of the data. Fallback to array functions when no copies are involved.
- 100% test coverage.
Installation
Why use it?
The pipe operator of PHP is a great way to chain functions together. However, it is not perfect because it requires that the chained functions accept exactly one mandatory argument. When this is not the case, we have to resort to workarounds by wrapping the functions in a closure.
This might be solved in a future version of PHP, with a proposed syntax like this:
This is a step in the right direction. However, it is still not perfect:
- We still have to remember the infamously inconsistent order of the arguments.
- It works only for arrays, but not for iterables, because the standard library offers too few functions for that.
With Impartial Pipes, we can write the same code as this:
Can I use it before PHP 8.5?
Yes, you can!
Even if the pipe operator is not available, partial functions can still be used. As a result, we get a syntax that is slightly more consistent than that of the standard library. On top of that, we can use these functions with both arrays and iterables.
Alternatively, you can use the pipe function as syntax sugar. In this case, the order of the operations is the same as in the pipe operator.
Full Reference
Combining partial functions
- p_concat_preserving_keys
Mapping partial functions
- p_map_preserving_keys
- p_flat_map_preserving_keys
- p_map_keys
- p_values
- p_keys
- p_group_by_preserving_keys
- p_order_by_preserving_keys
- p_then_by
Filtering partial functions
- p_filter_preserving_keys
- p_reject_preserving_keys
- p_compact_preserving_keys
- p_unique_preserving_keys
- p_unique_keys_preserving_keys
- p_take_preserving_keys
- p_while_preserving_keys
- p_while_not_null_preserving_keys
- p_skip_preserving_keys
- p_skip_while_preserving_keys
Reducing partial functions
- p_any
- p_all
- p_count
- p_first
- p_first_or
- p_first_key
- p_first_key_or
- p_last
- p_last_or
- p_last_key
- p_last_key_or
- p_sum
- p_implode
- p_to_array
Tapping partial functions
- p_tap
- p_foreach
- p_narrow
- p_not_null
- p_unfold