javascript - 如何解决 Ramda reduce 函数的错误返回结果 (javascript)

标签 javascript functional-programming reduce ramda.js

我有一个数据结构,我称之为“规范”,如下所示:

const spec = {
  command: {
    name: 'name',
    description: 'description',
    alias: 'alias',
    arguments: '_children/Arguments'
  },
  arguments: {
    name: 'name',
    alias: 'alias',
    optional: 'optional',
    description: 'description'
  }
};

所以 commandarguments 中的元素是映射到路径的属性。最好的例子是 spec.command.arguments。我需要做的是将其转换为另一个具有相同形状的对象,但路径被转换为 Ramda 镜头(使用 R.lensPath)。

所以从概念上讲,这被翻译成这样:

const spec = {
  command: {
    name: lens('name'),
    description: lens('description'),
    alias: lens('alias'),
    arguments: lens('_children/Arguments')
  },
  arguments: {
    name: lens('name'),
    alias: lens('alias'),
    optional: lens('optional'),
    description: lens('description')
  }
};

以上不是字面意思,是伪结构。例如 lens('_children/Arguments') 仅表示使用 Ramda lensPath 构建的镜头。

这是我的代码:

const spec = {
  command: {
    name: 'name',
    description: 'description',
    alias: 'alias',
    arguments: '_children/Arguments'
  },
  arguments: {
    name: 'name',
    alias: 'alias',
    optional: 'optional',
    description: 'description'
  }
};

function lensify (spec) {
  const result = R.pipe(
    R.toPairs,
    R.reduce((acc, pair) => {
      const field = pair[0];
      const path = pair[1];
      const lens = R.compose(
        R.lensPath,
        R.split('/')
      )(path);

      acc[field] = lens; // Is there something wrong with this, if so what?
      return acc;
    }, { dummy: '***' }) // list of pairs passed as last param here
  )(spec);

  // The following log should show entries for 'name', 'description', 'alias' ...
  console.log(`+++ lensify RESULT: ${JSON.stringify(result)}`);
  return result;
}

function makeLenses (spec) {
  const result = {
    command: lensify(spec.command),
    arguments: lensify(spec.arguments)
  };

  return result;
}

makeLenses(spec);

我认为失败的关键点在 reducer 函数内部,它返回更新后的累加器 (acc[field] = lens;)。由于某种我无法理解的原因,这个分配正在丢失,并且在每次迭代中都没有正确填充累加器。从代码示例中可以看出,传递给 reduce 的初始值是一个具有单个 dummy 属性的对象。 reduce 的结果错误地只是这个单一的虚拟值,而不是所有具有各自 Ramda 镜头的字段。

但是,真正让您吃不消的是,在 Ramda repl 中运行的完全相同的代码表现出不同的行为,请参阅 repl 中的这段代码:Ramda code

我正在运行节点版本 10.13.0

Repl 代码产生的结果是这样的:

{
  'arguments': {
    'alias': function (r) {
      return function (e) {
        return z(function (t) {
          return n(t, e)
        }, r(t(e)))
      }
    },
    'description': function (r) {
      return function (e) {
        return z(function (t) {
          return n(t, e)
        }, r(t(e)))
      }
    },
    'dummy': '***',
    'name': function (r) {
      return function (e) {
        return z(function (t) {
          return n(t, e)
        }, r(t(e)))
      }
    },
    'optional': function (r) {
      return function (e) {
        return z(function (t) {
          return n(t, e)
        }, r(t(e)))
      }
    }
  },
  'command': {
    'alias': function (r) {
      return function (e) {
        return z(function (t) {
          return n(t, e)
        }, r(t(e)))
      }
    },
    'arguments': function (r) {
      return function (e) {
        return z(function (t) {
          return n(t, e)
        }, r(t(e)))
      }
    },
    'description': function (r) {
      return function (e) {
        return z(function (t) {
          return n(t, e)
        }, r(t(e)))
      }
    },
    'dummy': '***',
    'name': function (r) {
      return function (e) {
        return z(function (t) {
          return n(t, e)
        }, r(t(e)))
      }
    }
  }
}

如您所见,结果看起来有点复杂,因为每个属性的值都是由 lensProp 创建的镜头。

这与下面的对比(注意命令和参数的顺序是颠倒的,但这应该不重要):

{
  'command': {
    'dummy': '***'
  },
  'arguments': {
    'dummy': '***'
  }
}

在我的单元测试中被返回。

我已经在这上面浪费了大约 2 天时间,现在承认失败了,所以希望有人能对此有所启发。干杯。

最佳答案

这显示了我能想到的最简单的输出用法,将镜头上的 view 映射到一个普通物体上。它似乎在 REPL 和 Node 10.13.0 中都能正常工作:

const {map, pipe, split, lensPath, view} = ramda  

const makeLenses = map ( map ( pipe ( split ('/'), lensPath )))

const applyLensSpec = (lensSpec) => (obj) => 
  map ( map ( f => view (f, obj) ), lensSpec)

const spec = {command: {name: "name", description: "description", alias: "alias", arguments: "_children/Arguments"}, arguments: {name: "name", alias: "alias", optional: "optional", description: "description"}};

const myTransform = applyLensSpec(
  makeLenses(spec),
)

const testObj =   {
  name: 'foo', 
  alias: 'bar', 
  description: 'baz', 
  optional: false, 
  _children: {
    Arguments: ['qux', 'corge']
  }
}

console .log (
  myTransform (testObj)
)
<script src="https://bundle.run/ramda@0.26.1"></script>

关于javascript - 如何解决 Ramda reduce 函数的错误返回结果 (javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56396101/

相关文章:

javascript - Vue.js:子组件如何改变父组件的状态?

javascript - Universal Analytics 无法跟踪 magento 中的交易

javascript - jquery 中的元素无法读取 css 文件

Javascript:按日期字符串属性对对象列表进行排序,其中日期可以是未定义的

javascript - 在 JavaScript 中仅使用 `.reduce()` 交换数组中的 2 个元素?

functional-programming - 函数式编程的正确注释

oop - OOP 接口(interface)和 FP 类型类的区别

java - Java 8 中 Haskell 的 foldr 等价物

map - 运行时异常 : java. lang.NoSuchMethodException : tfidf$Reduce. <init>()

javascript - JavaScript 中 reduceRight 的原生实现是错误的