【问题标题】:x = (a) => b(a) is not the same thing as `x = b`x = (a) => b(a) 与 `x = b` 不同
【发布时间】:2017-05-17 00:05:54
【问题描述】:

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!

第一个返回函数,第二个返回结果

检查我的代码 sn-p...

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') 返回一个函数,这没关系,因为R.any 的第一个参数是一个函数
  2. getErrors('mac') 返回一个数组,这没关系,因为R.any 的第二个参数是一个数组。

【问题讨论】:

  • @Xufox, R.propEq 是咖喱。 R.propEq('a', 'b')R.propEq('a')('b') 相同,正如您在我的问题中看到的那样。
  • 这真的取决于 R.propEq 到底是什么,以及你所说的“不起作用”到底是什么意思。您已经编辑了您的问题,但现在您的工作行在两个代码示例中不一致。哪一个真的不行?以及如何?
  • 我从未见过R.propEq 像你所说的“不工作”那样使用 - 也许这就是它不起作用的原因,因为它不应该那样使用
  • 另外,它“不工作”是什么意思,有错误吗?还是您尝试错误地使用equalField
  • The first one return a function 是的,它会,当然会,因为它会

标签: javascript functional-programming ramda.js


【解决方案1】:

这一切都取决于b ...

x = (a) => b(a)x = b相同

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

问题更加复杂,因为 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

这会产生一些混乱(我个人认为这很糟糕),但我们现在将忽略它


简化演示

下面: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 是一个需要多个参数的函数——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 函数,它需要多个参数 - 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 魔法酱;混乱的根源

所以特别是关于 Ramda,答案稍微复杂一些

下面,如果我们以 curried 形式调用 xy,我们得到的正是我们所期望的答案 - 然而,如果我们用 x 和 y >both 剩下的参数,那么只有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)}

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

您的意图

我(认为)我了解您在更新后的帖子中想要做什么。如果是这样,下面的sn-p可能对你有一些帮助

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

注意不要迷恋无点编程。在上面的代码中,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

仅当 所有 函数接受/期望恰好 1 个参数 tho(在 lambda 演算中就是这种情况)时,这仅作为 定律 起作用。

【讨论】:

  • 这个答案太棒了!
  • 是的……我同意。帮我很多。
  • @zerkms 谢谢。这意味着很多来自你^_^
  • 当然,这是一个很好的答案。虽然我同意我们不应该迷恋无点,但 Ramda 确实提供了useWith,如果您愿意,可以在这种情况下允许无点。你可以这样使用它:const errorsInclude = R.useWith(R.any, [ R.propEq('field'), R.prop('errors')]);
猜你喜欢
  • 1970-01-01
  • 2021-10-06
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 2022-04-11
  • 1970-01-01
  • 1970-01-01
  • 2014-03-29
相关资源
最近更新 更多