PHPackages                             apantle/fun-php - 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. apantle/fun-php

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

apantle/fun-php
===============

Functional Programming to enhance OOP PHP developer experience

0.3(6y ago)1721[2 PRs](https://github.com/apantle/fun-php/pulls)2MITPHPCI failing

Since Aug 23Pushed 3mo ago1 watchersCompare

[ Source](https://github.com/apantle/fun-php)[ Packagist](https://packagist.org/packages/apantle/fun-php)[ RSS](/packages/apantle-fun-php/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (2)Versions (8)Used By (2)

fun-php
=======

[](#fun-php)

Functional Programming to enhance OOP PHP developer experience

[![Build Status](https://camo.githubusercontent.com/a55533e0563cf06406e72c98ececf37cdca9024b8bc8e4277d6b31f808ea3fec/68747470733a2f2f7472617669732d63692e6f72672f6170616e746c652f66756e2d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/apantle/fun-php) [![Maintainability](https://camo.githubusercontent.com/ad0554dd765eb919bae9ec9ec201bbc14b8a513f33d6bbdb1d075a2b856ca560/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35333637643039326561373361653637343734332f6d61696e7461696e6162696c697479)](https://codeclimate.com/github/apantle/fun-php/maintainability) [![Test Coverage](https://camo.githubusercontent.com/f8182215f0fc8306e98636f58f423945b6f81353cee1cfd4807ee068af22f045/68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f35333637643039326561373361653637343734332f746573745f636f766572616765)](https://codeclimate.com/github/apantle/fun-php/test_coverage)

Here you have the supported functions with a minimal example, there are more examples in the tests of the package:

### compose

[](#compose)

applies a par of functions, first the one to the right, then to the returned value applies the function to the left:

Example:

```
$stringSquaredRoot = compose('strval', 'sqrt');

var_dump('4' === $stringSquaredRoot(16)); // bool(true)
```

### pipe

[](#pipe)

Returns a function that receives any number of callables, first one is passed an arbitrary number of arguments, then it passes the returned value to next one in the pipe, and so on. Next functions must be unary (only accept one argument).

Example with a simplified functional controller:

```
$getFoldersController = pipe(
    'validateHasUser',
    'validatePermissionOfUser',
    'getRootFolders'
);

function validateHasUser($request = null)
{
    if (!array_key_exists($request, 'user')) {
        throw new \LogicException('User parameter not received');
    }
    return $request;
}

function validatePermissionOfUser($request)
{
    if (intval($request['user']) !== 1) {
        throw new \DomainException('Only root user has permissions on this route');
    }
    return $request;
}

function getRootFolders($request) {
    return [ 'folder-' . $request['user'] ];
}

// $user key absent of request:
$getFoldersController(['path' => 'whatever']); // throws LogicException

// $user key present, non root user
$getFoldersController(['user' => 20]); // throws DomainException

// $user is root
$getFoldersController(['user' => 1]); // returns [ 'folder-1' ];
```

### curryToUnary

[](#currytounary)

Allow to partially apply any callable, returning an unary function. Most useful in combination with `pipe` to write point-free style algorithms.

Passing in the list of arguments to bound, the constant `Apantle\FunPHP\_` the position of the argument expected can be swapped. For example, you can build a Mappable collection with array\_map and the array to be mapped, and pass to this collection a different mapper every time.

Examples:

```
$simpleArrayReverse = curryToUnary('array_reverse');

var_dump([4,3,2,1] === $simpleArrayReverse([1,2,3,4]); // bool(true)

// custom position of argument to receive:
$map = curryToUnary('array_map', \Apantle\FunPHP\_, [1, 2, 3, 4]);

$result = $map(function ($num) { return $num * 10; });

var_dump([10, 20, 30, 40] === $result); // bool(true)
```

### constant

[](#constant)

Returns a function that always return the value passed, useful for placeholders or composition with constant values but to avoid hardcoding values:

```
$request = [
    'store_id' => filter_input(
        INPUT_POST,
        'store',
        VALIDATE_FILTER_INT
    )
];

$scope = constant($request['store_id']);
$scope() // always return the value received in $_POST['store']
```

### identity

[](#identity)

useful for functor tests, always returns the passed value

### head

[](#head)

gets the first item of an array

```
var_dump(1 === head[1,2,3,4]); // bool(true)
```

### unfold

[](#unfold)

Returns a function that takes a single value, applies an array of transformations to it, and returns a an associative array with the same keys as the array of specs, with the value mapped by those functions.

Useful to take a single input and pass it to several services, then collecting the output in a single hashmap. A perfect dual to apantle/hashmapper.

Example:

```
$input = 4;

var_dump(json_encode(unfold([
    'sqrt' => 'sqrt',
    'factorial' => 'gmp_fact'
])($input)));
// string(25) "{"sqrt":2,"factorial":24}"
```

If the function is unary, it passes only the input value.

In order to composer more complex algorithms easily, the mappers can receive a second argument, that apantle/hashmapper passes to every mapper automatically, being that the whole associative array being mapped.

Apart from the spec of transformations, it takes an optional argument that allows you to pass values through the mappers, it is recommended this argument to be an object to be passed by reference.

See the test sources for [more examples](https://github.com/apantle/fun-php/blob/566bfe539bb193028c5bad4c6687a6f3a1b1e82c/tests/FunctionsTest.php#L50-L70).

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance54

Moderate activity, may be stable

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity53

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

5

Last Release

2445d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/2f01afc6e15ca1ba483cc1773c5f5d1b6c5f5fa98e0fe224ae8410048eb84041?d=identicon)[jefrancomix](/maintainers/jefrancomix)

---

Top Contributors

[![tzkmx](https://avatars.githubusercontent.com/u/11432557?v=4)](https://github.com/tzkmx "tzkmx (9 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/apantle-fun-php/health.svg)

```
[![Health](https://phpackages.com/badges/apantle-fun-php/health.svg)](https://phpackages.com/packages/apantle-fun-php)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
