这个Ramda.js数据转换可以改进吗?

问题描述 投票:3回答:2

我有一个商店列表和一个id列表:

var stores = [{id: '1', name: 'first store'}, {id: '2', name: 'second store'}, {id: '3', name: 'third store'}];
var ids = ['1', '2'];

我想获取与列表中的ID匹配的商店名称:

["first store", "second store"]

这是我想出的:

var filterStoresById = R.intersectionWith(R.useWith(R.equals, R.view(R.lensProp('id'))));
var extractNames =  R.map(R.view(R.lensProp('name')));
extractNames(filterStoresById(stores,ids));

我的目标是学习函数式编程概念,同时生成我可以在现实生活中使用的代码,但我认为我的解决方案不具有可读性或性能(我正在迭代两次),所以可以对此进行哪些改进码?

javascript functional-programming ramda.js
2个回答
3
投票

你的代码并不可怕,只是可读性稍差。

首先,要访问顶级属性,尤其是当您不重用访问方法时,使用prop(name)比使用view(lensProp(name))要简单得多。 Ramda为pluckmap(prop)

其次,我发现通过功能组合构建的函数比通过嵌套的括号级别更容易阅读。我通常更喜欢pipecompose,但要么会这样做。

所以我会像这样修改你的代码:

var filterStoresById = R.intersectionWith(R.useWith(R.equals, R.prop('id')));
var storeNamesById = R.pipe(filterStoresById, R.pluck('name'));

storeNamesById(stores, ids);

这样做的一个优点是,如果您发现需要的不仅仅是名称,那么现在您可以使用可重复使用的filterStoresById函数。

另一个问题是表现。迭代两次肯定会受到惩罚。问题是,更清晰,更容易重构的代码是否值得。有一些技术可以将这样的代码转换成代码来执行相同的操作,但只迭代一次并避免使用中间容器。你可以看到these articles on transducers更多的信息。

但我会避免担心这个,除非你能向自己证明这实际上是你应用程序中的性能问题。我假设每个人都知道Knuth错误引用“过早优化是所有邪恶的根源”。


1
投票

innerJoin FTW

const stores = [
  {id: '1', name: 'first store'}, 
  {id: '2', name: 'second store'}, 
  {id: '3', name: 'third store'},
]

const ids = ['1', '2']

const f = 
    R.pipe(R.innerJoin(R.flip(R.propEq('id'))), R.pluck('name'))

console.log(f(stores, ids))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
© www.soinside.com 2019 - 2024. All rights reserved.