Jez Higgins

Freelance software generalist
software created
extended or repaired


Older posts are available in the archive or through tags.

Feed

Follow me on Twitter
My code on GitHub

Contact
About

Monday 25 November 2013 The Forest Road Reader, No 112

I mentioned Steve's C++ range library, narl, a few weeks ago but only got round to reading the accompanying article in Overload last week. I liked it so much I read it twice. And then I wrote some code of my own.

A few years ago I gave a couple of talks on doing useful things with iterators, specifically in Java, looking at them for filtering, transformation, and so on. The main idea I was trying to get across was that while, particularly in Java, iterators are mainly used to loop from one end of a collection to another, iterators are a useful thing in their own right. They can iterate over anything, even things that donn't exist. This is more familiar idea in C++, where things like stream extractors and insertors exist as part of the standard library, but still not as widely used as it might be. This is, I'd suggest, primarily because of the pointer-like nature of C++ iterators, which means you have to write a lot of things twice. Look at the last bit of sample code in my piece about a C++ filtering iterator (which, incidentally, also co-stars Steve) for a typical example.

Coming back to Java, I think what I was saying was good stuff, and fundamentally sound (I got quoted, unconsciously I'm sure, by Kevlin in one of his talks a couple of years back so it must have been ok), but the code was really unwieldy to work with.

  Iterator<U> r =
          new Transformer<T, U>(
                 new Filter<T>(list, new Predicate<T> { ... }),
                 new Function<T,U> { ... });
  ...
There is SO MUCH NOISE. Plus it's all in the wrong order. It isn't obvious what's going on. I did build some quite handy stuff with it, and other people did too, but I was never really happy with it.

I was in Andrei's ranges talk too. The ideas he described and the code he presented were terrific and I got quite excited. However, even though what he was describing as a range was, conceptually, very close to a Java iterator, and it was only a few months since I'd given my own presentation I didn't make the connection. Basically I am an idiot.

I get it now though, essentially because Steve has pulled me by the nose through his article. There's an elegance and clarity to Steve's narl stuff. You read the line and you know what it does.

  auto r = from(src)
          | where([](const item& i) { return i.size() > 0; })
          | select([](const item &i) { return i.name(); });
It's lovely. It why C++ is so cool sometimes - no base classes, no runtime overheads, clear syntax, extensible, etc, etc. (It's a one article argument for operator overloading even without anything else.)

So, I went back to the Java world I mainly live in now, put back in the base classes and the runtime overhead, sacrificed the extensibility, but hopefully managed to retain some of the clarity. Now I'm writing stuff like

  Iterator<U> r = from(list).
                   where(new Predicate<T> {...}).
                   select(new Function<T, U> {...});
This is does exactly the same thing as the noisy code snippet above, except that what it does is much clearer. I'll take that.

Thanks Andrei. Thanks Steve.


Tagged c++, and java


Jez Higgins

Freelance software generalist
software created
extended or repaired

Older posts are available in the archive or through tags.

Feed

Follow me on Twitter
My code on GitHub

Contact
About