RAA-stream implicit iterator maybe offbyone ?

Discussion in 'Ruby' started by Simon Strandgaard, Sep 14, 2003.

  1. 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
     
    Simon Strandgaard, Sep 14, 2003
    #1
    1. Advertising

  2. Simon Strandgaard

    Horst Guest

    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
     
    Horst, Sep 17, 2003
    #2
    1. Advertising

  3. 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
     
    Simon Strandgaard, Sep 17, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest
    Replies:
    5
    Views:
    618
  2. Ville Vainio
    Replies:
    11
    Views:
    612
    Hamish Lawson
    Aug 10, 2004
  3. bb
    Replies:
    3
    Views:
    1,702
    peter koch
    Apr 30, 2007
  4. Donglai Gong

    where to put private/raa libraries?

    Donglai Gong, Jun 27, 2003, in forum: Ruby
    Replies:
    1
    Views:
    114
    Bermejo, Rodrigo
    Jun 27, 2003
  5. Derek Basch
    Replies:
    8
    Views:
    134
    Ben Morrow
    Aug 12, 2006
Loading...

Share This Page