From: Robert Dober Date: 2007-06-09T05:07:27+09:00 Subject: Re: What about a 'series' type? On 6/8/07, Robert Klemme wrote: > On 08.06.2007 14:27, Robert Dober wrote: > > On 6/8/07, Robert Klemme wrote: > >> On 07.06.2007 23:12, Robert Dober wrote: > >> > On 6/7/07, Robert Klemme wrote: > >> >> On 07.06.2007 21:41, Robert Dober wrote: > >> >> > On 6/7/07, Robert Klemme 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