集合和流类在Smalltalk、Perl、Python和Ruby之间的等价性

2024-09-30 03:24:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python、Perl和Ruby这样的语言没有什么经验,但我已经用Smalltalk开发了一段时间。有一些非常基本的Smalltalk类非常受欢迎,它们是跨Smalltalk实现的:

FileStream
ReadWriteStream
Set
Dictionary
OrderedCollection
SortedCollection
Bag
Interval
Array

在Python、Perl和Ruby中,哪些类是等价的或有效的语义替换?我发现有几个语言比较页面比较了语法,但是对于核心库和基本库的翻译似乎没有什么帮助。在

我还想知道,在Python、Perl或Ruby中,是否存在Smalltalk或viceversa中没有的基类或核心类?在


Tags: 语言核心dictionary经验arrayperlbagruby
3条回答

Perl公司

我会用Perl,因为我对Perl和Smalltalk都很流利。在

Smalltalk的字典非常接近Perl的散列类型。字典对键使用对象等价。Perl使用简单的字符串作为键,因此灵活性受到一定限制。在

Smalltalk的OrderedCollection非常接近Perl的数组类型。在

Smalltalk的FileStream有点像Perl的filehandles,从某种意义上说,它们代表到外部文件或设备的数据流。在

就这样,因为Perl只有散列、数组和文件句柄。:)

红宝石

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.

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)

我应该注意到,有一个集合。计数器对象,它相当于Bag。在

相关问题 更多 >

    热门问题