munusphp / munus
结合面向对象编程的力量和函数式编程的优雅。
0.16.0
2024-06-01 13:24 UTC
Requires
- php: ^8.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.27
- nette/php-generator: ^4.0
- phpstan/phpstan: ^1.11.2
- phpunit/phpunit: ^9.6.13
README
结合面向对象编程的力量和函数式编程的优雅。通过减少代码量来提高健壮性。
目前处于实验阶段。
由于缺乏泛型类型,Munus 通过 PHPStan template
注解来实现泛型。
流示例:找到前十个偶数的平方和
Stream::from(1) ->filter(fn($n) => $n%2===0) ->map(fn($n) => $n**2) ->take(10) ->sum();
其他示例
/** @var Stream<int> $stream */ $stream = Stream::range(1, 10)->map(function(int $int): int {return $int * 5}); /** @var Option<Success> $option */ $option = Option::of(domainOperation()); /** @return Either<Failure,Success> */ function domainOperation(): Either {} /** @var TryTo<Result> $result */ $result = TryTo::run(function(){throw new \DomainException('use ddd');}); $result->getOrElse(new Result())
目标是帮助实现:Psalm 能够推断代码库的 100% 类型
功能
值
- TryTo
- Either
- Option
- Lazy
集合
- Set
- Stream(作为懒连接列表实现)
- GenericList(作为不可变连接列表实现)
- 迭代器
其他
- Tuple
路线图
- 模式匹配
- 属性检查
灵感来源
这个库受到了 vavr.io 的启发
许可证
Munus 在 MIT 许可证下发布。有关详细信息,请参阅捆绑的 LICENSE 文件。