Ruby 中的 map 和 collect 之间的区别? [英] Difference between map and collect in Ruby?

查看:33
本文介绍了Ruby 中的 map 和 collect 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在谷歌上搜索了这个并得到了不完整/矛盾的意见 - 在 Ruby/Rails 中对数组执行 map 和执行 collect 之间实际上有什么区别吗?

I have Googled this and got patchy / contradictory opinions - is there actually any difference between doing a map and doing a collect on an array in Ruby/Rails?

文档 似乎没有任何建议,但在方法或性能上可能存在差异吗?

The docs don't seem to suggest any, but are there perhaps differences in method or performance?

推荐答案

没有区别,实际上 map 在 C 中实现为 rb_ary_collectenum_collect(例如,数组和任何其他枚举上的 map 之间有区别,但 mapcollect 之间没有区别).

There's no difference, in fact map is implemented in C as rb_ary_collect and enum_collect (eg. there is a difference between map on an array and on any other enum, but no difference between map and collect).

为什么 mapcollect 都存在于 Ruby 中? map 函数有很多不同的命名约定语言.维基百科提供了概述:

Why do both map and collect exist in Ruby? The map function has many naming conventions in different languages. Wikipedia provides an overview:

map 函数起源于函数式编程语言,但如今在许多过程、面向对象和多范式语言中也得到支持(或可能被定义):在 C++ 的标准模板库中,它被称为 transform,在 C# (3.0) 的 LINQ 库中,它作为一个名为 Select 的扩展方法提供.Map 也是 Perl、Python 和 Ruby 等高级语言中经常使用的操作;该操作在所有这三种语言中都称为 map.Ruby 还提供了一个 collect 别名(来自 Smalltalk) [强调我的].Common Lisp 提供了一系列类似地图的函数;与这里描述的行为相对应的那个称为 mapcar(-car 表示使用 CAR 操作访问).

The map function originated in functional programming languages but is today supported (or may be defined) in many procedural, object oriented, and multi-paradigm languages as well: In C++'s Standard Template Library, it is called transform, in C# (3.0)'s LINQ library, it is provided as an extension method called Select. Map is also a frequently used operation in high level languages such as Perl, Python and Ruby; the operation is called map in all three of these languages. A collect alias for map is also provided in Ruby (from Smalltalk) [emphasis mine]. Common Lisp provides a family of map-like functions; the one corresponding to the behavior described here is called mapcar (-car indicating access using the CAR operation).

Ruby 为来自 Smalltalk 世界的程序员提供了一个别名,让他们感觉更自在.

Ruby provides an alias for programmers from the Smalltalk world to feel more at home.

为什么数组和枚举有不同的实现?枚举是一种通用的迭代结构,这意味着 Ruby 无法预测下一个元素是什么(你可以定义无限枚举,参见 Prime 示例).因此它必须调用一个函数来获取每个连续的元素(通常这将是 each 方法).

Why is there a different implementation for arrays and enums? An enum is a generalized iteration structure, which means that there is no way in which Ruby can predict what the next element can be (you can define infinite enums, see Prime for an example). Therefore it must call a function to get each successive element (typically this will be the each method).

数组是最常见的集合,因此优化它们的性能是合理的.由于 Ruby 非常了解数组的工作原理,因此不必调用 each 而只能使用简单的 指针操作,明显更快.

Arrays are the most common collection so it is reasonable to optimize their performance. Since Ruby knows a lot about how arrays work it doesn't have to call each but can only use simple pointer manipulation which is significantly faster.

对于zipcount 等许多数组方法存在类似的优化.

Similar optimizations exist for a number of Array methods like zip or count.

这篇关于Ruby 中的 map 和 collect 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