Collections and Stream classes equivalences between Smalltalk, Perl, Python and Ruby
I have few experience with languages like Python, Perl and Ruby, but I have developed in Smalltalk from some time. There are some pretty basic Smalltalk classes which are very popular and cross-Smalltalk implementation:
FileStream
ReadWriteStream
Set
Dictionary
Ordere开发者_Go百科dCollection
SortedCollection
Bag
Interval
Array
Which classes would be the equivalent or valid semantic replacements in Python, Perl and Ruby? I've found several language comparison pages comparing syntax, however it seems there is little help when comes to the translation of core and base libraries.
I also wonder if there is a base or core class in Python, Perl or Ruby which is not present in Smalltalk or viceversa?
Perl
I'll answer for Perl, since I'm fluent in both Perl and Smalltalk.
Smalltalk's Dictionary is fairly close to Perl's hash type. A Dictionary uses object equivalence for the keys. Perl uses simple strings for keys, so the flexibility is somewhat limited.
Smalltalk's OrderedCollection is fairly close to Perl's array type.
Smalltalk's FileStream is somewhat like Perl's filehandles, in the sense that they represent a stream of data to an external file or device.
And that's about it, since Perl has only hashes and arrays and filehandles. :)
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.
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)
I should note, that there is a collections.Counter object for Python 2.7 that is equivalent to Bag.
精彩评论