From: Joel VanderWerf Date: 2002-09-05T13:55:28+09:00 Subject: Re: Ruby aesthetics Gavin Sinclair wrote: >>Gavin Sinclair wrote: >>... >> >>>My frame-of-reference for good language design is Ruby. So compared >>>to Ruby, I would add: >>> - list comprehensions >>> - calling procs would look like real functions, not arrays! >>> e.g. id = proc { |x| x }; id(12) # not id[12] >> >>This bothered me a little, OUAT (once upon a time :), but not now. I >>like being able to use procs and hashes interchangeably. This makes it >>possible to think of a hash as a function in the sense of a set of >>ordered pairs: >> >> succ_mod_3 = {0=>1, 1=>2, 2=>0} >> succ_integer = proc {|x| x+1} >> >> def cycle(succ_map) >> x = 0 >> 10.times do >> print x >> x = succ_map[x] >> end >> end >> >> cycle succ_mod_3 # ==> 0120120120 >> cycle succ_integer # ==> 0123456789 > > > Hey, nice! I like that. Although using an iterator would be more > Ruby-ish and flexible, wouldn't it? Besides, we've seen Perl and Python > make a real hash of hashes (sorry...) Okay, with iterator support: def cycle(succ_map=nil, &block) succ_map ||= block x = 0 10.times do print x x = succ_map[x] end end cycle do |x| x-1 end # ==> 0-1-2-3-4-5-6-7-8-9 ...and the hash and proc arguments still work as well. >>If you want to use procs and methods interchangeably, you have a little >>more work, but not much--just apply #method. > > > Correct, but (please excuse my possibly ignorance and definite > inexperience) I think it's a bit messy. I just can't see the uniformity. > It'll come in time... and in the meanwhile, it doesn't get in my way. How's this: def add_5 x x + 5 end cycle method(:add_5) # ==> 051015202530354045 (You could even change #cycle to check if its first arg is a symbol...) The really hoopy thing about dynamic typing is that all we assume about the succ_map is that it supports #[], so this snippet will work with, say, PStore, or a database class that has #[].