On Jun 7, 2007, at 5:00 AM, Peter Marsh wrote:

> I'm sure everyone is fimilar with ranges:
>
> (1..10).to_a = [1,2,3,4,5,6,7,8,9,10] (from manual)
>
> But this isn't any good if you want a arithmetic [3,5,7,9] or  
> geometric
> [2,6,18,54] series. So I suggest this:
>
>
> arithmetic_series = Arith.new(First_Term,Common_Difference)
>
> aritmetic_series[9] will then generate the 10th term of the  
> sequence (0
> being the first to be consistent) and arithmetic_series[0..9] will
> return an array with the first to tenth terms. The same goes for a
> geometric series.
>
> gemometric_series = Geometric.new(First_Term,Common_Ratio)
>
>
> Now, there may be other ways to achieve this, but I think this way is
> nicer.
>
> -- 
> Posted via http://www.ruby-forum.com/.
>

it can be done with lambdas in ruby on a case by case basis:

cfp:~ > cat a.rb
arithmetic = lambda do |i|
   if i.respond_to? :map
     i.map{|j| arithmetic[j]}
   else
     i >= 1 ? (2 + arithmetic[i-1]) : 1
   end
end

p arithmetic[0]
p arithmetic[1]
p arithmetic[4]

p arithmetic[0..9]


cfp:~ > ruby a.rb
1
3
9
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

kind regards.

-a
--
we can deny everything, except that we have the possibility of being  
better. simply reflect on that.
h.h. the 14th dalai lama