Ramda中的反向布尔返回函数

时间:2019-10-31 13:43:35

标签: javascript typescript functional-programming ramda.js

我刚刚开始探索Ramda库,但遇到了一些问题。

假设我们有一个函数,该函数将字符串和字符串列表作为参数,如果给定的字符串在列表中,则返回true。在第4行,我想记录otherList中未包含的的第一个元素list

const isInList = R.curry((name: string, list: string[]) => list.some(elem => elem === name))
const list = ['a', 'b', 'c']
const otherList = ['a', 'b', 'd', 'c']
console.log(otherList.find(!isInList(R.__, list)))

我找不到能反转给定功能逻辑结果的Ramda函数。

如果存在,则看起来像这样:

const not = (func: (...args: any) => boolean) => (...args: any) => !func(args)

然后可以将我的目标存档:

console.log(otherList.find(not(isInList(R.__, list)))

功能是否像Ramda中那样?

3 个答案:

答案 0 :(得分:1)

找到了!叫做R.complement()

答案 1 :(得分:1)

尝试R.difference():

const list = ['a', 'b', 'c']
const otherList = ['a', 'b', 'd', 'c']
R.difference(otherList, list); //=> ['d']

在线演示here

答案 2 :(得分:1)

R.complement是否定功能的方法

const isInList = R.includes;
const isNotInList = R.complement(isInList);

const list = ['Giuseppe', 'Francesco', 'Mario'];

console.log('does Giuseppe Exist?', isInList('Giuseppe', list));
console.log('does Giuseppe Not Exist?', isNotInList('Giuseppe', list));
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>