Super-iterator?

I like it.

What about a ‘multilpe_of?’ function. I hope this makes sense:

You could have a multiple_of? function so you could say multiple_of?(3)
and it would give you every third iteration. You could also add another
parameter that would subtract back that number like multiple_of?(3,2)
would give you 1,4,7,10 etc. That would make even? be multiple_of?(2)
and odd? be multiple_of?(2,1)

def multiple_of?(multiple,back)
@count_ % multiple == back
end

···

From: “Hal E. Fulton” hal9000@hypermetrics.com
Date: Tue Aug 06, 2002 02:25:23 AM US/Eastern
To: ruby-talk@ruby-lang.org (ruby-talk ML)
Subject: Re: Super-iterator? (long)
Reply-To: ruby-talk@ruby-lang.org

----- Original Message -----
From: “David Alan Black” dblack@candle.superlink.net
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Monday, August 05, 2002 4:37 PM
Subject: Re: Super-iterator? (long)

Just for fun, I’ve converted your tests to Test::Unit format. This is
a drop-in replacement for everything after the module Enumerable
extensions.

Neat. I need to get into that habit.

A few comments:

  1. I’m still not sold on the whole thing :slight_smile: But it’s extremely
    interesting.

Oh, neither am I (completely sold). It’s an experiment.
I’m sold on my own “motivations” but not on this solution.

  1. I had to change #next to #i_next, because it was next’ing integers
    and
    characters.

Huh?? next is a method of IteratorVar… it shouldn’t cause any
kind of conflict. Can you explain?

  1. #odd? and #even? are, in my view, too “meta”. When I see “x.odd?”,
    it’s virtually impossible not to expect that it’s testing an integer
    for oddness.

I agree. But I couldn’t think of what else to call them. When I want
to do things alternately in a loop, sometimes I want the even-numbered
iterations, and sometimes the odd.

  1. Are you sure you want #next (or #i_next, or whatever) to consume
    items? What if you want to grab the next item, but also want to
    iterate to that item in the normal way? You seem to have less
    functionality here (if I’m right about that) than you would with one
    of the “index” methods.

According to my own usage patterns, I usually do want it to consume.
If I were writing some kind of parser, it might be different.

  1. I’ve done some fancy #to_s’ing… not sure if I should have to do
    that. Anyway, I’ve done it, just to get the tests to pass initially.

Actually, according to the way I was doing things, I should have
said x.next.value instead of just x.next (assuming no one actually
wants to store an IteratorVar). Maybe that would help.

Hal

Joseph Erickson wrote:

I like it.

What about a ‘multilpe_of?’ function. I hope this makes sense:

You could have a multiple_of? function so you could say multiple_of?(3)
and it would give you every third iteration. You could also add another
parameter that would subtract back that number like multiple_of?(3,2)
would give you 1,4,7,10 etc. That would make even? be multiple_of?(2)
and odd? be multiple_of?(2,1)

def multiple_of?(multiple,back)
@count_ % multiple == back
end

If you wanted to generalize it further, you could feed it a block
which evaluates the ith desired index. e.g., {|i|2i} for evens,
{|i|2
i+1} for odds, {|i|n*i} for every nth, {|i|f(i)} for something
hairy, …