地图之间的差异与Ruby的收集? [英] Difference between map and collect in Ruby?

查看:147
本文介绍了地图之间的差异与Ruby的收集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Google搜索这一点,并得到了片状/矛盾的意见 - 是否有真正做一个地图和做之间的收集中的Ruby / Rails的阵列上?

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?

推荐答案

有什么区别,其实地图用C实现为 rb_ary_collect enum_collect (如上有一个数组,任何其他地图之间的差异枚举,但没有什么区别地图收集)。

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).

在Ruby中为什么这两个地图收集存在吗?地图函数在不同的语言许多命名约定。 维基百科提供了一个概述

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

地图功能起源于函数式编程语言,但今天支持(或可被定义)面向许多程序,对象,多范型的语言,以及:在C ++的标准模板库,它被称为变换,在C#(3.0)的LINQ库,它是作为一种称为扩展方法选择。地图也是高级语言如Perl,Python和Ruby经常使用的操作;该操作在所有这三种语言的名为地图。的 A 收集在地图别名也在红宝石(从Smalltalk中)提供的的[重点煤矿。 Common Lisp中提供了一系列地图样的功能;对应于这里所描述的行为之一(使用汽车的操作说明-car访问)名为 mapcar

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.

为什么会出现不同的实现对于数组和枚举?枚举是一个广义的迭代结构,这意味着没有办法在红宝石可以predict下一个元素可以是什么(你可以定义无限的枚举,请参阅总理一个例子)。因此,它必须调用一个函数来获取每一个连续的元素(通常这将是每个方法)。

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).

阵列是最常见的集合,以便它是合理的优化其性能。由于红宝石知道很多关于它是如何工作的数组没有叫每个,但只能用简单的指针操作这是显著更快。

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.

类似的优化为一些阵列的方法,如存在拉链计数

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

这篇关于地图之间的差异与Ruby的收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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