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

标签 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();


有什么区别..

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

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

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

检查我的代码片段...

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 的第二个参数是一个数组。

最佳答案

这一切都取决于b ......

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

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

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

// 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 魔法酱;困惑的根源

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

下面,如果我们以柯里化(Currying)形式调用 xy,我们会得到我们期望的答案——但是,如果我们调用xy 以及 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

您的意图

我(认为)我理解您在更新后的帖子中尝试做的事情。如果是这样,以下代码片段可能对您有所帮助

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 必须 以柯里化(Currying)形式调用。 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 个参数时作为一个定律起作用(在 lambda 演算中就是这种情况)。

关于javascript - x = (a) => b(a) 与 `x = b` 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44013478/

相关文章:

kotlin - 在 Kotlin 中以纯函数式编程风格在 O(n) 时间内计算所有前缀和

javascript - 在对象上使用过滤器

javascript - 尝试重构此代码以仅使用不可变结构

php - 输入时提交

javascript - 包含分页后搜索功能不起作用

javascript - 按钮上的简单 onclick 功能不起作用

java - 在工厂类内部或外部有一个缓存机制更好吗?

ramda.js - Ramda 相当于 underscore.js 'compact' 是什么?

ramda.js - Ramda - 如何向数组中的对象添加新属性

javascript - 汉堡包菜单按钮更改为 x 按钮