[Python-ideas] Why not Ruby -- and what else
spir
denis.spir at free.fr
Wed Apr 15 11:50:37 CEST 2009
I have just found the following from http://en.wikipedia.org/wiki/Iterator#Implicit_iterators
===================================
Some object-oriented languages such as Perl, Python, C#, Ruby and later versions of Java and Delphi provide an intrinsic way of iterating through the elements of a container object without the introduction of an explicit iterator object. An actual iterator object may exist in reality, but if it does it is not exposed within the source code of the language.
Implicit iterators are often manifested by a "foreach" statement (or equivalent), such as in the following Python example:
for value in iterable:
print value
Or other times they may be created by the collection object itself, as in this Ruby example:
iterable.each do |value|
puts value
end
==================================
For any weird reason, Ruby's famous PLP (principle of least surprise) translates by me to PHC (principle of highest confusion). Like if adding colours, fonts, shapes, makes a better visual design.
Still, instead of Python's version, my favorite language would say:
(1) syntactic magic version
traverse container with item
console.write item
or
through container with item
console.write item
Which I find both more obvious and explicit.
(2) OO version (see Io language for worthful source of inspiration)
container.traverse(item,
console.write item
)
The latter case intends to semantically express the fact that 'traverse' (or 'through', or whatever) is a method that takes an item _name_ and a code block as parameters. Actually, if consistency really counts, it should read:
container.traverse("item",
console.write item
)
or even
container.traverse("item",
"console.write item"
)
Which is ugly, isn't it? The reason why I still prefere syntactic magic ;-) Or we could consider another possibility:
(3) postfix version
container traverse
console write
or
container.traverse
console.write
that often simply gets rid of function parameter names -- usually. What happens to be on the data stack will be written out -- in this case: each item provided by traverse. Another view on the same thing: 'traverse' is a higher order function that takes 'console write' as parameter and feeds it with data to write out.
Postfix notation is imo an endless source of elegant formulation -- as long as stack juggling can be avoided.
Denis
------
la vita e estrany
More information about the Python-ideas
mailing list