Generators in Eiffel? 
Author Message
 Generators in Eiffel?

I was recently reading about the recent additions to Python(2.2) of an
iterator/generator capability and also the recent addition to Eiffel
of agents in order to write generic iterators.

There is usually a strong connection between iterators and generators
in that many iterators can be written very simply using generators.

This lead me to a question, is there a way in Eiffel to write
generators in the spirit found in the new python 2.2 release? (Which
was based on the idea of generators in Icon.  See
http://www.*-*-*.com/ #SECTION000400000000000000000
for a gentle introduction to this new Python feature)

If not, is this an appropriate new language extension for Eiffel?

I suspect that utilizing SCOOP maybe a solution to this question, but
I'm not that proficient in Eiffel to say for sure.

I have had to write generators in C and they're not pretty.  If I have
to write them in Eiffel (which I plan on doing soon), I'd like to know
there is a simple and elegant way to do it (just as writing almost
anything in Eiffel tends to be!).

Thanks for any light that can be shed on this conundrum.

--> John



Thu, 25 Mar 2004 21:49:16 GMT  
 Generators in Eiffel?

Quote:

> I was recently reading about the recent additions to Python(2.2) of an
> iterator/generator capability and also the recent addition to Eiffel
> of agents in order to write generic iterators.

> There is usually a strong connection between iterators and generators
> in that many iterators can be written very simply using generators.

Well may I ask what is so wonderful about iterators and generators?

see e.g Iterators: Signs of Weakness in Object Oriented Languages

what about agents or higher order functions?
With anonymous agents you get IMHO a much nicer solution.

Quote:

> This lead me to a question, is there a way in Eiffel to write
> generators in the spirit found in the new Python 2.2 release? (Which
> was based on the idea of generators in Icon.  See
> http://www.amk.ca/python/2.2/index.html#SECTION000400000000000000000
> for a gentle introduction to this new Python feature)

Well it certainly can be done. The question is why should one do it?

Quote:

> If not, is this an appropriate new language extension for Eiffel?

IMHO no. The right things seems to me using higher order functions on
different datastructures.

Quote:

> I suspect that utilizing SCOOP maybe a solution to this question, but
> I'm not that proficient in Eiffel to say for sure.

I doubt it, IIRC is SCOOP about concurrency in Eiffel.

Regards
Friedrich



Fri, 26 Mar 2004 16:12:10 GMT  
 Generators in Eiffel?


Quote:

> > I was recently reading about the recent additions to Python(2.2) of an
> > iterator/generator capability and also the recent addition to Eiffel
> > of agents in order to write generic iterators.

> > There is usually a strong connection between iterators and generators
> > in that many iterators can be written very simply using generators.
> Well may I ask what is so wonderful about iterators and generators?

> see e.g Iterators: Signs of Weakness in Object Oriented Languages

Links, if any? ISBN?

-- eirik

"If I can't Eiffel in heaven, I won't go"

Quote:

> what about agents or higher order functions?
> With anonymous agents you get IMHO a much nicer solution.

> > This lead me to a question, is there a way in Eiffel to write
> > generators in the spirit found in the new Python 2.2 release? (Which
> > was based on the idea of generators in Icon.  See
> > http://www.amk.ca/python/2.2/index.html#SECTION000400000000000000000
> > for a gentle introduction to this new Python feature)
> Well it certainly can be done. The question is why should one do it?

> > If not, is this an appropriate new language extension for Eiffel?
> IMHO no. The right things seems to me using higher order functions on
> different datastructures.

> > I suspect that utilizing SCOOP maybe a solution to this question, but
> > I'm not that proficient in Eiffel to say for sure.
> I doubt it, IIRC is SCOOP about concurrency in Eiffel.

> Regards
> Friedrich



Fri, 26 Mar 2004 16:05:46 GMT  
 Generators in Eiffel?

Quote:




> > > I was recently reading about the recent additions to Python(2.2) of an
> > > iterator/generator capability and also the recent addition to Eiffel
> > > of agents in order to write generic iterators.

> > > There is usually a strong connection between iterators and generators
> > > in that many iterators can be written very simply using generators.
> > Well may I ask what is so wonderful about iterators and generators?

> > see e.g Iterators: Signs of Weakness in Object Oriented Languages

Sorry

   Iterators: Signs of Weakness in Object-Oriented Languages
   ftp://ftp.netcom.com/pub/hb/hbaker/Iterator.html
   ftp://ftp.netcom.com/pub/hb/hbaker/Iterator.ps.Z

Don't know if one can get it there but I have read it some days ago
somewhere

Regards
Friedrich



Fri, 26 Mar 2004 16:40:54 GMT  
 Generators in Eiffel?
http://lambda.weblogs.com/
http://lambda.weblogs.com/discuss/msgReader$1789


