ramda.js - 比较两个数字,ramdajs
问题描述
说我有两个功能
const getMeanPrice = R.....
const getLastPrice = R...
我应该使用哪些函数来检查一个值是否大于另一个?
const isLastPriceHigherThanMeanPrice = R. ???
有 R.gt https://ramdajs.com/0.22.1/docs/#gt
但它只接受两个数字。需要接受两个功能的东西。喜欢
R.somefunc(getMeanPrice, getLastPrice)(prices) => boolean
解决方案
lift
将对值进行操作的函数转换为对值容器进行操作的函数。例如,
lift (gt) ([8, 1, 6], [3, 5, 7])
//=> [8 > 3, 8 > 5, 8 > 7, 1 > 3, 1 > 5, 1 > 7, 6 > 3, 6 > 5, 6 > 7]
//=> [true, true, true, false, false, false, true, true, false]
返回某个类型的函数可以被认为是该类型元素的容器,所以如果我们lift R.gt
,它也会对函数进行操作。因此:
// Dummy implementations
const getMeanPrice = R.mean
const getLastPrice = R.last
const isLastPriceHigherThanMeanPrice = R.lift (R.gt) (getLastPrice, getMeanPrice)
console .log ([
[4, 5, 6],
[6, 5, 4],
[8, 6, 7, 5, 3, 0, 9],
[8, 6, 7, 5, 3, 0]
].map(a => `[${a.join(', ')}] ==> ${isLastPriceHigherThanMeanPrice(a)}`).join('\n'))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script>
lift
将适用于任何Apply
类型,即具有合法ap
和map
定义功能的类型。这包括数组、函数和许多其他有用的类型,例如Maybe
、Either
、Future
和许多其他的大多数实现。
推荐阅读
- c - C - 如何确定位的存储方式
- javascript - 我有一个包含三个项目的 CSS 导航栏。我想让每个链接显示自己的内容而不重叠另一个
- c++ - CMake的导出如何知道目标是什么版本?
- django - 使用 Django reduce(or_) - 如何将结果为 0 的不匹配查询存储在变量中
- python - “multi_gpu_model”是否支持“Sequential”?
- php - Scan files in a directory to get the number of methods and PHP classes in a directory
- javascript - 如何计算 JSON 数据在 Jquery 中具有特定值的键的次数?
- amazon-web-services - AWS SNS - how should I be sending message data, MessageAttributes?
- java - 一种 3D 数据结构,以彼此之间的位置关系保存项目
- mysql - MySQL Pivot Tables Without Numbers