From: Gavin Sinclair Date: 2002-09-02T10:44:38+09:00 Subject: Re: Ruby aesthetics > > The question is whether it's essential enough for many people to have > it. I see it more as a convenience feature; it does not add much in > terms of essential functionality, as you say. The same could surely be said of many other features of the Ruby language. I'm not arguing that all forms of syntactic sugar should be included, nor even this one (which I've snipped). But aliases, attr_* etc. don't add any essential functionality, yet we love them. >> > In general, you do not want to expand the language when existing >> abstraction mechanisms can already capture the same thing adequately >> and with more room for customization. >> >> True, but what about getting all pairs of number less than 10 whose >> combination mulitplies to greater than 25? >> >> result = [[a,b] for a in (1...10) for b in (1...10) if a * b > 25] > > What you seem to be getting out of the list comprehension facility > mostly is the generation of pairs. A functionality that could arguably > be separate from a list comprehension facility. (Also, if the above is > to be legal Python code, it would be range(1, 10) instead of 1...10). It was meant to be a demo of how it could be written in Ruby. > We can try the following to enumerate pairs: > > module Enumerable > def pair_with(other) > result = [] > each do | item | > other.each do | item2 | > result << [item, item2] > end > end > result > end > end > > p((1...10).pair_with(1...10).select{|x, y| x * y > 25}) That looks ugly - I'm sure you agree. >> This construct hasn't had much popularity in this thread, but I think >> it would be a worthwhile addition to the language. It's simply a >> beautiful abstraction. (I know a little about functional programming, >> but cannot be said to have a background in it: my appreciation for >> list comprehensions is more mathematical.) > > It can be written in Ruby (or as a C extension), if you are so inclined. > > Enum.all(1...10, 1...10) {|x,y| [x,y] if x * y > 25} > Enum.all(numbers) {|x| x ** 2 if x < 10 } > Enum.all(1..3, 1..4) > > Below is a quick & dirty implementation in Ruby (the module name "Enum" > and the method name "all" probably aren't good choices either, but this > is just proof of concept). > > Reimer Behrends That is a very nice implementation indeed. It is almost as terse and readable as list comprehensions, and a very nice use of abstraction. If I need to use such a construct, I'll implement this in anger, and release it on RAA. BTW I think the names are fine. Enum captures the meaning, though could be confused with C "enum"s; perhaps List is better, although still using something too general for something too specific. The method "all" is great - reminiscent of the mathematical symbol upside-down-A, meaning "for all". [Implementation snipped.] Cheers, Gavin