Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > RAA-stream implicit iterator maybe offbyone ?

Reply
Thread Tools

RAA-stream implicit iterator maybe offbyone ?

 
 
Simon Strandgaard
Guest
Posts: n/a
 
      09-14-2003
I am testing the 'stream' module..
http://raa.ruby-lang.org/list.rhtml?name=stream


Q1: I have to overload the implicitstream.at_beginning_proc
with a offbyone value.. Why ?

Q2: Is this me which is thinking wrong, or is it a design flaw
in the 'stream' library?


I have made two methods which should return virtually the
same stream; [1, 2, 3, 4, 5, 6]




server> expand -t2 test_misc.rb
require 'test/unit'
require 'stream'

class TestMisc < Test::Unit::TestCase
def mk_str16
(1..6).create_stream
end
def mk_str16x
Stream::ImplicitStream.new{|s|
x = 1
s.at_end_proc = proc {x == 6}
s.forward_proc = proc {x += 1}
s.at_beginning_proc = proc {x == 0} # todo: why is'nt it {x == 1}
s.backward_proc = proc {x -= 1}
}
end
def setup
@data = mk_str16.filtered{|x| x % 2 == 0}
@data += mk_str16x.filtered{|x| x % 2 != 0}
end
def test_toa
assert_equal([2, 4, 6, 1, 3, 5], @data.to_a)
end
def test_revtoa
assert_equal([5, 3, 1, 6, 4, 2], @data.reverse.to_a)
end
end

if $0 == __FILE__
require 'test/unit/ui/console/testrunner'
Test::Unit::UI::Console::TestRunner.run(TestMisc)
end
server> ruby test_misc.rb
Loaded suite TestMisc
Started
...
Finished in 0.008114 seconds.

2 tests, 2 assertions, 0 failures, 0 errors
server>



If I change the 'at_beginning_proc' to stop at '1' which I believe
is where it should stop, then I get this error:


server> ruby test_misc.rb
Loaded suite TestMisc
Started
FF
Finished in 0.009856 seconds.

1) Failure!!!
test_revtoa(TestMisc) [test_misc.rb:25]:
<[5, 3, 1, 6, 4, 2]> expected but was
<[5, 3, 6, 4, 2]>

2) Failure!!!
test_toa(TestMisc) [test_misc.rb:22]:
<[2, 4, 6, 1, 3, 5]> expected but was
<[2, 4, 6, 3, 5]>

2 tests, 2 assertions, 2 failures, 0 errors
server>


--
Simon Strandgaard
 
Reply With Quote
 
 
 
 
Horst
Guest
Posts: n/a
 
      09-17-2003
Simon Strandgaard wrote:

> I am testing the 'stream' module..
> http://raa.ruby-lang.org/list.rhtml?name=stream
>
>
> Q1: I have to overload the implicitstream.at_beginning_proc
> with a offbyone value.. Why ?
>

Because the stream is at beginning if the next forward should give the first
element in the stream which is 1 in the test case below. Thats why the
initial value of the variable x should be zero and the at_beginning_proc
should test this.

Here ist my version of the stream:

# a stream which is aquivalent (1..5).create_stream
def new_implicit_stream
s = Stream::ImplicitStream.new { |s|
x = 0
s.at_beginning_proc = proc {x < 1}
s.at_end_proc = proc {x == 5}
s.forward_proc = proc {x += 1 }
s.backward_proc = proc {|y| y = x; x -= 1; y }
}
end

This version passes all tests that the collection stream passes. See tests
TestStream.rb

> Q2: Is this me which is thinking wrong, or is it a design flaw
> in the 'stream' library?
>

When designing the library I also had to think more about this. Have you
read the stream-doku contained in the RGL-Library which also includes the
stream module? I regret that it is not included in tarball of the stream
module. See http://rgl.sourceforge.net/files/stream_rb.html

Cheers
Horst


