Smalltalk、Perl、Python和Ruby的集合与流类对比

7 投票
3 回答
575 浏览
提问于 2025-04-17 00:18

我对Python、Perl和Ruby这些语言了解不多,但我已经在Smalltalk上开发了一段时间。Smalltalk里有一些非常基础的类,它们在不同的Smalltalk版本中都很流行:

FileStream
ReadWriteStream
Set
Dictionary
OrderedCollection
SortedCollection
Bag
Interval
Array

那么,在Python、Perl和Ruby中,哪些类可以和这些Smalltalk类对应,或者可以替代它们呢?我找到了一些语言比较的页面,主要是比较语法,但在核心和基础库的转换上似乎没有太多帮助。

我还想知道,Python、Perl或Ruby中是否有Smalltalk没有的基础类,或者反过来呢?

3 个回答

2

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)

我想提一下,Python 2.7 有一个叫做 collections.Counter 的对象,它的功能和 Bag 是一样的。

4

Ruby

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

Perl

我来讲讲Perl,因为我对Perl和Smalltalk都很熟悉。

Smalltalk里的字典(Dictionary)和Perl里的哈希(hash)类型挺像的。字典用对象的相等性来作为键,而Perl则是用简单的字符串作为键,所以在灵活性上稍微有限一些。

Smalltalk的有序集合(OrderedCollection)和Perl的数组(array)类型也很相似。

Smalltalk的文件流(FileStream)有点像Perl的文件句柄(filehandles),因为它们都代表着与外部文件或设备的数据流。

就这些了,因为Perl主要就只有哈希、数组和文件句柄这几种东西。:)

撰写回答