PHPackages                             marcosh/lamphpda - 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. marcosh/lamphpda

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

marcosh/lamphpda
================

A collection of functional programming data structures

v3.2.1(1y ago)12313.5k↓40.5%9[7 issues](https://github.com/marcosh/lamphpda/issues)[2 PRs](https://github.com/marcosh/lamphpda/pulls)4Hippocratic-2.1PHPPHP &gt;= 8.1CI passing

Since Dec 14Pushed 1y ago3 watchersCompare

[ Source](https://github.com/marcosh/lamphpda)[ Packagist](https://packagist.org/packages/marcosh/lamphpda)[ Docs](https://github.com/marcosh/lamphpda)[ RSS](/packages/marcosh-lamphpda/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (4)Versions (20)Used By (4)

lamPHPda
========

[](#lamphpda)

A collection of type-safe functional data structures

Aim
---

[](#aim)

The aim of this library is to provide a collection of functional data structures in the most type safe way currently possible within the PHP ecosystem, still providing a generic and consistent API.

Main ideas
----------

[](#main-ideas)

The two ideas which differentiate this from other functional libraries in PHP are:

- a safe usage of sum types, following
- the usage of higher kinded types to increase reusability and type safety (see and )

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

[](#installation)

```
composer require marcosh/lamphpda
```

Tools
-----

[](#tools)

We use [Psalm](https://psalm.dev/) as a type checker. It basically works as a compilation step, ensuring that all the types are aligned.

To benefit from this library, it is compulsory that your code runs through a Psalm check.

Development
-----------

[](#development)

This library includes a `flake.nix` file, enabling you to access a development environment equipped with PHP 8.1 and Composer. To utilize it, simply execute `nix develop` within the directory. For those using `direnv`(with `nix-direnv`), a preconfigured `.envrc` file is also provided, which will automatically load the environment upon entering the library directory.

Decision record
---------------

[](#decision-record)

The relevant decisions regarding the project are collected in the [`adr`](adr) folder, following the [Architectural decision record](https://adr.github.io/) format

Content
-------

[](#content)

The library provides several immutable data structures useful to write applications in a functional style.

Currently, the implemented data structures are:

- [Maybe](src/Maybe.php), which allows modelling data which could be missing;
- [Either](src/Either.php), which models the idea of alternative;
- [Identity](src/Identity.php), which is just a simple wrapper
- [LinkedList](src/LinkedList.php), which models the possibility of having multiple values;
- [Pair](src/Pair.php), which models having two thing at the same time;
- [Reader](src/Reader.php), which models values which depend on a context;
- [State](src/State.php), which models values which can interact with a global state;
- [IO](src/IO.php), which models lazy values.

You can find more details about the implementation and the idea behind each data structure in the [docs/data-structures folder](docs/data-structures).

How to interact with the data structures
----------------------------------------

[](#how-to-interact-with-the-data-structures)

The library is built to be extremely abstract and generic to allow extreme composability and reusability.

There are various ways which you can use to interact with the provided data structures.

### Typeclasses

[](#typeclasses)

You can think of typeclasses as of behaviours which could be attached to a data structure. Since a data structure could in principle have more than one way to implement a specific behaviour (e.g., there's more than one way to use two integers to compute a new integer), we can not use directly interfaces to be implemented by our data structures. Therefore, typeclass instances are implemented as separate independent objects implementing an interface which describes the typeclass itself.

For example, the `Semigroup` typeclass, which describes the behaviour of [putting together two things of the same type to obtain a thing of the same type](http://marcosh.github.io/post/2020/08/21/type-equality-in-object-oriented-programming.html), could be implemented as

```
/**
 * @template A
 */
interface Semigroup
{
    /**
     * @param A $a
     * @param A $b
     * @return A
     */
    public function append($a, $b);
}
```

Now we can implement a `Semigroup` instance for any type we want, even for native types. For example, we could implement a semigroup for addition between integers

```
/**
 * @implements Semigruop
 */
final class IntAddition implements Semigroup
{
    /**
     * @param int $a
     * @param int $b
     * @return int
     */
    public function append($a, $b): int
    {
        return $a + $b;
    }
}
```

Then we could use it to sum two integers

```
(new IntAddition())->append(1, 2); // returns 3
```

This specific instance is not that interesting, but the fact that you could write code which depends on a generic `Semigroup` definitely is!

The typeclasses we are currently exposing are:

- [Functor](src/Typeclass/Functor.php), which allows lifting functions of one argument to a given context;
- [Apply](src/Typeclass/Apply.php), which allows lifting functions of any arity to a given context;
- [Applicative](src/Typeclass/Applicative.php), which allows lifting values to a context;
- [Alternative](src/Typeclass/Alternative.php), which models the ability of combining values wrapped in a context;
- [Monad](src/Typeclass/Monad.php), which allows sequencing functions which return a value in a context;
- [MonadThrow](src/Typeclass/MonadThrow.php), which allows managing exceptions in a pure way
- [Foldable](src/Typeclass/Foldable.php), which allows to shrink a data structure to a single value;
- [Traversable](src/Typeclass/Traversable.php), which allows transforming a data structure with a function returning values in an applicative context;
- [Semigroup](src/Typeclass/Semigroup.php), which allows combining two values of the same type;
- [Monoid](src/Typeclass/Monoid.php), which allows creating an identity element;
- [Bifunctor](src/Typeclass/Bifunctor.php), which models context which depends on two covariant type variables;
- [Profunctor](src/Typeclass/Profunctor.php), which models context which depends on a contravariant and a covariant type variable.

More details on each typeclass can be found in the [docs/typeclasses folder](docs/typeclasses).

### Typeclasses and data structures

[](#typeclasses-and-data-structures)

As a [design principle](adr/2021-11-22-methods-come-from-typeclasses.md) for this library, we try to expose on our data structures only methods which come from a typeclass. This means that the provided data structure have a standard common API which makes use of typeclasses instances.

For example, `Either` has two `Apply` instances. To choose which one you want to use, `Either` exposes the `iapply`method which takes as first argument an instance of an `Apply` typeclass for `Either`.

```
/**
 * @template A
 * @template B
 */
final class Either
{
    /**
     * @template C
     * @param Apply $apply
     * @param HK1 $f
     * @return Either
     */
    public function iapply(Apply $apply, HK1 $f): self
}
```

We are able to specify that a typeclass instance refers to a specific data structure using the so-called [`Brands`](src/Brand/Brand.php), which are nothing else that tags at the type level which enable us to simulate higher kinded types.

### Default typeclass instances

[](#default-typeclass-instances)

More often than not a data structure admits only one instance of a typeclass, or there exists one which is considered standard in the literature. In such cases it is quite inconvenient to sustain the burden of passing the typeclass instance; to ease the pain, we expose also the method where the default typeclass instance is already provided.

Continuing with the example in the previous section, `Either` exposes also a method `apply` where the `EitherApply`instance is hardcoded.

```
/**
 * @template A
 * @template B
 */
final class Either
{
    /**
     * @template C
     * @param HK1 $f
     * @return Either
     */
    public function apply(HK1 $f): self
    {
        return $this->iapply(new EitherApply(), $f);
    }
}
```

Contributing
------------

[](#contributing)

If you wish to contribute to the project, please read the [CONTRIBUTING notes](CONTRIBUTING.md).

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance44

Moderate activity, may be stable

Popularity41

Moderate usage in the ecosystem

Community23

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 87.5% 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 ~87 days

Recently: every ~135 days

Total

14

Last Release

420d ago

Major Versions

v1.2.0 → v2.0.02023-03-21

v1.4.0 → v3.0.02023-09-27

v1.4.1 → v3.1.02024-08-27

PHP version history (3 changes)v1.0.0PHP ^7.4 || ^8.0

v1.1.1PHP &gt;= 7.4

v2.0.0PHP &gt;= 8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/2643972?v=4)[Marco Perone](/maintainers/marcosh)[@marcosh](https://github.com/marcosh)

---

Top Contributors

[![marcosh](https://avatars.githubusercontent.com/u/2643972?v=4)](https://github.com/marcosh "marcosh (322 commits)")[![drupol](https://avatars.githubusercontent.com/u/252042?v=4)](https://github.com/drupol "drupol (41 commits)")[![someniatko](https://avatars.githubusercontent.com/u/15856706?v=4)](https://github.com/someniatko "someniatko (2 commits)")[![derickr](https://avatars.githubusercontent.com/u/208074?v=4)](https://github.com/derickr "derickr (1 commits)")[![jdreesen](https://avatars.githubusercontent.com/u/424602?v=4)](https://github.com/jdreesen "jdreesen (1 commits)")[![veewee](https://avatars.githubusercontent.com/u/1618158?v=4)](https://github.com/veewee "veewee (1 commits)")

---

Tags

data-structuresfunctional-programminghacktoberfestphppsalmdata structuresfunctional-programmingmonadfunctormonoidApplicative

###  Code Quality

Static AnalysisPsalm

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/marcosh-lamphpda/health.svg)

```
[![Health](https://phpackages.com/badges/marcosh-lamphpda/health.svg)](https://phpackages.com/packages/marcosh-lamphpda)
```

###  Alternatives

[php-ds/php-ds

Specialized data structures as alternatives to the PHP array

4108.8M134](/packages/php-ds-php-ds)[qaribou/immutable.php

Immutable, highly-performant collections, well-suited for functional programming and memory-intensive applications.

344146.0k](/packages/qaribou-immutablephp)[react/partial

Partial function application.

115376.0k13](/packages/react-partial)[doganoo/php-algorithms

A collection of common algorithms implemented in PHP. The collection is based on "Cracking the Coding Interview" by Gayle Laakmann McDowell

9485.4k7](/packages/doganoo-php-algorithms)[mpetrovich/dash

A functional programming library for PHP. Inspired by Underscore, Lodash, and Ramda.

10428.9k1](/packages/mpetrovich-dash)[chemem/bingo-functional

A simple functional programming library.

716.9k3](/packages/chemem-bingo-functional)

PHPackages © 2026

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