将 ramda 转换为普通 js
Convert ramda to normal js
我在 ramda 中有一个遗留代码,我很难弄清楚它的作用。如何将 ramda 函数转换为简单的 javascript?
我的功能如下:
R.forEach(
R.compose(
vm.importAll,
b =>
R.compose(
R.map(R.merge(R.pick(["index"])(b))),
R.prop("transactions")
)(b)
)
)
代码创建了一个函数,它将下面的数据结构作为输入-
[ { index: 123
, transactions: [ { ... }, ... ]
}
, { index: 456
, transactions: [ { ... }, ... ]
}
, ...
]
对于输入数组中的每个对象,o
,它构建一个对象的交易数组,o.transactions
,添加对象的索引,o.index
到每个新的交易对象.
中间状态是这样的,其中...
代表单个交易数据-
[ { index: 123, ... }, { index: 123, ... }, { index: 123, ... }, ... ]
这个数组传递给vm.importAll
,即-
vm.importAll([ { index: 123, ... }, ... ])
对输入数组中的每个对象重复该过程 -
vm.importAll([ { index: 456, ... }, ... ])
vm.importAll([ ... ])
享受编写非 Ramda 版本的乐趣!
user633183 该代码的用途。对我而言,这段代码表明了对 Ramda 用途的误解。它以一种散漫的方式将 Ramda 技术与其他技术混合在一起。
我最初的 vanilla JS 重写看起来像这样:
const submitTransactions = (xs) => xs .forEach (
({transactions, index}) =>
vm .importAll (transactions .map (t => ({...t, index}) ))
)
const input = [
{index: 42, transactions: [{foo: 'bar'}, {foo: 'baz'}]},
{index: 51, transactions: [{foo: 'qux'}, {foo: 'corge'}]}
]
const vm = { // dummy -- I have no idea what this actually does
importAll: (xs) => console .log (`importing all of ${JSON .stringify (xs)}`)
}
submitTransactions (input)
但我还是不喜欢这样。我宁愿将 vm.importAll
的副作用进一步推向我系统的边缘,所以我更喜欢这个版本:
const transformTransactions = (xs) => xs .map (
({transactions, index}) =>
transactions .map (t => ({...t, index}) )
)
const input = [
{index: 42, transactions: [{foo: 'bar'}, {foo: 'baz'}]},
{index: 51, transactions: [{foo: 'qux'}, {foo: 'corge'}]}
]
const vm = { // dummy -- I have no idea what this actually does
importAll: (xs) => console .log (`importing all of ${JSON .stringify (xs)}`)
}
transformTransactions (input) .forEach (vm .importAll)
这是标准的 ES6 代码。不知道是不是符合你的"normal"或者"simple".
的标准
你可以用Ramda函数重写,而且比原来显示的更简单,但我认为它不会像原来那么简单,所以似乎没有必要。一个这样的版本可能如下所示:
const {map, lift, compose, merge, pick, prop} = R
const transformTransactions = map (
lift (map) (
compose (merge, pick (['index']) ),
prop ('transactions')
)
)
虽然这并不可怕,但与以前的版本相比并没有明显的优势。
最后,根据 vm.importAll
的工作方式,使用所有数据对它进行一次调用可能比此处隐含的多次调用更好。只需更改为 flatMap
即可实现:
const submitTransactions = (xs) => xs .flatMap (
({transactions, index}) =>
transactions .map (t => ({...t, index}) )
)
vm .importAll (submitTransactions (input))
我是 Ramda 的创始人之一,也是它的重度用户。但它有一个特定的目的:使一些函数式编程技术在 Javascript 中更容易获得。老实说,我希望我们永远不要包括 .forEach
,因为它使得使用 Ramda 来做它不是为它设计的目的变得太容易了。
这也是我第一次听说 "legacy" 与 Ramda 代码有关。我想要么是图书馆,要么是我开始变老了! :-)
我在 ramda 中有一个遗留代码,我很难弄清楚它的作用。如何将 ramda 函数转换为简单的 javascript? 我的功能如下:
R.forEach(
R.compose(
vm.importAll,
b =>
R.compose(
R.map(R.merge(R.pick(["index"])(b))),
R.prop("transactions")
)(b)
)
)
代码创建了一个函数,它将下面的数据结构作为输入-
[ { index: 123
, transactions: [ { ... }, ... ]
}
, { index: 456
, transactions: [ { ... }, ... ]
}
, ...
]
对于输入数组中的每个对象,o
,它构建一个对象的交易数组,o.transactions
,添加对象的索引,o.index
到每个新的交易对象.
中间状态是这样的,其中...
代表单个交易数据-
[ { index: 123, ... }, { index: 123, ... }, { index: 123, ... }, ... ]
这个数组传递给vm.importAll
,即-
vm.importAll([ { index: 123, ... }, ... ])
对输入数组中的每个对象重复该过程 -
vm.importAll([ { index: 456, ... }, ... ])
vm.importAll([ ... ])
享受编写非 Ramda 版本的乐趣!
user633183
我最初的 vanilla JS 重写看起来像这样:
const submitTransactions = (xs) => xs .forEach (
({transactions, index}) =>
vm .importAll (transactions .map (t => ({...t, index}) ))
)
const input = [
{index: 42, transactions: [{foo: 'bar'}, {foo: 'baz'}]},
{index: 51, transactions: [{foo: 'qux'}, {foo: 'corge'}]}
]
const vm = { // dummy -- I have no idea what this actually does
importAll: (xs) => console .log (`importing all of ${JSON .stringify (xs)}`)
}
submitTransactions (input)
但我还是不喜欢这样。我宁愿将 vm.importAll
的副作用进一步推向我系统的边缘,所以我更喜欢这个版本:
const transformTransactions = (xs) => xs .map (
({transactions, index}) =>
transactions .map (t => ({...t, index}) )
)
const input = [
{index: 42, transactions: [{foo: 'bar'}, {foo: 'baz'}]},
{index: 51, transactions: [{foo: 'qux'}, {foo: 'corge'}]}
]
const vm = { // dummy -- I have no idea what this actually does
importAll: (xs) => console .log (`importing all of ${JSON .stringify (xs)}`)
}
transformTransactions (input) .forEach (vm .importAll)
这是标准的 ES6 代码。不知道是不是符合你的"normal"或者"simple".
的标准你可以用Ramda函数重写,而且比原来显示的更简单,但我认为它不会像原来那么简单,所以似乎没有必要。一个这样的版本可能如下所示:
const {map, lift, compose, merge, pick, prop} = R
const transformTransactions = map (
lift (map) (
compose (merge, pick (['index']) ),
prop ('transactions')
)
)
虽然这并不可怕,但与以前的版本相比并没有明显的优势。
最后,根据 vm.importAll
的工作方式,使用所有数据对它进行一次调用可能比此处隐含的多次调用更好。只需更改为 flatMap
即可实现:
const submitTransactions = (xs) => xs .flatMap (
({transactions, index}) =>
transactions .map (t => ({...t, index}) )
)
vm .importAll (submitTransactions (input))
我是 Ramda 的创始人之一,也是它的重度用户。但它有一个特定的目的:使一些函数式编程技术在 Javascript 中更容易获得。老实说,我希望我们永远不要包括 .forEach
,因为它使得使用 Ramda 来做它不是为它设计的目的变得太容易了。
这也是我第一次听说 "legacy" 与 Ramda 代码有关。我想要么是图书馆,要么是我开始变老了! :-)