On 11 May 2004, skaller wrote:
> On Tue, 2004-05-11 at 17:36, Nicolas Cannasse wrote:
> > > Is there a particular reason that a new data type was invented? Is it
> > > still possible to switch to streams?
> > Enum permit also the cloning of an enumeration at any
> > time,
> This isn't quite correct. IF the enum represents a forward
> iterator, THEN the enum is clonable, otherwise it isn't.
A side note: alterative implementations don't necessarily have this
restriction. The OO iterator code I posted a couple of days ago doesn't
have this restriction.
I've been spending time working in C++ again recently. And I'm returning
to my old philosophy of simply assuming if C++ does something, it's wrong.
In this specific case, I don't see the advantage of having 18 gazillion
different types of iterators/enums. You very rapidly need to know what
data structure the iterator came from- at which point the prime benefit of
iterators (genericity) has vanished. As an example, I *can* implement a
"back" function on Ocaml's singly linked lists:
let rec back orig_list curr_loc =
match orig_list, curr_loc with
| car :: ((foo :: _) as cdr), x :: _ ->
if foo == x then
back cdr curr_loc
| _ -> assert false
Only problem is- that function is O(n). Opps! So we don't provide it.
But at what point is a function to expensive to provide? What if the back
function is O(log n) cost- do you provide it or not? What if it's an
expensive O(1) cost (lots of lseeks and small reads to find where the last
line began, for example)?
No. Keep things simple. If you need to do something more complicated
with the data, either put it in a more advanced data structure, or require
it already be in one (by taking an argument with a type of that data
structure, not a enum).
Personally, I think I'd ditch the count function before I'd introduce a
second class of iterators.
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
- Gene Spafford