https://github.com/atomicptr/phonad
Building blocks for functional programming in PHP, inspired by Haskell.
https://github.com/atomicptr/phonad
functional-programming php php-library
Last synced: 17 days ago
JSON representation
Building blocks for functional programming in PHP, inspired by Haskell.
- Host: GitHub
- URL: https://github.com/atomicptr/phonad
- Owner: atomicptr
- License: mit
- Created: 2025-12-04T16:34:59.000Z (about 2 months ago)
- Default Branch: master
- Last Pushed: 2025-12-18T18:38:25.000Z (about 1 month ago)
- Last Synced: 2026-01-14T00:20:54.317Z (18 days ago)
- Topics: functional-programming, php, php-library
- Language: PHP
- Homepage:
- Size: 30.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Phonad
Building blocks for functional programming in PHP, inspired by Haskell.
**Note**: This is an experiment to play around with Functional Programming in PHP
## Install
Install via composer:
```bash
$ composer require phonad/core
```
## Data Structures
### Maybe
Handles optional values without `null`.
```php
map(fn($n) => $n * 2)
->orElse(0); // 20
```
### Either
Error handling: `Right` for success, `Left` for failure.
```php
flatMap(fn($n) => $n > 5 ? Either::Right($n) : Either::Left("Too small"))
->either(fn($err) => "Error: $err", fn($val) => "Value: $val");
```
### ImmutableList
Immutable linked list (PDS).
```php
cons(0)
->filter(fn($n) => $n > 0); // [1, 2, 3]
```
## Effects
### Reader
Dependency injection via environment.
```php
map(fn($env) => "Env is: " . $env['mode']);
echo $logic->run(['mode' => 'production']);
```
### Writer
Pure logging alongside values.
```php
flatMap(fn($n) => Writer::of($n + 1, ["Incremented"]))
->run();
```
### IO
Lazy side effects.
```php
IO::make(function () use ($name) {
echo "Hello, $name!";
});
// Chain the logic (nothing actually happens yet)
$program = IO::of('world')
->map('strtoupper')
->flatMap(fn($name) => $sayHello($name));
// FIRE!
$program->run(); // Outputs: Hello, WORLD!
```
## License
MIT