On 2/22/11 3:23 PM, Philippe Sigaud wrote:
On Tue, Feb 22, 2011 at 15:23, Andrei Alexandrescu
<seewebsiteforem...@erdani.org>  wrote:

auto singletonRange(T)(T element)
{
    static struct Result
    {
        private T _element;
        private bool _done;
        @property bool empty() { return _done; }
        @property auto front() { assert(!empty); return _element; }
        void popFront() { assert(!empty); _done = true; }
        auto save() { return this; }
    }

    return Result(element, false);
}
That's also what many people would like a findFirst function to
return. Either a 1-element range with the found value or an empty
range if the value doesn't exist.
auto v = findFirst(range, needle);
if (!v.empty) { ... }

Good point.

Maybe you could allow for the creation of an empty output
singletonRange where a lone value could then be put.
auto singleton(T)() { ... } but the user would need to provide the 'T'
by himself.

Yah, I thought of that after posting. It would look something like this:

auto singletonRange(T)()
{
    typeof(singletonRange(T.init)) result = void;
    result._done = true;
    return result;
}

No mercy :o). But that's unsafe so probably we'd need to leave out "= void".

and then:
void put(T element) {assert(empty); _element = element;}
That could be a way to modify a container. Btw, is there a way to have
an empty container and use an output range to fill it? I didn't look
at std.container for quite some time.

No, but this may be an interesting idea to pursue.


Andrei

Reply via email to