x = (a) => b(a) 与 `x = b` 不同

x = (a) => b(a) is not the same thing as `x = b`

const x = (a) => b(a) 等同于 const x = b

const a = () => { console.log('hi'); }
let x1 = a;
let x2 = () => a();

x1();
x2();


..有什么区别

const equalField = R.propEq('field'); // NOT WORK!

const equalField = (f) => R.propEq('field')(f); // WORKS!

第一个return函数,第二个return结果.

检查我的代码片段...

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);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.23.0/ramda.js"></script>

我的意图是...

当我打电话给anyField('mac')

R.any 将与 R.any(equalField('mac'), getErrors('mac'))

一起调用
  1. equalField('mac') return是一个函数,这没关系,因为 R.any 的第一个参数是一个函数
  2. getErrors('mac') return是一个数组,这没问题,因为R.any的第二个参数是一个数组。

全看b

x = (a) => b(a) is not the same thing as x = b

  • 如果 b 是一个只需要 1 个参数的函数,或者 b 是一个柯里化函数
  • ,那你就错了
  • 你是对的如果 b 是一个需要超过 1 个参数的函数

问题变得复杂,因为 Ramda 有一个神奇的 API 允许你与单个函数交互,就好像它是柯里化的 未柯里化的。

// 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

这会引起一些混乱(我个人认为这很糟糕),但我们暂时忽略它


简化演示

下面:b 是一个需要 1 个参数的函数。 xy 按预期工作

const b = n => n + 1
const x = b
const y = n => b (n)

console.log(x(1)) // 2
console.log(y(1)) // 2


下面: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)) // NaN


下面: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)) // 3


拉姆达魔法酱;混淆的来源

所以具体到 Ramda,答案稍微复杂一些

下面,如果我们以柯里化形式调用 xy,我们将得到我们期望的准确答案 – 然而 ,如果我们调用 xy 以及 both 个剩余参数,那么只有 x 会按预期执行; y 将 return 等待最后一个参数的函数

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)}

错误当然是因为我们定义的方式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

您的意向

我(认为)我理解你在更新后的 post 中想要做什么。如果是这样,以下代码片段可能对您有所帮助

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

注意不要迷恋 point-free 编程。在上面的代码中,errorsInclude 必须 以柯里化形式调用。 Ramda Way® 可能建议您使用 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

但是!这种做法违背了 R.compose 的目的——我个人认为如果你想遵守 ramda 约定,以下可能是最好的结果

const errorsInclude = R.curry((type, x) =>
  R.any(fieldEq(type), R.prop('errors', x)))

它没有什么性感的地方,但至少它很简单——只是我的 2 美分。


@ScottSauyet 评论说它也可以使用 R.useWith 来解决:

const errorsInclude = R.useWith(R.any, [R.propEq('field'), R.prop('errors')]);

物有所值

您的问题标题是 lambda 演算的核心' eta conversion

const g = x => f (x)  ==  f
      g(y)            ==  f(y)
      g               ==  f

all 函数 accept/expect 恰好有 1 个参数时,这仅作为 law 起作用(在 lambda 中就是这种情况微积分)。