Ramda 中函数的反向布尔值 return
Reverse boolean return of function in Ramda
我刚刚开始探索 Ramda 库并遇到了一些问题。
假设我们有一个函数,它将字符串和字符串列表作为参数,如果给定的字符串在列表中,则returns为真。在第 4 行,我想记录 otherList
中的第一个元素,即 not 包含在 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 中有这样的功能吗?
找到了!它被称为 R.complement()
试试 R.difference():
const list = ['a', 'b', 'c']
const otherList = ['a', 'b', 'd', 'c']
R.difference(otherList, list); //=> ['d']
在线演示here
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>
我刚刚开始探索 Ramda 库并遇到了一些问题。
假设我们有一个函数,它将字符串和字符串列表作为参数,如果给定的字符串在列表中,则returns为真。在第 4 行,我想记录 otherList
中的第一个元素,即 not 包含在 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 中有这样的功能吗?
找到了!它被称为 R.complement()
试试 R.difference():
const list = ['a', 'b', 'c']
const otherList = ['a', 'b', 'd', 'c']
R.difference(otherList, list); //=> ['d']
在线演示here
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>