[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

What about a 'series' type?

Peter Marsh

6/7/2007 11:01:00 AM

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-....

3 Answers

Robert Dober

6/7/2007 11:30:00 AM

0

On 6/7/07, Peter Marsh <evil_grunger@hotmail.com> wrote:

Honestly I do not believe that the core is the place to put such
things, furthermore I believe it is too specific a feature, a more
general approach might have better chances to be fit for the core; Yet
I do not think what follows is fit for the core either, but maybe you
find it interesting or helpful:

class Lazy
def initialize init, op, *args
@init = init
@op = op
@args = args.dup
end

def upto value
return [] if value < 1
(2..value).inject([@init]){ |acc,| acc << acc.last.send( @op, *@args ) }
end
end # class Lazy

505/6 > irb -r lazy.rb
irb(main):001:0> l = Lazy.new 1, :+, 2
=> #<Lazy:0xb7ddfa60 @args=[2], @init=1, @op=:+>
irb(main):002:0> l.upto 5
=> [1, 3, 5, 7, 9]
irb(main):003:0> m = Lazy.new 2, :*, 3
=> #<Lazy:0xb7dd4908 @args=[3], @init=2, @op=:*>
irb(main):004:0> m.upto 4
=> [2, 6, 18, 54]
irb(main):005:0>

Cheers
Robert

P.S. Implementations without #inject are theoretically possible ;)
R.
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw

ara.t.howard

6/7/2007 1:42:00 PM

0


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-....
>

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




Robert Dober

6/8/2007 8:07:00 PM

0

On 6/8/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> On 08.06.2007 14:27, Robert Dober wrote:
> > On 6/8/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >> On 07.06.2007 23:12, Robert Dober wrote:
> >> > On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
> >> >> On 07.06.2007 21:41, Robert Dober wrote:
> >> >> > On 6/7/07, Robert Klemme <shortcutter@googlemail.com> wrote:
That's a dialog, right, I wonder if OP lost interest or did I just
hitchhike another thread?

let us see what I came up with under your tutorship.

require 'test/unit'
class Serial

def initialize(*init, &f)
@init = init
@f = f
@arity = f.arity
end

def get_some( some = nil, &b)
current = @init.dup
if some && b.nil? then
r = @init.dup
(some - r.size).times do
current = compute_next( current )
r << current.last
end
r
else
a = b.arity
loop do
break if some && ( some -= 1 ) < 0
b[ *current.first( a ) ]
current = compute_next( current )
end
self
end
end

private
def compute_next current
(current + Array( @f[*current] ) ).last @arity
end
end
if __FILE__ == $0 then

class Testee < Test::Unit::TestCase
def test_1
s1 = Serial.new 0 do |x| x+1 end
assert_equal [*0..3], s1.get_some( 4 )
a = []
s1.get_some{ |x| a << x; break if x > 10 }
assert_equal [*0..11], a
end # def test_1

def test_2
f = Serial.new 1, 1 do |x, y| x + y end
assert_equal [1,1,2,3,5,8], f.get_some(6)
a = []
f.get_some{ |x| a << x; break if x > 10 }
assert_equal [1,1,2,3,5,8,13], a
a = []
f.get_some(4){ |x| a.unshift x }
assert_equal [3,2,1,1], a
end # def test_2


def test_3
f = Serial.new 0, 0, 1 do |a, b, c| 3*a + 2*b + c end
assert_equal [0,0,1,1,3,8,17,42,100], f.get_some(9)
end # def test_3
end # class Testee < Test::Unit::TestCase
end

Thanks again

Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw