yuhanz/ramda-php

该软件包的最新版本(dev-master)没有提供许可证信息。

函数式编程库,模仿 ramdajs。https://github.com/yuhanz/ramda-php

dev-master 2017-09-04 17:26 UTC

This package is not auto-updated.

Last update: 2024-09-24 23:00:45 UTC


README

这是 ramda.js 的 PHP 实现——一个函数式编程库。(http://ramdajs.com/)

到目前为止,只实现了 curry 函数和部分函数。该库可以执行 ramda.js 示例中的操作。

实现的方法

  • add
  • addIndex
  • adjust
  • all
  • allPass
  • allUniq
  • always
  • and
  • any
  • anyPass
  • ap
  • aperture
  • append
  • apply
  • applySpec
  • assoc
  • assocPath
  • binary
  • bind
  • both
  • call
  • chain
  • clamp
  • clone
  • comparator
  • complement
  • compose
  • composeK
  • composeP
  • concat
  • cond
  • construct
  • constructN
  • contains
  • converge
  • countBy
  • curry
  • curryN
  • dec
  • defaultTo
  • difference
  • differenceWith
  • dissoc
  • dissocPath
  • divide
  • drop
  • dropLast
  • dropLastWhile
  • dropRepeats
  • dropRepeatsWith
  • dropWhile
  • either
  • empty
  • eqBy
  • eqProps
  • equals
  • evolve
  • filter
  • find
  • findIndex
  • findLast
  • findLastIndex
  • flatten
  • flip
  • forEach
  • fromPairs
  • groupBy
  • gt
  • gte
  • has
  • hasIn
  • head
  • identical
  • identity
  • ifElse
  • inc
  • indexBy
  • indexOf
  • init
  • insert
  • insertAll
  • internal
  • intersection
  • intersectionWith
  • intersperse
  • into
  • invert
  • invertObj
  • invoker
  • is
  • isArrayLike
  • isEmpty
  • isNil
  • join
  • juxt
  • keys
  • keysIn
  • last
  • lastIndexOf
  • length
  • lens
  • lensIndex
  • lensPath
  • lensProp
  • lift
  • liftN
  • lt
  • lte
  • map
  • mapAccum
  • mapAccumRight
  • mapObjIndexed
  • match
  • mathMod
  • max
  • maxBy
  • mean
  • median
  • memoize
  • merge
  • mergeAll
  • mergeWith
  • mergeWithKey
  • min
  • minBy
  • modulo
  • multiply
  • nAry
  • negate
  • none
  • not
  • nth
  • nthArg
  • objOf
  • of
  • omit
  • once
  • or
  • over
  • pair
  • partial
  • partialRight
  • partition
  • path
  • pathEq
  • pathOr
  • pathSatisfies
  • pick
  • pickAll
  • pickBy
  • pipe
  • pipeK
  • pipeP
  • pluck
  • prepend
  • product
  • project
  • prop
  • propEq
  • propIs
  • propOr
  • propSatisfies
  • props
  • range
  • reduce
  • reduceBy
  • reduceRight
  • reduced
  • reject
  • remove
  • repeat
  • replace
  • reverse
  • scan
  • sequence
  • set
  • slice
  • sort
  • sortBy
  • split
  • splitAt
  • splitEvery
  • splitWhen
  • subtract
  • sum
  • symmetricDifference
  • symmetricDifferenceWith
  • 尾部
  • 取最后
  • 取最后直到
  • 取直到
  • 滴答
  • 测试
  • 次数
  • 转为小写
  • 转为键值对
  • 转为键值对在
  • 转为字符串
  • 转为大写
  • 转换
  • 转置
  • 遍历
  • 修剪
  • 尝试捕获
  • 类型
  • 反应用
  • 一元
  • 解curryN
  • 展开
  • 并集
  • 并集用
  • 唯一
  • 唯一按
  • 唯一用
  • 除非
  • 展开
  • 更新
  • 使用with
  • 值在
  • 视图
  • 哪里
  • 哪里等于
  • 没有
  • 包装
  • 笛卡尔积
  • 连接
  • 连接对象
  • 连接用

引入方法

R::$flatten1 - 以非递归方式展开(仅限于1层)。

如何使用

组合函数

  require_once 'ramda.php';
  
  $getIncompleteTaskSummaries = function($membername) {
    return (R::$compose)(
      (R::$sortBy)((R::$prop)('dueDate')),
      (R::$map)((R::$pick)(['id', 'dueDate', 'title', 'priority'])),
      (R::$reject)((R::$propEq)('complete', true)),
      (R::$filter)((R::$propEq)('username', $membername)),
      (R::$prop)('tasks'));
  };

在数据上使用函数

  $data = [
      'result' => "SUCCESS",
      'interfaceVersion' => "1.0.3",
      'requested' => "10/17/2013 15:31:20",
      'lastUpdated' => "10/16/2013 10:52:39",
      'tasks' => [
          ['id' => 104, 'complete' => false,            'priority' => "high",
                    'dueDate' => "2013-11-29",      'username' => "Scott",
                    'title' => "Do something",      'created' => "9/22/2013"],
          ['id' => 105, 'complete' => false,            'priority' => "medium",
                    'dueDate' => "2013-11-22",      'username' => "Lena",
                    'title' => "Do something else", 'created' => "9/22/2013"],
          ['id' => 107, 'complete' => false,             'priority' => "high",
                    'dueDate' => "2013-11-22",      'username' => "Mike",
                    'title' => "Fix the foo",       'created' => "9/22/2013"],
          ['id' => 108, 'complete' => false,            'priority' => "low",
                    'dueDate' => "2013-11-15",      'username' => "Punam",
                    'title' => "Adjust the bar",    'created' => "9/25/2013"],
          ['id' => 110, 'complete' => false,            'priority' => "medium",
                    'dueDate' => "2013-11-15",      'username' => "Scott",
                    'title' => "Rename everything", 'created' => "10/2/2013"],
          ['id' => 112, 'complete' => true,             'priority' => "high",
                    'dueDate' => "2013-11-27",      'username' => "Lena",
                    'title' => "Alter all quuxes",  'created' => "10/5/2013"],
          ['id' => 122, 'complete' => false,             'priority' => "high",
                    'dueDate' => "2013-11-01",      'username' => "Mike",
                    'title' => "Fix the bar",       'created' => "9/22/2013"],
          ['id' => 123, 'complete' => true,             'priority' => "high",
                    'dueDate' => "2013-11-22",      'username' => "Mike",
                    'title' => "Fix the foobar",       'created' => "9/22/2013"],

      ]
  ];
  $tasks = $getIncompleteTaskSummaries("Mike")($data);
  print_r($tasks);

输出

(
    [0] => Array
        (
            [id] => 122
            [dueDate] => 2013-11-01
            [title] => Fix the bar
            [priority] => high
        )

    [1] => Array
        (
            [id] => 107
            [dueDate] => 2013-11-22
            [title] => Fix the foo
            [priority] => high
        )

)

注意:由于PHP编译器的缺陷,静态方法 R::$add(1,2) 不会编译。为了解决这个问题,请将静态方法放在括号内 (R::$add)(1,2)

curry函数

(注意:R::$_ 是占位符参数。)

  $f3 = function($a, $b, $c) {
      return $a*2+$b*3+$c*4;
  };
  $c = R::curry($f3);
  $cc = $c(R::$_,2,R::$_);
  print_r($cc(1,3));

输出

  20

(更多示例可以在 /tests 文件夹中找到。)