函数式编程:过滤然后映射的术语?
有时我发现自己想要过滤集合,然后映射结果。
例如,在 JavaScript 中:
var completedStepIds = steps.filter(function(step) {
return step.isComplete;
}).map(function(step) {
return step.id;
});
或者在 C#/LINQ 中:
var completedStepIds =
from step in steps
where step.IsComplete
select step.Id
;
在功能术语中是否有一个术语来表示这种先过滤后映射的组合?
Sometimes I find myself wanting to filter a collection, then map the results.
In JavaScript, for example:
var completedStepIds = steps.filter(function(step) {
return step.isComplete;
}).map(function(step) {
return step.id;
});
Or in C#/LINQ:
var completedStepIds =
from step in steps
where step.IsComplete
select step.Id
;
In there a term in functional parlance for this filter-then-map combination?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我猜你想要列表理解:
嗯,jQuery 的
map
是这样工作的,这是一个变相的列表理解:另一方面,Prototype 根本不过滤(这是正统的做法) ,映射不应减少):
我不知道您使用哪个库,但您始终可以编写自己的抽象,从映射中清除 null/undefined :
I guess you want list-comprehensions:
Well, jQuery's
map
works this way, which is a list-comprehension in disguise:On the other hand Prototype does not filter at all (which is the orthodox thing to do, a map should not reduce):
I don't know which library are you using, but you can always write your own abstraction which clears null/undefined from the mapping: