A commenter on yesterday’s post asked about the lack of Dispose. Here’s a way of doing it:

public static IEnumerable<T> AsEnumerable<T>(
    this Tuple<Func<Tuple<bool, T>>, Action> source)
{
    try
    {
        for (var i = source.Item1(); i.Item1; i = source.Item1())
            yield return i.Item2;
    }
    finally { source.Item2(); }
}

public static Tuple<Func<Tuple<bool, T>>, Action> AsTuple<T>(
    this IEnumerator<T> source)
{
    return Tuple.Create<Func<Tuple<bool, T>>, Action>(
        () => source.MoveNext() ? Tuple.Create(true, source.Current)
                                : Tuple.Create(false, default(T)),
        source.Dispose);
}

public static Tuple<Func<Tuple<bool, T>>, Action> AsShared<T>(
    this Tuple<Func<Tuple<bool, T>>, Action> source, int clients)
{
    var padlock = new object();
    return Tuple.Create<Func<Tuple<bool, T>>, Action>(
        () => { lock (padlock) { return source.Item1(); }},
        () => { lock (padlock) { if (--clients == 0) source.Item2(); } }
        );
}

So then we can make something that can be shared between threads.

var mySequence = SomeFancyIteratorMethod();

var shared = mySequence.GetEnumerator().AsTuple().AsShared(4);

// in four other threads...
foreach (var n in shared.AsEnumerable())
{
    // can use 'break' in here
}

But is this necessarily a “good” way to do it?

It’s important to remember that we’re trying to solve two problems at once. Firstly, we need a single method for stepping through the items to be able to lock around it. Secondly, it’s usually more succinct and expressive to be able to write an inline lambda instead of a whole separate named class to implement an interface.

But when your interface of necessity has two operations in it, as this one does, then you’re going to be dealing with nested tuples of delegates, and C#’s type infererence just isn’t powerful enough to make that look pretty. The thing we’re using to represent a disposable iteration is Tuple<Func<Tuple>, Action>, or in other words: a tuple containing two things: a delegate that returns a tuple containing a bool and T, and an delegate that returns nothing. There are no helpful meaningful names on these things, so it starts to get pretty obscure.

This is what custom types (class, struct) are all about. Although tuples are great in short examples, I suspect in most real C# programs, we will continue to prefer custom-declared types so we can give meaningful names to things.

The above code would therefore probably be improved by writing a small class to use instead of Tuple, specifically for holding a GetNext delegate and a Dispose delegate. Apart from that, I think it has an edge over writing out traditional interfaces and classes that implement them, because lambdas and closures are just so much more succinct. A lot of noise in the code disappears because we don’t have to write constructors and fields explicitly.

There are lots of things of value that we can learn from functional programming languages, but I think un-named tuples are probably not the greatest thing.

By the way, there’s not actually anything strictly functional about this whole topic; in a pure functional language, a function never returns a different value the second time you call it with the same arguments.

If you remove that constraint, then a functional language would be nothing special! People talk about how in functional languages, there are higher order functions, which are functions that accept a function and return another function, and functions can be bound to data (closures). Well, in widely-used OO languages, an object is essentially a dictionary of functions (indeed, in JavaScript, that’s exactly what an object is). So string.Trim is a function that takes a dictionary of functions and returns another dictionary of functions. Big deal!

The mighty Skeet posted the other day about the idea of using a single instance of IEnumerable as a shared input for multiple threads (not that I realised at first, but I got there eventually).

Clearly the interface is no good for that purpose, because two operations (“check for existence of next item” and “read next item”) are exposed as separate methods/properties, so it can’t be made atomic. Jared Parsons has blogged a lot of times very readably about this.

This got me thinking, because I’ve noticed that I can often shrink an interface declaration down so it only has one method. And then it doesn’t need to be an interface; it can just be a delegate. That way you can implement it on the fly with a lambda expression. If you express it without any out/ref parameters, you don’t even have to declare a new delegate type. And if you have a Tuple class (as in .NET 4.0), you don’t need to declare any new types – just higher order methods.

Suppose you have a method that takes no parameters and returns something, i.e. any Func. This might be a useful general facility:

public static IEnumerable<T> Repeat<T>(this Func<T> read)
{
    for (; ; ) yield return read();
}

So now with any such method you can turn it into an IEnumerable:

Func<string> readLine = Console.ReadLine;
var lines = readLine.Repeat();

If you are in the habit of handing out such functions to multiple threads, and you want them to synchronize their calls to it, this would presumably be useful:

public static Func<TResult> ToAtomic<TResult>(this Func<TResult> func)
{
    var padlock = new object();
    return () => { lock (padlock) { return func(); } };
}

So for any function, you can now make another function that takes out a lock before calling the original function. (At first glance this may appear pointless, as the padlock object looks like a local variable, but it’s not – it’s captured by the new function we’re returning, and so will be shared across any callers who have a reference to that returned function.) So for example:

var threadSafeReadLine = readLine.ToAtomic();

(Not a great example if readLine is Console.ReadLine, as I think that’s probably already atomic).

As atomic interfaces often involve single methods (because otherwise they are impossible to make atomic in a simple way), this might be a very general useful facility, and you could create overloads of ToAtomic for 1, 2 … 8 arguments.

IEnumerator doesn’t fit that pattern, but we can fix it very easily with another extension method:

public static Func<Tuple<bool, T>> ToFuncT>(this IEnumerator<T> source)
{
    return () => source.MoveNext() ? Tuple.Create(true, source.Current)
                                   : Tuple.Create(false, default(T));
}

This makes us a new delegate that returns a Tuple, in which the bool will be false when we reach the end of the list. By converting to a Func in this way, we can now apply our ToAtomic extension to it. That makes it safe to hand out to multiple threads. Then for each thread seperately, we can turn it back into an IEnumerable by applying our Repeat extension to it.

Although for real convenience, a worker thread doesn’t really want an infinite sequence of tuples! They’d surely prefer a sequence of simple values, which terminates when there are no more values. That’s easy enough to enable:

public static IEnumerable<T> ToEnumerable<T>(
                  this Func<Tuple<bool, T>> read)
{
    return read.Repeat().TakeWhile(item => item.Item1)
                        .Select(item => item.Item2);
}

So now we can write a simple test:

var results = new HashSet<int>();

var atomicResultsAdd = new Func<int, bool>(results.Add).ToAtomic();

const int count = 10000000;
var numbers = Enumerable.Range(0, count);

var atomicNumbers = numbers.GetEnumerator().ToFunc().ToAtomic();

Util.Multithread(4, () =>
    {
        foreach (var number in atomicNumbers.ToEnumerable())
        {
            if (!atomicResultsAdd(number))
                Console.WriteLine("Clash: " + number);
        }
    });

for (int n = 0; n < count; n++)
{
    if (!results.Contains(n))
        Console.WriteLine("Skipped: " + n);
}

The definition of Util.Multithread is left as an exercise for the reader. :) Of course all it does is run the action passed to it on the requested number of threads, and waits for them all to finish.

We use a HashSet to check that each number in the range is consumed exactly once by a thread. (Another job for ToAtomic to do – this time, it’s the overload that takes one argument, which wraps the HashSet.Add method.)

You can see the difference if you change the expression for atomicNumbers (apologies for the chemistry pun):

var atomicNumbers = numbers.GetEnumerator().ToFunc() /*.ToAtomic()*/ ;

By commenting out that ToAtomic at the end, we cause a random number of messages about clashes and skips. Even so, they can be surprisingly rare, which is why threading bugs are so horrible. Here I’m testing with a range of ten million, and I still only get a handful of clashes or skips. This would presumably depend a little on the hardware.

I should say that the only problem with all this is that IEnumerable is not actually something that can generally be collapsed into a single method, because of its pesky (but very important) Dispose method that it inherits from IDisposable. If a consumer abandons an iteration without calling Dispose, then if the iteration is defined by an iterator method (containing yield return statements) it will not execute any outstanding finally blocks. So you can only use this approach safely for enumerators that don’t require clean up when they are abandoned.

In BCL 4.0, there’s still no Unfold method (boooo), but at least there’s a Zip extension method to keep us warm as winter draws on.

I played with it thusly:

    var list1 = new[] { 1, 2, 3 };
    var list2 = new[] { "a", "b", "c" };
    var both = list1.Zip(list2, (first, second) => new { first, second });
    foreach (var item in both)
        Console.WriteLine(item);

Then I thought, what about these new fangled tuples? Shouldn’t there be an overload of Zip that only takes two lists and is smart enough to return a binary tuple?

Then I realised there’s not really much need, because you can ask for that to happen very succinctly already:

var both = list1.Zip(list2, Tuple.Create);

This is not technically that astounding, because it’s just an example of a pre-defined function happening to fit the delegate required by some method. No big deal.

But in another way, it is a big deal, because of the generality of it. Suppose you have a generic method that accepts a delegate as one of its parameters, and that delegate must accept some fixed number of parameters of specific types, but can return any type. Sometimes you just want to be able to say “Oh, whatever they are, just stick them together and I’ll figure out what to do with them later”. Well, now that wish has a name, and it’s name is Tuple.Create. And this is applicable for any method that takes a delegate that takes up to 8 parameters and can return a value of unconstrained type.

Sometimes I just want to cry at the beauty of functions-as-first-class-objects.

(Just occasionally.)

Error message box with no text, not even on the button!

I’ve had error messages before where there was no text, just OK or Yes/No buttons, which looks amusing because you’ve no idea what you’re agreeing to. But if the button has no text either, the whole thing looks oddly sinister.

Assuming we have:

public class Base {}
public class Derived : Base {}
public class Unrelated {}

We can make a list of Base:

List<Base> baseList = new List<Base>();

And we can loop through the list:

foreach (Base item in baseList)

What other types can we use for the loop variable? We would expect to be able to use a type from which the collection’s item type is derived. In this case, that’s object:

foreach (object item in baseList)

And that’s fine, it makes perfect sense, because everything on our list is surely an object. If we had a list of Derived it would also make sense to let us use Base as the type of the loop variable. All good.

It wouldn’t make sense to let us use Unrelated:

foreach (Unrelated item in baseList)

It’s a certainty that the objects on our list are not of that type, so it has no hope of working. So we get a compiler error – good call!

But what about:

foreach (Derived item in baseList)

Now, the items on our list could be of that type – it’s certainly possible. But they could be of other types derived from Base. This is called a downcast, and if you need to do that, it’s often (but not always) a sign that there’s a kludge in your design.

What may be surprising is that the compiler allows this to compile. In other words, it silently inserts a downcast. This is unfortunate, because it’s probably a mistake. Most situations where you required a downcast, you would do this instead:

foreach (Derived item in baseList.OfType<Derived>())

That way, anything that couldn’t be cast to the required type would be safely skipped over.

Fortunately, the var keyword comes to the rescue. Always use var to declare the loop variable and you won’t fall into this hole.

foreach (var item in baseList)

The compiler uses the static type argument of the IEnumerable interface.

Note that var with a non-generic collection will be equivalent to object. But you can use the OfType trick again:

ArrayList whatever = new ArrayList();

foreach (var x in whatever.OfType<Unrelated>())

The foreach keyword predates generic collections, which is why it has the ability to silently cast; before generic collections, any code that looped through the contents of a collection would require a downcast, so it made a kind of sense to build it into the foreach keyword. Now we have generic collections, it makes almost no sense; but it’s not so bad because we have var to plug the hole, so we can’t fall into it.

Fun question on Stack Overflow just now. Someone wants to know how to fill a circle by setting pixels. In the interests of trying the simplest thing first, I suggested:

    Bitmap bmp = new Bitmap(200, 200);

    int r = 50; // radius
    int ox = 100, oy = 100; // origin

    for (int x = -r; x < r ; x++)
    {
        int height = (int)Math.Sqrt(r * r - x * x);

        for (int y = -height; y < height; y++)
            bmp.SetPixel(x + ox, y + oy, Color.Red);
    }

    bmp.Save(@"c:\users\dearwicker\Desktop\circle.bmp");

As the questioner was already using the Bresenham algorithm to minimise the use of square-root calculation, my answer naturally attracted comments, because I’d completely ignored this issue.

To be honest, I probably would have leapt to the same conclusion. After all – Math.Sqrt looks like some kind of hefty call to a method in the BCL. Who knows how many thousands of instructions we’re going to be wasting on it?

Actually, we can find out by looking in the disassembly window. So what exactly does Math.Sqrt turn into when you run the program?

for (int n = 0; n < reps; n++)
00000098 mov dword ptr [rsp+2Ch],0
000000a0 jmp 00000000000000DD
c += (int) Math.Sqrt(n);
000000a2 mov rcx,7FF001D1FF8h
000000ac mov ecx,dword ptr [rcx]
000000ae cvtsi2sd xmm0,dword ptr [rsp+2Ch]
000000b4 sqrtsd xmm0,xmm0
000000b8 movsd mmword ptr [rsp+60h],xmm0
000000be cvttsd2si eax,mmword ptr [rsp+60h]
000000c4 add ecx,eax
000000c6 mov rax,7FF001D1FF8h
000000d0 mov dword ptr [rax],ecx

That’s right – it gets inlined into what is effectively a single x86 assembler instruction!

The above disassembly comes from a test program I wrote to compare the time taken to sum the results of lots of Math.Sqrt calls, with the time taken to do simple addition. There is no difference – or at least, the difference is so small that it is lost in the noise.

So, no – square roots are not slow!

I was thinking about a gotcha in Java, and started wondering about the idea of making it convenient to intern reference objects.

“Interning” refers to the idea of ensuring that there is only one instance of an object with a given internal configuration – the classic example being strings in Java and .NET. When you construct a new string from a string literal, code is inserted to ensure that you get back a reference to the same string object for any given string literal value. So if you have the quoted string "Hello, world" in two places in your source code, they will point to the same object. This is a form of referential identity.

This predictably causes confusion in Java because learners are duped into thinking that they will be able to compare string values with ==, but this is not generally true, because other operations that create strings (such as concatenation, or reading from a file) do not take part in interning. It’s not such a problem in C# because you can implement a special version of == that compares the actual values instead of references, and this is exactly what the built-in System.String type does.

But then there are types that frequently occur in C# programs where you don’t have the ability to override the == operator. An example is an interface type – no overloading of operators is allowed with them. In the code I’m working on, a lot of things are described by COM interfaces, which look just like interfaces. I’d frequently like to be able to intern references to such things, allowing me to casually compare two references in a meaningful way. Especially where I’m dealing with an external COM library that just allocates objects on the fly to represent data, and so the identity of the object is not currently being put to good use – it’s completely meaningless.

But the right way to do this depends on some detail of the interface. There is also the question of the scope in which internship should apply – a whole process, or just the current thread?

Trivial example:

public interface IMyWeirdInterface
{
    string Name { get;}
}

What I’d like is to be able to assume that any two objects of that type will be the same instance if they have the same Name. So what I need is an object that manages the “internship” in that way:

var i = new Internship<IMyWeirdInterface, string>(m => m.Name);

IMyWeirdInterface x = new MockMyWeirdInterface { Name = "Fred" };
IMyWeirdInterface y = new MockMyWeirdInterface { Name = "Fred" };
IMyWeirdInterface z = new MockMyWeirdInterface { Name = "Jim" };

i.Intern(ref x);
i.Intern(ref y);
i.Intern(ref z);

Debug.Assert(x == y);
Debug.Assert(x != y);

So no matter from where a method obtains some references to IMyWeirdInterface, it can intern them and they will have referential identity. It just needs access to the right Internship object to manage this. With most COM objects, they are “apartment threadsafe”, which is a coy way of saying that they aren’t particularly threadsafe at all and should not be shared between threads. So for such objects, the right thing to do is make the Internship object thread local:

public static class Internships
{
    [ThreadStatic] private static Internship<IMyWeirdInterface, string> WeirdThreadLocal;

    public static Internship<IMyWeirdInterface, string> Weird
    {
        get
        {
            if (WeirdThreadLocal == null)
                WeirdThreadLocal = new Internship<IMyWeirdInterface, string>(m => m.Name);

            return WeirdThreadLocal;
        }
    }
}

As well as being absolutely necessary to ensure objects don’t get mysteriously shared between threads, this also has the nice quality of avoiding the need for locks to coordinate between threads – it’s a per-thread singleton. Now, any method can say:

Internships.Weird.Intern(ref x);
Internships.Weird.Intern(ref y);

And know that if x and y have the same Name, they will now be the same object.

Here’s the declaration of Internship (not exactly rocket science, is it?):

public class Internship<TRef, TKey>
{
    private readonly Dictionary<TKey, TRef> _map = new Dictionary<TKey, TRef>();
    private readonly Func<TRef, TKey> _keySelector;

    public Internship(Func<TRef, TKey> keySelector)
    {
        _keySelector = keySelector;
    }

    public void Intern(ref TRef obj)
    {
        TKey key = _keySelector(obj);
        TRef result;
        if (_map.TryGetValue(key, out result))
            obj = result;
        else
            _map.Add(key, obj);
    }
}

The keySelector parameter to the constructor is the thing that controls how intances can be treated as identical, by picking a key value from a property (the Name property in my examples above).

Inspired by this and this, here’s my version.

Jan Van Ryswyck is right to use static reflection, and to do that he "registers" the properties in a list. On the other hand, assuming you’d have a lot of these objects building up in the GC, it might prove important to reduce the number of ancillary allocations going on, so it may be better to avoid creating a list of the properties in every single instance.

It’s not really necessary to have the full information about the properties via reflection; we only need the values. Also it’s a pain to have to distinguish between fields and properties. Finally, we are really doing operations on a sequence of values, which can be expressed very neatly with Linq operators, and specified in the derived class with an iterator method.

So here’s what I came up with:

public abstract class ValueObject<T> : IEquatable<T>
    where T : ValueObject<T>
{
    protected abstract IEnumerable<object> Reflect();

    public override bool Equals(Object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (obj.GetType() != GetType()) return false;
        return Equals(obj as T);
    }

    public bool Equals(T other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return Reflect().SequenceEqual(other.Reflect());
    }

    public override int GetHashCode()
    {
        return Reflect().Aggregate(36, (hashCode, value) => value == null ?
                                hashCode : hashCode ^ value.GetHashCode());
    }

    public override string ToString()
    {
        return "{ " + (string) Reflect().Aggregate((l, r) => l + ", " + r) + " }";
    }
}

First off, it’s a lot shorter! Aside from the standard ReferenceEquals checks, every method is a one-liner, a return of a single expression. Check out how SequenceEqual does so much work for us. And Aggregate is designed for turning a sequence into one value, which is exactly what GetHashCode and ToString are all about.

This is all possible because we’re treating the properties or fields as just a sequence of values, obtained from the Reflect method, which the derived class has to supply.

(You could easily add operator== and operator!= of course. Also in case you’re wondering about the way ToString appears not to check for null, actually it does, because one odd thing about .NET is that string concatenation can cope with null strings.)

Secondly, the way you use it is pretty neat as well:

public class Person : ValueObject<Person>
{
    public int Age { get; set; }
    public string Name { get; set; }

    protected override IEnumerable<object> Reflect()
    {
        yield return Age;
        yield return Name;
    }
}

It wouldn’t matter if I yielded the values of fields or properties, and there’s no need for expression-lambda trickery to get a PropertyInfo.

If you’re unfamiliar with how iterator methods work, you may be wondering, what if I have twenty fields and the first fields of two instances are unequal, isn’t this going to waste time comparing the other nineteen fields? No, because SequenceEqual will stop iterating as soon as it finds an unequal element pair, and iterator methods are interruptible.

(Note that if you need this to work on .NET 2.0, you can grab Jared Parsons’ BCL Extras library to get the necessary sequence operations. If you’re using the C# 2.0 compiler you just need to rejig the method call syntax to avoid using them as extension methods. Iterator methods were already available in 2.0, so nothing here is dependent on 3.0.)

C# has the yield return feature, which makes it easy to write state machines as if they were plain old methods:

class Program
{
    static IEnumerable<int> SomeNumbers()
    {
        Console.WriteLine("Started");

        yield return 1;

        Console.WriteLine("Yielded 1");

        yield return 2;

        Console.WriteLine("Yielded 2");

        yield return 3;

        Console.WriteLine("Finished");
    }

    static void Main(string[] args)
    {
        foreach (int n in SomeNumbers())
        {
            Console.WriteLine(n);
        }
    }
}

The output shows that the sequence of numbers is generated "lazily", with each chunk of code in the method being executed only on demand as the foreach pulls values from sequence.

How close can we get to this using only lamdbas? Pretty close:

class Program
{
    static Lazy.Yielding<int> SomeNumbers()
    {
        Console.WriteLine("Started");

        return Lazy.Yield(1, () => {

        Console.WriteLine("Yielded 1");

        return Lazy.Yield(2, () => {

        Console.WriteLine("Yielded 2");

        return Lazy.Yield(3, () => {

        Console.WriteLine("Finished");

        return null; }); }); });
    }

    static void Main(string[] args)
    {
        foreach (int n in Lazy.Enumerate<int>(SomeNumbers))
        {
            Console.WriteLine(n);
        }
    }
}

By messing with the nesting of my curly braces, I’ve made it look like the original, but really it’s made of three nested lambdas. So this version of SomeNumbers is, deep breath… a function that returns a function that returns a function that returns a function.

Each returned function supplies the code to execute for the next step.

The main remaining ingredient is a helper function Lazy.Enumerate that turns our strange contraption into a plain IEnumerable, so we can loop through it conveniently.

public static class Lazy
{
    public class Yielding<T>
    {
        public readonly T Result;
        public readonly Func<Yielding<T>> Next;

        public Yielding(T result)
        {
            Result = result;
            Next = null;
        }

        public Yielding(T result, Func<Yielding<T>> next)
        {
            Result = result;
            Next = next;
        }
    }

    public static Yielding<T> Yield<T>(T value, Func<Yielding<T>> next)
    {
        return new Yielding<T>(value, next);
    }

    public static Yielding<T> Yield<T>(T value)
    {
        return new Yielding<T>(value);
    }

    public class Seq<T> : IEnumerable<T>
    {
        private readonly Func<Yielding<T>> _generator;

        public Seq(Func<Yielding<T>> generator)
        {
            _generator = generator;
        }

        private class Iter : IEnumerator<T>
        {
            private Func<Yielding<T>> _generator;

            public Iter(Func<Yielding<T>> generator)
            {
                _generator = generator;
            }

            public T Current { get; set; }

            public void Dispose() { }

            object System.Collections.IEnumerator.Current
            {
                get { return Current; }
            }

            public bool MoveNext()
            {
                if (_generator == null)
                    return false;

                Yielding<T> yielding = _generator();
                if (yielding == null)
                {
                    _generator = null;
                    return false;
                }

                Current = yielding.Result;
                _generator = yielding.Next;
                return true;
            }

            public void Reset()
            {
                throw new NotImplementedException();
            }
        }

        public IEnumerator<T> GetEnumerator()
        {
            return new Iter(_generator);
        }

        System.Collections.IEnumerator
            System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    public static IEnumerable<T> Enumerate<T>(
                      Func<Yielding<T>> generator)
    {
        return new Seq<T>(generator);
    }
}

Most of the code is "boilerplate" implementation of IEnumerable/IEnumerator. The important bit is the MoveNext method, which calls the generator method to get the next value and the next generator method.

So what’s missing? The major thing (aside from the misery of getting the syntax right, closing all the right brackets, etc.) is the lack of try/finally support, which turns out to be extremely useful. We could add support for that, however. Firstly, we’d add this member to Yielding.

public readonly Action Finally;

The generator code would initialize that field to whatever action it liked, to represent the finally block, before returning the Yielding instance. And Lazy.Seq.Iter would store it so it could execute it before retrieving the next value, and it would also execute it from the Dispose method, so that the Finally action would run even if the loop was abandoned.

A while ago I wrote up a little demo of using lambdas to implement convenient lazy list comprehension in C++0x, but at the time the VC++ compiler I was using lacked decltype, and I found a need for it.

I just tried out the Beta 1 that was released this week, and it has decltype, so here’s the updated sample:

#include <iostream>
#include <vector>
#include <sstream>

// Some helpers for working with decltype 
// (maybe these are in std:: somewhere?)
template <class T>
T *make_ptr() { return (T *)0; }

template <class T>
const T &make_ref() { return *(make_ptr<T>()); }

// so you don't need to obtain boost::lexical_cast!
template <class T>
std::string to_string(const T &v)
{
    std::ostringstream s;
    s << v;
    return s.str();
}

// Unary function that always returns true
template <class T>
struct always_true
{
    bool operator() (const T &) const { return true; }
};

// Unary function that returns its argument
template <class T>
struct pass_thru
{
    T operator() (const T &e) const { return e; }
};

template <class TElemFrom,
          class TElemTo,
          class TIterFrom,
          class TSelector,
          class TPredicate>
class filter
{
    TIterFrom from_;
    TIterFrom end_;
    const TSelector &selector_;
    const TPredicate &predicate_;

public:
    filter(TIterFrom from, TIterFrom end,
             const TSelector &selector,
             const TPredicate &predicate)
        : from_(from), end_(end),
          selector_(selector),
          predicate_(predicate) {}

    class const_iterator
    {
        TIterFrom from_;
        TIterFrom end_;
        const TSelector &selector_;
        const TPredicate &predicate_;

        void locate()
        {
            while (!done())
            {
                if (predicate_(*from_))
                    return;

                ++from_;
            }
        }

        bool done() const { return (from_ == end_); }

    public:
        const_iterator(TIterFrom from, TIterFrom end,
                       const TSelector &selector,
                       const TPredicate &predicate)
            : from_(from), end_(end),
              selector_(selector),
              predicate_(predicate) { locate(); }

        TElemTo operator*() const { return selector_(*from_); }

        const_iterator operator++()
        {
            ++from_;
            locate();
            return *this;
        }

        bool operator==(const const_iterator &other) const
            { return done() && other.done(); }

        bool operator!=(const const_iterator &other) const
            { return !done() || !other.done(); }
    };

    typedef TElemFrom value_type;

    const_iterator begin() const
        { return const_iterator(from_, end_, selector_, predicate_); }

    const_iterator end() const
        { return const_iterator(end_, end_, selector_, predicate_); }

    template <class TSelector>
    filter<TElemTo,
             decltype(make_ref<TSelector>()(make_ref<TElemTo>())),
             const_iterator,
             TSelector,
             always_true<TElemTo> >
        select(const TSelector &selector)
    {
        return filter<TElemTo,
            decltype(make_ref<TSelector>()(make_ref<TElemTo>())),
                      const_iterator,
                      TSelector,
                      always_true<TElemTo> >
                    (begin(),
                     end(),
                     selector,
                     always_true<TElemTo>());
    }

    template <class TPredicate>
    filter<TElemTo,
              TElemTo,
             const_iterator,
             pass_thru<TElemTo>,
             TPredicate>
        where(const TPredicate &predicate)
    {
        return filter<TElemTo,
                        TElemTo,
                        const_iterator,
                        pass_thru<TElemTo>,
                        TPredicate>
                    (begin(),
                     end(),
                     pass_thru<TElemTo>(),
                     predicate);
    }
};

template <class TCollFrom>
filter<typename TCollFrom::value_type,
         typename TCollFrom::value_type,
         typename TCollFrom::const_iterator,
         pass_thru<typename TCollFrom::value_type>,
         always_true<typename TCollFrom::value_type> >
    from(const TCollFrom &from)
{
    return filter<typename TCollFrom::value_type,
                    typename TCollFrom::value_type,
                    typename TCollFrom::const_iterator,
                    pass_thru<typename TCollFrom::value_type>,
                    always_true<typename TCollFrom::value_type> >
                (from.begin(),
                 from.end(),
                 pass_thru<typename TCollFrom::value_type>(),
                 always_true<typename TCollFrom::value_type>());
}

int main(int argc, char* argv[])
{
    std::vector<int> vecInts;
    vecInts.push_back(5);
    vecInts.push_back(2);
    vecInts.push_back(9);

    auto filtered = from(vecInts)
        .where([] (int n) { return n > 3; })
        .select([] (int n) { return "\"" + to_string(n) + "\""; });

    for (auto i = filtered.begin(); i != filtered.end(); ++i)
        std::cout << *i << std::endl;

    return 0;
}

The main function is the pay-off, of course. I have a vector of ints, and I wrap it something that ends up stored in a variable called filtered, which I can then iterate through. The filtering (discarding anything not greater than 3) and transforming (int to quoted string) of the items happens on-the-fly during that iteration.

(And to clarify, this is different from the last time I posted it because select no longer requires any type parameters to be explicitly given – thanks to decltype).