【发布时间】:2019-08-24 07:56:29
【问题描述】:
我正在尝试将多个对象与 Sanctuary 合并。
使用 Ramda.js,我会做这样的事情(参见 REPL here):
const R = require('ramda');
const initialEntry = { a: 0, b: 1 };
const entries = [{c: 2}, {d: 3, e: 4}, {f: 5, g: 6, h: 7}];
R.reduce(Object.assign, initialEntry, entries);
但是,对于 Santuary.js,以下代码行会引发异常。
S.reduce(Object.assign)(initialEntry)(entries)
这是我得到的例外:
! Invalid value
reduce :: Foldable f => (a -> b -> a) -> a -> f b -> a
^^^^^^
1
1) {"a": 0, "b": 1} :: Object, StrMap Number, StrMap FiniteNumber, StrMap Integer, StrMap NonNegativeInteger, StrMap ValidNumber
The value at position 1 is not a member of ‘b -> a’.
我对这个错误信息感到困惑。我是否错误地使用了S.reduce?另外,如果我写S.reduce(Object.assign)(initialEntry)([]),我不会收到任何错误。
【问题讨论】:
-
如果它有助于在 Sanctuary 中找到类似的功能;我会在 Ramda 中使用
mergeAll来合并对象列表。 -
... 和
unapply(mergeAll)用于更接近Object.assign的可变参数版本。 -
或者可能是ˋunapply(Object.assign)ˋ?
标签: javascript functional-programming ramda.js sanctuary