We need to rethink remove in std.container

Philippe Sigaud philippe.sigaud at gmail.com
Tue Feb 22 13:23:42 PST 2011


On Tue, Feb 22, 2011 at 15:23, Andrei Alexandrescu
<SeeWebsiteForEmail at 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) { ... }


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.
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.


More information about the Digitalmars-d mailing list