PHPackages                             bartfeenstra/fu - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bartfeenstra/fu

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

bartfeenstra/fu
===============

0.3.5(8y ago)312.7k1[6 issues](https://github.com/bartfeenstra/fu-php/issues)[4 PRs](https://github.com/bartfeenstra/fu-php/pulls)MITPHPPHP ~7.1

Since Jun 23Pushed 8y ago1 watchersCompare

[ Source](https://github.com/bartfeenstra/fu-php)[ Packagist](https://packagist.org/packages/bartfeenstra/fu)[ RSS](/packages/bartfeenstra-fu/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (3)Versions (29)Used By (0)

Functional PHP
==============

[](#functional-php)

[![Build Status](https://camo.githubusercontent.com/361a4ec70dcdc756a839d3176736ed0f29793764f52028f6be71d932d990647e/68747470733a2f2f7472617669732d63692e6f72672f626172746665656e737472612f66752d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/bartfeenstra/fu-php) [![Coverage Status](https://camo.githubusercontent.com/c545c6bbef947ce25feeec65686f1b61abbca9b201fd58cf33d72a9f358df7c4/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f626172746665656e737472612f66752d7068702f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/bartfeenstra/fu-php?branch=master) [![License](https://camo.githubusercontent.com/4efb981041040cf80874d29c4c771285a2254457ea9a54fd345cef21475291b7/68747470733a2f2f706f7365722e707567782e6f72672f626172746665656e737472612f66752f6c6963656e7365)](https://packagist.org/packages/bartfeenstra/fu) [![Latest Stable Version](https://camo.githubusercontent.com/e8a43f2617f541cda2d34d5c472ea7e7621dd9af6f8bf424ac093f049e46254e/68747470733a2f2f706f7365722e707567782e6f72672f626172746665656e737472612f66752f762f737461626c65)](https://packagist.org/packages/bartfeenstra/fu) [![Latest Unstable Version](https://camo.githubusercontent.com/099d5ad24564316ec2016d40452d082fb464f316679a0fcce43a19bdf2ceda9e/68747470733a2f2f706f7365722e707567782e6f72672f626172746665656e737472612f66752f762f756e737461626c65)](https://packagist.org/packages/bartfeenstra/fu) [![Total Downloads](https://camo.githubusercontent.com/31f573bc293d2d756c556db8d387acbaeb9d92cc3d3e764b9ccf6e4b2c64ad3c/68747470733a2f2f706f7365722e707567782e6f72672f626172746665656e737472612f66752f646f776e6c6f616473)](https://packagist.org/packages/bartfeenstra/fu)

This library provides tools to write more functional PHP code. Its concise and consistent API makes you more productive in different ways:

- Universal tools for [processing iterables](#iterators) like arrays.
- Callback [generation](#predicates) and [modification](#partial-function-application) functions.
- [Optional value types](#the-option-type) to aid with stronger typing and erorr handling.
- Shorthand [exception handling](#exception-handling).

Table of contents
-----------------

[](#table-of-contents)

1. [Installation](#installation)
2. [About](#about)
3. [Usage](#usage)
    1. [Iterators](#iterators)
    2. [Operations](#operations)
    3. [Exception handling](#exception-handling)
    4. [Predicates](#predicates)
    5. [The `Option` type](#the-option-type)
    6. [The `Result` type](#the-result-type)
    7. [Partial function application](#partial-function-application)
    8. [Currying](#currying)
4. [Contributing](#contributing)
5. [Development](#development)

About
-----

[](#about)

This library was written to address several concerns:

- Provide a single, consistent API to the different [iterable](http://php.net/manual/en/language.types.iterable.php)types in PHP, and the different [operations](#operations) available to the individual types: one API, any iterable, always associative, access to keys.
- Provide iterable processing operations that do not yet exist in PHP.
- Make writing closures quick and easy. [Predicate](#predicates) factories can be used to generate common (filter) conditions.
- Allow developers to create functions that easily distinguish between different function outputs using [optional value types](#the-option-type). These can be used to solve problems like with `json_decode()`, which returns `NULL` in case of an error, or when it successfully decodes the JSON string `null`. It is impossible to distinguish between the different outcomes without additional code, such as option types.
- Use native PHP features where possible for improved interoperability and performance. Naming and parameter order follow the predominant conventions in PHP. This means all iterators implement `\Iterator`, and many PHP core functions are used internally.
- Add laziness where possible, so many [operations](#operations) are only applied to the iterator items you actually use.

Installation
------------

[](#installation)

Run `composer require bartfeenstra/fu` in your project's root directory.

Usage
-----

[](#usage)

To use any of the code, you must first import the namespaces at the top of your files:

```

```

### Iterators

[](#iterators)

Traversable/iterable data structures can be converted to universal iterators:

```

```

### Operations

[](#operations)

The following operations work with iterator values, and even keys in the case of user-supplied callbacks:

#### each

[](#each)

Executes code for every value.

```

```

#### filter

[](#filter)

Filters out values that do not match.

```

```

#### find

[](#find)

Tries to find a single matching value.

```

```

#### map

[](#map)

Converts all values individually.

```

```

#### mapKeys

[](#mapkeys)

Converts all keys individually.

```

```

#### reduce

[](#reduce)

Combines all values into a single one.

```

```

To terminate the reduction before all items have been processed, throw a `TerminateReduction` with the final carrier value.

#### fold

[](#fold)

Combines all values into a single one, with a default start value.

```

```

To terminate the fold before all items have been processed, throw a `TerminateFold` with the final carrier value.

#### take

[](#take)

Takes *n* values.

```

```

#### takeWhile

[](#takewhile)

Take as many consecutively matching values as possible from the beginning.

```

```

#### slice

[](#slice)

Slices the values into a smaller collection.

```

```

#### min

[](#min)

Gets the lowest value.

```

```

#### max

[](#max)

Gets the highest value.

```

```

#### sum

[](#sum)

Sums all values.

```

```

#### forever

[](#forever)

Infinitely repeats the set of values.

```

```

#### zip

[](#zip)

Combines the values of two or more iterables into [tuples](https://en.wikipedia.org/wiki/Tuple).

```

```

#### list

[](#list)

Converts all keys to integers, starting from 0.

```

```

#### listKeys

[](#listkeys)

Uses keys as values, and indexes them from 0.

```

```

#### flip

[](#flip)

Swaps keys and values, similarly to [`array_flip()`](http://php.net/manual/en/function.array-flip.php).

```

```

#### reverse

[](#reverse)

Reverses the order of the values.

```

```

#### first

[](#first)

Gets the first value.

```

```

#### last

[](#last)

Gets the last value.

```

```

#### empty

[](#empty)

Checks if there are no values.

```

```

#### sort

[](#sort)

Sorts items by their values.

```

```

#### sortKeys

[](#sortkeys)

Sorts items by their keys.

```

```

#### chain

[](#chain)

Chains other iterables to an existing iterator, and re-indexes the values.

```

```

#### flatten

[](#flatten)

Flattens the iterables contained by an iterator into a single new iterator.

```

```

#### unique

[](#unique)

Removes all duplicate values.

```

```

### Exception handling

[](#exception-handling)

Complex `try`/`catch` blocks can be replaced and converted to [`Result`](#the-result-type) easily:

```

```

### Predicates

[](#predicates)

Predicates can be used with [`filter()`](#filter) and [`find()`](#find). They can be any [callable](http://php.net/manual/en/language.types.callable.php) that takes a single parameter and returns a boolean, but we added some shortcuts for common conditions. These functions take configuration parameters, and return predicates.

```
