首页 > 解决方案 > __ 应该只适用于咖喱函数吗?为什么它在这里工作?

问题描述

我试图理解为什么__在这段代码中可以正常工作:

function editAddress (id, addressId, model) {
    return BusinessService
      .getById(id)
      .then(unless(
        () => checkUrlValue(addressId, model.id),
        rejectWithError(InvalidData.error('Invalid address data: Address id is different from request'))
      ))
      .then(pipe(
        updateModel(__, 'addresses', model, 'id', addressId),
        juxt([ always(id), identity ]),
        apply(BusinessService.editById)
      ))
      .then(pipe(
        prop('addresses'),
        find(propEq('id', addressId))
      ))
  }
function updateModel (entity, property, model, attr, id) {
  return evolve({
    [property]: pipe(
      juxt([
        findIndex(propEq(attr, id)),
        pipe(
          find(propEq(attr, id)),
          mergeLeft(model)
        ),
        identity
      ]),
      apply(update)
    )
  })(entity)
}

既然调用的函数( updateModel )没有被柯里化,为什么__在这种情况下仍然有效?

标签: javascriptnode.jsfunctional-programmingramda.js

解决方案


updateModel不是柯里化的,而是返回一个evolve被柯里化的函数的结果。第一个调用传入:

{
    [property]: pipe(
      juxt([
        findIndex(propEq(attr, id)),
        pipe(
          find(propEq(attr, id)),
          mergeLeft(model)
        ),
        identity
      ]),
      apply(update)
    )
}

然后用 调用此调用的结果entity,在您的情况下为 __。如果不看其内部,evolve就不可能进一步理解代码。


推荐阅读