【发布时间】:2017-04-20 11:35:32
【问题描述】:
我正在学习无点函数,并正在尝试以这种风格实现这种递归空删除器。
有效,但不是无点:
function removeNulls(obj) {
return R.ifElse(
R.either(R.is(Array), R.is(Object)),
R.pipe(
R.filter(R.pipe(R.isNil, R.not)),
R.map(removeNulls)
),
R.identity
)(obj)
}
module.exports = removeNulls
以下是我的无效尝试:
const removeNulls = R.ifElse(
R.either(R.is(Array), R.is(Object)),
R.pipe(
R.filter(R.pipe(R.isNil, R.not)),
// throws `ReferenceError: removeNulls is not defined`
R.map(removeNulls)
),
R.identity
)
【问题讨论】:
-
你不能,因为 JavaScript 不提供懒惰声明参数的方法。使用
Y组合器可能会很幸运,但这会变得非常难看。 -
@Bergi 感谢您的提示,我将保持原样。如果只是为了将来使用,我很想了解更多关于使用 Y 组合器的信息。看来ramda lacks a
Ycombinator,但我不知道从那里去哪里。谷歌搜索有点困难...... -
@Bergi 是正确的,你不能用 const... ) const recurseAction = action => ifElse( any(is(Array), is(Object)), pipe( action, map(action) ), identity )
-
@NigelBenns 你的
recurseAction不是无点的,也不是递归的——它应该是map(recurseAction(action)) -
在 JS 中实现一个 Y-combinator 版本很容易。但它不是为了任何实用的东西。
标签: javascript recursion functional-programming pointfree ramda.js