幸运的是,JavaScript 有资源来解决它缺乏惰性的问题。因此,完全可以通过以下方式使用 lambda 函数来声明递归 pointfree 解决方案:a => f(a)
。只需更换R.map(removeNull)
with R.map(a => removeNull(a))
.
const removeNulls = R.ifElse(
R.either(R.is(Array), R.is(Object)),
R.pipe(
R.filter(R.pipe(R.isNil, R.not)),
R.map(a => removeNulls(a))
),
R.identity
)
对于你的情况,我建议你使用R.reject
这是相反的R.filter
。由于您要否定谓词,R.filter(R.pipe(R.isNil, R.not))
等于R.reject(R.isNil)
const removeNulls = R.ifElse(
R.either(R.is(Array), R.is(Object)),
R.pipe(
R.reject(R.isNil),
R.map(a => removeNulls(a))
),
R.identity
)
最后,这个函数的结构如下ifElse(predicate, whenTrue, identity)
这等于when(predicate, whenTrue)
const removeNulls = R.when(
R.either(R.is(Array), R.is(Object)),
R.pipe(
R.reject(R.isNil),
R.map(a => removeNulls(a))
)
)
简化版本,关于 Declan Whelan 的评论,因为数组是对象
const removeNulls = R.when(
R.is(Object),
R.pipe(
R.reject(R.isNil),
R.map(a => removeNulls(a))
)
)