maarky/singlecontainer
Composer 安装命令:
composer require maarky/singlecontainer
包简介
Interface for SingleContainer monads
README 文档
README
SingleContainer is an interface for creating monads. The idea is that you can create a container that wraps a single value, or no value, and then perform actions on that value and create new monads containing that value or the result of an action performed on that value.
Although it doesn't have to be implemented this way, the idea is that a SingleContainer should expect values of specific types. For example, one type of SingleContainer might only contain strings. This provides type safety, allowing you to apply functions with the confidence that the function is able to operate on the type of value stored in the container.
Documentation
A number of these methods take callables as their argument. These methods come in two types: those whose container does hold a value, and those whose container holds no value. On those methods which do expect a container to have a value the callable must always take that value as the first and only required argument. On those methods which do not expect a container to have a value the callable can optionally take the container as its only argument. The reason the container is passed to the callables is because it is possible the container might have additional information. For example, if the container has no value then the callable might want to know what type of container is calling it in order to provide context on what it should do.
Methods that expect a container to have a value are map(), flatmap(), filter(), filternot(), and foreach().
Methods that expect a container to have no value are getOrCall(), orCall(), and fornothing().
get()
Retrieves the container value. Must throw a Type error if the container has no value.
getOrElse($else)
Retrieves the container value, or $else if the container has no value. Must throw a Type error if the container has no value and $else is a different type than what the container should have. In other words, if the container is meant to store strings then $else must be a string.
getOrCall(callable $call)
Retrieves the container value, or calls $call if the container has no value and returns the result. Must throw a Type error if the container has no value and $call returns a different type than what the container should have. In other words, if the container is meant to store strings then $call must return a string.
$call should have signature function(SingleContainer $container): mixed where $container is the calling SingleContainer and mixed is the same datatype that should be stored in the container.
orElse(SingleContainer $else)
Returns container if the container has a value, otherwise returns $else.
orCall(callable $call): SingleContainer
Returns container if the container has a value, otherwise calls $call and returns its value.
$call should have signature function(SingleContainer $container): SingleContainer where $container is the calling SingleContainer.
isDefined(): bool
Returns true if container has value, otherwise false.
isEmpty(): bool
Returns false if container has value, otherwise true.
filter(callable $filter): SingleContainer
Calls filter passing in the container value. Returns itself if filter returns true, otherwise an empty container of the same type. If this method is called on an empty container there is nothing to filter so it should ignore $filter and return itself.
$filter should have signature function(mixed $var): boolean where $var is the data being stored in the calling SingleContainer.
filterNot(callable $filter): SingleContainer
The inverse of the filter() method. It should return itself if $filter returns false, otherwise it should return an empty container.
map(callable $map): SingleContainer
If container has a value it should call $map, passing in the value. $map should return a value to be wrapped inside a SingleContainer. If $map returns a SingleContainer it should be wrapped inside another SingleContainer. If $map returns an empty value it should return an empty SingleContainer. If the calling container has no value it should ignore $map and return itself.
$map should have signature function(mixed $var): mixed where $var is the data being stored in the calling SingleContainer.
flatMap(callable $map): SingleContainer
If container has a value it should call $map, passing in the value. $map must return any type of SingleContainer. If container has no value it should ignore $map and return itself.
$map Should have signature function(mixed $var): SingleContainer where $var is the data being stored in the calling SingleContainer.
foreach(callable $each): SingleContainer
If container has a value it calls $each and does nothing with the result. Then returns itself. If container has no value it ignores $each and returns itself.
This method is meant to be used when you want to produce side effects, not when you want to alter the container value.
$each should have signature function(mixed $var): void where $var is the data being stored in the calling SingleContainer.
fornothing(callable $nothing): SingleContainer
If container has a value it ignores $nothing and returns itself. If container has no value it calls $nothing and does nothing with the result. Then returns itself.
This method is meant to be used when you want to produce side effects.
$nothing should have signature function(SingleContainer $container): void where $container is the calling SingleContainer.
equals($value): bool
Determines if two containers are equal. They must be the same container class. They must also have the same value, or they must both be empty.
maarky/singlecontainer 适用场景与选型建议
maarky/singlecontainer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 88 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 02 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「monad」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 maarky/singlecontainer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 maarky/singlecontainer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 maarky/singlecontainer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Option type (Some/None) replacing nullable types with explicit presence semantics
Result type capturing success or failure as a value for controlled error handling
A collection of functional programming data structures
A functional programming library for PHP
Alfabank REST API integration
Functional and Monad Utility
统计信息
- 总下载量: 88
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-02-06