我几乎没有使用Python、Perl和Ruby等语言的经验,但我已经用Smalltalk开发过一段时间了。有一些非常基本的Smalltalk类,它们非常流行并且是跨Smalltalk实现的:
FileStream
ReadWriteStream
Set
Dictionary
OrderedCollection
SortedCollection
Bag
Interval
Array在Python、Perl和Ruby中,哪些类是等价的或有效的语义替代品?我发现了几个比较语法的语言比较页面,但是对于核心库和基础库的翻译似乎没有什么帮助。
我还想知道在Python、Perl或Ruby中是否存在Smalltalk或反之亦然的基类或核心类?
发布于 2011-08-25 10:08:06
Perl
我会回答Perl,因为我精通Perl和Smalltalk。
Smalltalk的Dictionary非常接近Perl的散列类型。字典对键使用对象等效项。Perl使用简单的字符串作为键,因此灵活性受到一定的限制。
Smalltalk的OrderedCollection与Perl的数组类型非常接近。
Smalltalk的FileStream有点像Perl的文件句柄,因为它们表示到外部文件或设备的数据流。
仅此而已,因为Perl只有散列、数组和文件句柄。:)
发布于 2011-08-25 12:18:08
红宝石
FileStream -> File
ReadWriteStream -> IO (or other things that duck type like it)
Set -> require 'set', then use the Set class
Dictionary -> Hash
OrderedCollection -> Array
SortedCollection nothing similar
Bag nothing similar
Interval -> Range
Array Ruby has no fixed-length collection class.发布于 2011-08-25 10:20:49
Python
FileStream -> file
ReadWriteStream -> file
Set -> set
Dictionary -> dict
OrderedCollection -> list
SortedCollection -> no equivalent object (must call sort on a list)
Bag -> no equivalent object (must implement using dict)
Interval -> no equivalent object (but a range() function exists for making lists)
Array -> no equivalent (tuple is read-only, fixed length. list is variable length)我需要注意的是,Python2.7中有一个collections.Counter对象,它等同于Bag。
https://stackoverflow.com/questions/7184240
复制相似问题