Ruby中的map和collect之间的区别?

编程入门 行业动态 更新时间:2024-10-21 16:33:37
本文介绍了Ruby中的map和collect之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 c \\ c \\ c \\ c \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ Ruby / Rails中的数组中的$ c>?

文档似乎没有提出任何建议,但方法或性能可能有差异?

解决方案

没有区别,实际上映射在C中实现为 rb_ary_collect 和 enum_collect (例如,数组和任何其他枚举之间的 map 之间有区别,但是 map 和收集)。

code> map 和收集存在于Ruby? 地图函数具有不同语言的许多命名约定。 维基百科提供概述:

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

Ruby为Smalltalk世界的程序员提供了一个别名,可以在家里感受更多。

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

数组是最常见的集合,因此优化其性能是合理的。由于Ruby很了解数组的工作原理,因此不需要调用每个,而只能使用简单的指针操作,这显着更快。

存在一些类似的优化数组方法如 zip 或计数。

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?

解决方案

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

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

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 provides an alias for programmers from the Smalltalk world to feel more at home.

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中的map和collect之间的区别?

本文发布于:2023-11-26 00:20:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631914.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:区别   Ruby   map   collect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!