>
> I have made two methods which should return virtually the
> same stream; [1, 2, 3, 4, 5, 6]
>
>
>
>
> server> expand -t2 test_misc.rb
> require 'test/unit'
> require 'stream'
>
> class TestMisc < Test::Unit::TestCase
> def mk_str16
> (1..6).create_stream
> end
> def mk_str16x
> Stream::ImplicitStream.new{|s|
> x = 1
> s.at_end_proc = proc {x == 6}
> s.forward_proc = proc {x += 1}
> s.at_beginning_proc = proc {x == 0} # todo: why is'nt it {x == 1}
> s.backward_proc = proc {x -= 1}
> }
> end
> def setup
> @data = mk_str16.filtered{|x| x % 2 == 0}
> @data += mk_str16x.filtered{|x| x % 2 != 0}
> end
> def test_toa
> assert_equal([2, 4, 6, 1, 3, 5], @data.to_a)
> end
> def test_revtoa
> assert_equal([5, 3, 1, 6, 4, 2], @data.reverse.to_a)
> end
> end
>
> if $0 == __FILE__
> require 'test/unit/ui/console/testrunner'
> Test::Unit::UI::Console::TestRunner.run(TestMisc)
> end
> server> ruby test_misc.rb
> Loaded suite TestMisc
> Started
> ..
> Finished in 0.008114 seconds.
>
> 2 tests, 2 assertions, 0 failures, 0 errors
> server>
>
>
>
> If I change the 'at_beginning_proc' to stop at '1' which I believe
> is where it should stop, then I get this error:
>
>
> server> ruby test_misc.rb
> Loaded suite TestMisc
> Started
> FF
> Finished in 0.009856 seconds.
>
> 1) Failure!!!
> test_revtoa(TestMisc) [test_misc.rb:25]:
> <[5, 3, 1, 6, 4, 2]> expected but was
> <[5, 3, 6, 4, 2]>
>
> 2) Failure!!!
> test_toa(TestMisc) [test_misc.rb:22]:
> <[2, 4, 6, 1, 3, 5]> expected but was
> <[2, 4, 6, 3, 5]>
>
> 2 tests, 2 assertions, 2 failures, 0 errors
> server>
>
>
> --
> Simon Strandgaard


 
Reply With Quote
 
 
 
 
Simon Strandgaard
Guest
Posts: n/a
 
      09-17-2003
On Wed, 17 Sep 2003 03:32:13 +0200, Horst wrote:

> Simon Strandgaard wrote:
>
>> I am testing the 'stream' module..
>> http://raa.ruby-lang.org/list.rhtml?name=stream
>>
>>
>> Q1: I have to overload the implicitstream.at_beginning_proc
>> with a offbyone value.. Why ?
>>

> Because the stream is at beginning if the next forward should give the first
> element in the stream which is 1 in the test case below. Thats why the
> initial value of the variable x should be zero and the at_beginning_proc
> should test this.


OK, I were just wondering why. Now I see

[snip]


>> Q2: Is this me which is thinking wrong, or is it a design flaw
>> in the 'stream' library?
>>

> When designing the library I also had to think more about this. Have you
> read the stream-doku contained in the RGL-Library which also includes the
> stream module? I regret that it is not included in tarball of the stream
> module. See http://rgl.sourceforge.net/files/stream_rb.html


Perhaps you should add an example of a backward implicit stream, because it
is sligthly different from forward implicit streams ?


--
Simon Strandgaard



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
OffByOne Web Browser? Heidi Manway Computer Support 5 12-12-2006 04:25 PM
OffByOne Web Browser? Heidi Manway Computer Support 1 12-12-2006 04:11 PM
OffByOne Web Browser? Heidi Manway Computer Support 0 12-12-2006 04:01 PM
OffByOne Web Browser? Heidi Manway Computer Support 0 12-12-2006 04:01 PM
OffByOne Web Browser? Heidi Manway Computer Support 0 12-12-2006 04:01 PM



Advertisments