• Feeds

    Subscribe in a reader

  • Ads

Iterators (beyond iteration)

Diving deep into the implementation of streams in Cw has given me a new appreciation for the power of iterators.

Wes has posted some really cool stuff about how iterators can be applied to problems other than simple iteration. I haven’t quite parsed all of it yet, but his ideas are pretty interesting. Raised my eyebrows a bit, certainly.

Update: fixed the link

#1 Andy on 8.11.2004 at 5:54 PM

I don't get it? Why are the iterators such a big deal? They have been available in C++ for years. Is this a case of VB people learning C# and so now for the first time they are seeing iterators? That would make sense and I could see why they would be happy about it. But if it's developer coming from a C or C++ background raving about iterators in C# I would wonder what rock they have been hiding under.

#2 Steve Maine on 8.11.2004 at 6:13 PM

I stopped coding C++ when the STL was still in its relative infancy, so it's entirely possible that things have greatly evolved since I last wrote STL code. But from what I remember of STL iterators, they were more akin to C# enumerators than C# iterators. Specifically, I don't remember them offering the Haskell-like lazy list semantics that you get with C# iterators. I don't remember anything remotely approximating the meaning of "yield return". I suppose you could do some fancy stuff with templates in C++ to get the lexical closure that you get in C#, but it's been too long for me to estimate if this would be practical.I thought Wes's post was interesting because it showed that iterators have use beyond the simple foreach statement. After seeing how they were used inside of Cw to implement streams, I realized that there's a lot of cool stuff you can do with them. Wes's observation that iterators can be used as continuations made me go "hrmmm".

#3 Andy on 8.12.2004 at 9:01 PM

I see what you mean. The iterators in C# are the STL's generic algorithm's. Which very much enable you to do stuff like his chess game where the yield of one returns the other and so on. I think I read it wrong the first time. I thought he was calling iterators what I see now were what he's calling enumerators. The lazy list semantics you are talking about are done by using the algorthms on the STL List or whatever else you want to use them on. Iterators are like you said more like what he's calling enumerators although C++ iterators do a lot more than C#'s enumerators from the looks of it. I guess because I started with C++ after the STL was mature and I came from C I have always taken it for granted. When C# first came out I couldn't figure out why anyone would want to use it because it was missing things like generics, algorithms, etc. Everything you take for granted in the STL. Now as the newer versions of C# have come out it's getting cooler and cooler. I wrote a driver in it the other day. Took me ten minutes. Granted it was a simple driver but I am beggining to see why C# is so attractive. If they fully implement generics as fully as C++ implements templates I would like it even more. I use the cr@p out of templates and now anytime I'm working in another language that doesn't support generics I feel constrained. C# has already come out with way better generics than Java has ever had and the language is still young.For all the years between when you quit using C++ and when C# came out, what language were you using?