rid*_*nsb 1 javascript functional-programming ramda.js
const x = (a) => b(a) 是一样的 const x = b
const a = () => { console.log('hi'); }
let x1 = a;
let x2 = () => a();
x1();
x2();Run Code Online (Sandbox Code Playgroud)
是什么区别..
const equalField = R.propEq('field'); // NOT WORK!
Run Code Online (Sandbox Code Playgroud)
和
const equalField = (f) => R.propEq('field')(f); // WORKS!
Run Code Online (Sandbox Code Playgroud)
第一个返回一个函数,第二个返回结果.
检查我的代码段...
const addingError = {
message: '',
errors: [ { field: "number" }, { field: "mac" } ]
}
// const equalField = R.propEq('field'); // NOT WORK!
const equalField = (f) => R.propEq('field')(f); // WORKS!
const getErrors = R.pipe(R.always(addingError), R.prop('errors'))
const anyField = R.converge(R.any, [equalField, getErrors])
const result = anyField('mac');
console.log(result);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.23.0/ramda.js"></script>Run Code Online (Sandbox Code Playgroud)
我打电话的时候 anyField('mac')
R.any 将被召唤 R.any(equalField('mac'), getErrors('mac'))
equalField('mac')返回一个函数,这是好的,因为第一个参数R.any是一个函数getErrors('mac')返回一个数组,这是好的,因为第二个参数R.any是一个数组.这完全取决于b......
x = (a) => b(a)与... 不是一回事x = b
b一个函数只需要1个参数 - 或者b是一个curried函数,那你就错了b是一个需要多于1个参数的函数,那么你是对的这个问题更复杂,因为Ramda有一个神奇的API,允许你与单个函数进行交互,就好像它是curried 或 uncurried一样.
// Ramda magic
R.propEq('a', 'b', {a: 'b'}) // true
R.propEq('a')('b', {a: 'b'}) // true
R.propEq('a', 'b')({a: 'b'}) // true
R.propEq('a')('b')({a: 'b'}) // true
Run Code Online (Sandbox Code Playgroud)
这会产生一些混乱(我个人认为这很糟糕),但我们暂时忽略它
简化演示
下面:b是一个正好需要1个参数的函数.x并按y预期工作
const b = n => n + 1
const x = b
const y = n => b (n)
console.log(x(1)) // 2
console.log(y(1)) // 2Run Code Online (Sandbox Code Playgroud)
下面:b是一个期望超过1个参数的函数 - y在这种情况下是一个问题
const b = (n,m) => n + m
const x = b
const y = n => b(n)
console.log(x(1,2)) // 3
console.log(y(1,2)) // NaNRun Code Online (Sandbox Code Playgroud)
下面:b是一个curried函数,期望超过1个参数 - y不再是问题
const b = n => m => n + m
const x = b
const y = n => b(n)
console.log(x(1)(2)) // 3
console.log(y(1)(2)) // 3Run Code Online (Sandbox Code Playgroud)
拉姆达魔术酱; 混乱的根源
所以对于Ramda来说,答案稍微复杂一点
下面,如果我们打电话x并y以咖喱形式,我们得到了我们期望的答案 - 但是,如果我们调用x并y使用其余两个参数,那么只会x按预期执行; y将返回一个等待最后一个参数的函数
const b = R.propEq('a')
const x = b
const y = n => b(n)
// call in curried form, everything works as expected
console.log(x('b')({a: 'b'})) // true
console.log(y('b')({a: 'b'})) // true
// call with both args, y blows up
console.log(x('b', {a: 'b'})) // true
console.log(y('b', {a: 'b'})) // function n(r){return 0===arguments.length||b(r)?n:t.apply(this,arguments)}
Run Code Online (Sandbox Code Playgroud)
错误当然是因为我们定义的方式 y
// bad, only accommodates one extra argument
// ramda super magical api would allow any number of arguments per application
const y = n => b(n)
// instead write
const y = (...args) => b(...args)
// above: which of course is stupid in a whole new way
// instead just write
const y = b
// above which is stupid, too
// instead just write
b
Run Code Online (Sandbox Code Playgroud)
你的意图
我(想)我明白你在更新的帖子中想要做什么.如果是这样,以下代码段可能对您有所帮助
const fieldEq = R.propEq('field')
const errorsInclude = type =>
R.compose (R.any(fieldEq(type)), R.prop('errors'))
const addingError = {
message: '',
errors: [ { field: "number" }, { field: "mac" } ]
}
errorsInclude('mac') (addingError) // true
errorsInclude('number') (addingError) // true
errorsInclude('foo') (addingError) // false
Run Code Online (Sandbox Code Playgroud)
注意不要迷恋无点编程.在上面的代码中,errorsInclude 必须以curry形式调用.RamdaWay®可能会建议您使用R.curry二进制函数
const errorsInclude = R.curry((type, x) =>
R.compose (R.any(fieldEq(type)), R.prop('errors')) (x))
// now you can call it either way
console.log(errorsInclude('mac', addingError)) // true
console.log(errorsInclude('mac')(addingError)) // true
Run Code Online (Sandbox Code Playgroud)
但!这种方式失败了R.compose- 我个人认为如果你想遵守ramda惯例,以下可能是最好的结果
const errorsInclude = R.curry((type, x) =>
R.any(fieldEq(type), R.prop('errors', x)))
Run Code Online (Sandbox Code Playgroud)
没有什么性感,但它至少是直截了当的 - 只是我的2美分.
@ScottSauyet评论说它也可以用R.useWith以下方法解决:
const errorsInclude = R.useWith(R.any, [R.propEq('field'), R.prop('errors')]);
Run Code Online (Sandbox Code Playgroud)
物有所值
你的问题标题是lambda演算' eta转换的核心-
const g = x => f (x) == f
g(y) == f(y)
g == f
Run Code Online (Sandbox Code Playgroud)
当所有函数接受/期望恰好1个参数时,这仅作为一个定律(在lambda演算中就是这种情况).