phonad/core
最新稳定版本:v0.1.0
Composer 安装命令:
composer require phonad/core
包简介
Building blocks for functional programming in PHP
README 文档
README
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:
$ composer require phonad/core
Data Structures
Maybe
Handles optional values without null.
<?php use Phonad\Core\Data\Maybe; $val = Maybe::of(10) ->map(fn($n) => $n * 2) ->orElse(0); // 20
Either
Error handling: Right for success, Left for failure.
<?php use Phonad\Core\Data\Either; $res = Either::Right(10) ->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 use Phonad\Core\Data\ImmutableList; $list = ImmutableList::from([1, 2, 3]) ->cons(0) ->filter(fn($n) => $n > 0); // [1, 2, 3]
Effects
Reader
Dependency injection via environment.
<?php use Phonad\Core\Effect\Reader; $logic = Reader::ask()->map(fn($env) => "Env is: " . $env['mode']); echo $logic->run(['mode' => 'production']);
Writer
Pure logging alongside values.
<?php use Phonad\Core\Effect\Writer; [$val, $logs] = Writer::of(10) ->flatMap(fn($n) => Writer::of($n + 1, ["Incremented"])) ->run();
IO
Lazy side effects.
<?php use Phonad\Core\Effect\IO; // Wrap the effect into the `IO` monad $sayHello = fn(string $name) => 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
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-12-18