Fri, 26 Mar 2004 23:08:41 GMT  
 Generators in Eiffel?
I have to say that generators are a very nice feature. And they don't
have much to do with iterators, apart from a supeficial resemblace. So
the paper you refer to is kind of mooth. To properly mimic generators
you need functions as first class values and the ability to capture
continuations. That Eiffel does not have. But most computer languages
are turing complete. So it is possible to do the same using Agents.

MvH Dan Andersson



Fri, 26 Mar 2004 23:05:47 GMT  
 Generators in Eiffel?

Quote:

> I was recently reading about the recent additions to Python(2.2) of an
> iterator/generator capability and also the recent addition to Eiffel
> of agents in order to write generic iterators.

> There is usually a strong connection between iterators and generators
> in that many iterators can be written very simply using generators.

> This lead me to a question, is there a way in Eiffel to write
> generators in the spirit found in the new Python 2.2 release? (Which
> was based on the idea of generators in Icon.  See
> http://www.*-*-*.com/ #SECTION000400000000000000000
> for a gentle introduction to this new Python feature)

> If not, is this an appropriate new language extension for Eiffel?

> I suspect that utilizing SCOOP maybe a solution to this question, but
> I'm not that proficient in Eiffel to say for sure.

> I have had to write generators in C and they're not pretty.  If I have
> to write them in Eiffel (which I plan on doing soon), I'd like to know
> there is a simple and elegant way to do it (just as writing almost
> anything in Eiffel tends to be!).

> Thanks for any light that can be shed on this conundrum.

A reference that works for the paper "Iterators: Signs of Weakness in
Object-Oriented Languages" is here:
http://www.*-*-*.com/
Access the PDF cached version from the upper right corner.

At least two Eiffel compilers suppor agents, SmallEiffel and ISE, and
I believe the other vendors have committed to supporting this feature
in the future.

Agents are function closures, referred to in Baker's paper above. I've
been working a bit with agents and as far as I can tell they could
emulate the Python generator feature you cite.

But having agents, or closures, can eliminate the need for iterators
altogether. For example, the agent-enabled Eiffel compilers have
container classes that provide functions like "do_all" which takes an
agent that can then be applied to all the elements of an array.

Since the agent is a closure, the agent can be defined with some of
its parameters set at the time its created, and others set at the time
its executed.

Here's a simple example that will run with a recent version of the
SmallEiffel beta.

--------------------------------

indexing
   purpose:
   "A simple example of using an agent with%N%
   %the do_all feature from ARRAY. The feature%N%
   %exclaim will print out each element of an%N%
   %integer array, and if the valu of the element%N%
   %exceeds its e{*filter*}ment threshold, it will%N%
   %write out an exclamation. In this case,%N%
   %the agent is a feature of the current class,%N%
   %but it could just as easily reside in a%N%
   %separate class.%N"

class EXAMPLE
creation make
feature
   numbers: ARRAY[INTEGER] is -- our test array
      once
         create Result.from_collection(<<1,100,20,4,75>>)
      end

   exclaim(threshold, value: INTEGER) is
      -- print out an integer, exclaim if its big
      do  
         print(value.out)
         if value >= threshold then
            print("  -- WOW!%N")
         else
            print("%N")
         end
      end
feature
   make is
      do  
         -- apply the feature exclaim
         -- as an agent on all the elements
         -- in numbers.
         --
         numbers.do_all(exclaim(25,?))
      end
end

------- output -----------
1
100  -- WOW!
20
4
75  -- WOW!

---------------------------

The things worth noting: the agent, "exclaim" receives the two
parameters, but one, the threshold, is set when the agent is created
in the call to do_all. The second parameter is passed by do_all down
to exclaim as do_all iterates over the container. Third, there's no
need for looping logic. Essentially, the guts of a loop have been
placed into a self-contained routine, and the container handles all
the looping and iteration issues.



Sat, 27 Mar 2004 00:03:12 GMT  
 Generators in Eiffel?

Quote:

>I was recently reading about the recent additions to Python(2.2) of an
>iterator/generator capability and also the recent addition to Eiffel
>of agents in order to write generic iterators.

>There is usually a strong connection between iterators and generators
>in that many iterators can be written very simply using generators.

Does anyone have a reference that explains the difference between
generators and coroutines?

--
--
Patrick Doyle



Sat, 27 Mar 2004 03:57:10 GMT  
 Generators in Eiffel?

Quote:

> Sorry

>    Iterators: Signs of Weakness in Object-Oriented Languages
>    ftp://ftp.netcom.com/pub/hb/hbaker/Iterator.html
>    ftp://ftp.netcom.com/pub/hb/hbaker/Iterator.ps.Z

> Don't know if one can get it there but I have read it some days ago
> somewhere

There are cached copies at http://citeseer.nj.nec.com/55669.html (PS and PDF).

