我刚刚开始探索 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 中是否存在这样的函数?
最佳答案
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>
关于javascript - Ramda 中函数的反向 bool 返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58644672/