masterfermin02 / slash
PHP的函数式编程库
v2.0.3
2023-01-26 04:22 UTC
Requires
- php: ^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.14
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ~9
- squizlabs/php_codesniffer: ~3.0
README
PHP程序员的功能库,类似于Ramdajs。
功能
groupBy函数的小型使用示例
<?php use function Slash\groupBy; $records = [ ['id' => 1, 'value1' => 5, 'value2' => 10], ['id' => 2, 'value1' => 50, 'value2' => 100], ['id' => 1, 'value1' => 2, 'value2' => 2], ['id' => 2, 'value1' => 15, 'value2' => 20], ['id' => 3, 'value1' => 15, 'value2' => 20], ]; $groupById = groupBy('id'); $grouped = $groupById($records); /* * resultado : [ * 1 => [ [ "id" => 1, "value1" => 5, "value2" => 10 ], [ "id" => 1, "value1" => 1, "value2" => 2 ] ], * 2 => [ [ "id" => 2, "value1" => 50, "value2" => 100 ], [ "id" => 2, "value1" => 15, "value2" => 20 ] ], * 3 => [ [ "id" => 3, "value1" => 15, "value2" => 20 ] ] * ]; */
Map使用
<?php use Slash\Slash; Slash\map([1, 2, 3], fn ($n) => $n * 2); // === [2, 4, 6]
使用slash对象的示例
<?php use Slash\Slash; Slash::max([1, 2, 3]) // => 3 Slash::flatten([1, [2, [3]]]) // => [1, 2, 3] Slash::last([1, 2, 3], 2) // => [2, 3]
为什么选择Slash?
已经存在几个功能很好的库,它们通常旨在成为通用工具包,适用于多种范式。Slash有一个更专注的目标。我们想要一个专门为函数式编程风格设计的库,它使创建功能管道变得容易,它永远不会修改用户数据。
有什么不同?
slash的主要区别特征包括:
Slash强调更纯粹的函数式风格。不可变性和无副作用的函数是其设计哲学的核心。这可以帮助您用简单、优雅的代码完成任务。
Slash函数是自动柯里化的。这允许您通过不提供最终参数,轻松地从旧函数构建新函数。
Slash函数的参数排列使得柯里化变得方便。要操作的数据通常最后提供。
这两点共同使构建函数作为更简单函数的序列变得非常容易,每个函数都转换数据并将其传递给下一个函数。Slash旨在支持这种编码风格。
安装
需要PHP 8+
composer require masterfermin02/slash
对于PHP 7,请使用版本1.4.0
用法
Slash操作是纯函数,可以单独使用。
它提供了什么
- 60+ 个您可以在项目中使用的有用函数。
- Slash已完全测试。
- 源代码整洁且带有文档。
以下是您可以获得的:
- boolean isDate(mixed $value)
- boolean isNumber(mixed $value)
- boolean isString(mixed $value)
- boolean isFunction(mixed $value)
- boolean isEmpty(mixed $value)
- boolean isEqual(mixed $left, mixed $right)
- boolean isBoolean(mixed $value)
- boolean isObject(mixed $value)
- boolean isArray(mixed $value)
- boolean isTraversable(mixed $value)
- boolean isNull(mixed $value)
- boolean has(mixed $object, string $key)
- array keys(mixed $object)
- array values(mixed $object)
- array methods(mixed $object)
- mixed copy(mixed $value)
- mixed extend(mixed $source, mixed $destination)
- mixed apply(mixed $object, Closure $closure)
- mixed defaults(mixed $object, array|mixed $defaults)
- string escape(string $string)
- string id(string $prefix = '')
- mixed with(mixed $value)
- void times(integer $number, Closure $closure)
- mixed cache(Closure $closure)
- mixed wrap(Closure $closure, Closure $wrapper)
- mixed compose(array $closures, array $arguments = array())
- void once(Closure $closure)
- mixed after(integer $number, Closure $closure)
- mixed|array first(array $elements, integer $amount = 1)
- array initial(array $elements, integer $amount = 1)
- array rest(array $elements, integer $index = 1)
- mixed|array last(array $elements, integer $amount = 1)
- array pack(array $elements)
- array flatten(array $elements)
- array range(integer $to, integer $from = 0, integer $step = 1)
- array difference(array $one, array $another)
- array unique(array $elements, Closure $iterator = null)
- 没有元素的数组(array without(array $elements, array $ignore))
- 将两个数组组合成一个新数组(array zip(array $one, array $another))
- 在数组中查找元素的索引(integer indexOf(array $elements, mixed $element))
- 求两个数组的交集(array intersection(array $one, array $another))
- 求两个数组的并集(array union(array $one, array $another))
- 遍历集合(void each(array $collection, Closure $iterator))
- 使用回调函数映射集合的元素到新数组(array map(array $collection, Closure $iterator))
- 将值转换为数组(array toArray(mixed $value))
- 获取集合的大小(integer|null size(array|Countable $value))
- 对数组进行随机排序(array shuffle(array $collection))
- 检查集合中是否存在满足条件的元素(boolean any(array $collection, Closure $iterator))
- 检查集合中所有元素是否都满足条件(boolean all(array $collection, Closure $iterator))
- 从集合中排除满足条件的元素(array reject(array $collection, Closure $iterator))
- 从集合中提取指定键的值(array pluck(array $collection, string $key))
- 检查集合中是否包含指定的值(boolean contains(array $collection, mixed $value))
- 调用集合上的方法(array invoke(array $collection, string $function))
- 使用回调函数计算集合的累加值(mixed reduce(array $collection, Closure $iterator, mixed $initial = 0))
- 根据回调函数对集合进行排序(array sortBy(array $collection, Closure $iterator))
- 根据回调函数对集合进行分组(array groupBy(array $collection, Closure $iterator))
- 获取集合中的最大值(mixed max(array $collection))
- 获取集合中的最小值(mixed min(array $collection))
文档
反馈
发现错误或有建议?请创建一个新的GitHub问题。我们期待您的反馈!