Frank Mitchell



Sat, 27 Mar 2004 04:17:39 GMT  
 Generators in Eiffel?

Quote:

> I have to say that generators are a very nice feature. And they don't
> have much to do with iterators, apart from a supeficial resemblace. So
> the paper you refer to is kind of mooth. To properly mimic generators
> you need functions as first class values and the ability to capture
> continuations. That Eiffel does not have. But most computer languages
> are turing complete. So it is possible to do the same using Agents.

> MvH Dan Andersson

Dan, what do you mean by "the ability to capture continuations"?


Sat, 27 Mar 2004 07:02:55 GMT  
 Generators in Eiffel?

Quote:
> But most computer languages
> are turing complete. So it is possible to do the same using Agents.

FWIW, it's kind of useless to say "well, yes we can do that in Eiffel,
because Eiffel is turing complete." All that means is that Eiffel is
powerful enough to write an interpreter for a language that *does* have
completions (or whatever). Which is almost always overkill for solving
the problem at hand.

--
Darren New
San Diego, CA, USA (PST). Cryptokeys on demand.
                   Who is this Dr. Ibid anyway,
                  and how does he know so much?



Sat, 27 Mar 2004 07:47:56 GMT  
 Generators in Eiffel?

Quote:
> > I was recently reading about the recent additions to Python(2.2) of an
> > iterator/generator capability and also the recent addition to Eiffel
> > of agents in order to write generic iterators.

> > There is usually a strong connection between iterators and generators
> > in that many iterators can be written very simply using generators.

> <snip> ... Greg's code example ... <snip>

> The things worth noting: the agent, "exclaim" receives the two
> parameters, but one, the threshold, is set when the agent is created
> in the call to do_all. The second parameter is passed by do_all down
> to exclaim as do_all iterates over the container. Third, there's no
> need for looping logic. Essentially, the guts of a loop have been
> placed into a self-contained routine, and the container handles all
> the looping and iteration issues.

OK, that was an example of using an agent but that's not quite what I
had in mind when I was speaking of a generator.

Baker gives a nice example regarding summing an array of integers:

Let's suppose we have a function that needs the values of the
intermediate sums of an array(let's say its going to print them out in
a table), let's also suppose we want to write a second function that
returns the intermediate sums of the summation process which the first
function will call successively to get those intermediate values. [A
more classic example is to return a sequence of Random Numbers]

Without a generator like facility, in order to code the second
function returning the intermediate sums you'd need to either:

1) save state between calls to the second function so that it can pick
up where it left off.

or

2) run the second function concurrently, writing the intermediate sums
to a queue, with the first function reading them from the queue. [
This is the co-routines solution that someone was looking for a
defintion of ]

However, with a generator facility, the compiler arranges for the
state of the second funtion to be saved and restored automatically.
(In Python, this is indicated by using a "yield" rather than a
"return")

I'm not quite sure how you get that kind of behaviour using agents.

Friedrich seems to indicate that a re-formulation of the problem using
agents maybe more appropriate but I'm not quite sure how to do that.

Can you shed some light Greg on how to accomplish this?

Thanks!

John



Sat, 27 Mar 2004 10:56:24 GMT  
 Generators in Eiffel?

Quote:
> 2) run the second function concurrently, writing the intermediate sums
> to a queue, with the first function reading them from the queue. [
> This is the co-routines solution that someone was looking for a
> defintion of ]

Not really. That would be a multithreaded solution. A coroutine is more
like having a "return" statement that when you call the function again,
it picks up after the return statement rather than at the beginning
again. There's still only one thread of control. Of course, you can
simulate coroutines with threads by having one thread in the function
tracing the program counter, and basically using synchronization
primitives to block the progress of one thread or the other. But
coroutines aren't really threads any more than exceptions or
once-functions are.

--
Darren New
San Diego, CA, USA (PST). Cryptokeys on demand.
                   Who is this Dr. Ibid anyway,
                  and how does he know so much?



Sat, 27 Mar 2004 23:24:28 GMT  
 
 [ 29 post ]  Go to page: [1] [2]

 Relevant Pages 

1. random number generator in Eiffel

2. Eiffel Wrapper Generator 0.5.1 released

3. Eiffel Wrapper Generator Version 0.5.0 released

4. : Eiffel Wrapper Generator Version 0.4.0 released

5. Eiffel Wrapper Generator (Version 0.3.0)

6. Eiffel Wrapper Generator (Version 0.1)

7. call graph generator for any flavor of eiffel?

8. Parser Generator/Scanner Generator for Scheme?

9. Wanted: parser-generator/lexical-analyser-generator outputting Scheme

10. Enhanced Generators - two-way generators

11. Generator syntax (was: Calling a generator multiple times)

12. generator within a generator doesn't execute?

 

 
Powered by phpBB® Forum Software