std.gregorian contribution

Philippe Sigaud philippe.sigaud at gmail.com
Mon May 17 12:55:42 PDT 2010


On Mon, May 17, 2010 at 19:44, Andrei Alexandrescu <
SeeWebsiteForEmail at erdani.org> wrote:

>
> First, you may want to follow the model set by splitter() instead of
> split() when defining unjoin(). This is because split() allocates memory
> whereas splitter splits lazily so it doesn't need to. If you do want
> split(), just call array(splitter()).
>
> Second, there is an ambiguity between splitting using a string as separator
> and splitting using a set of characters as separator. This could be solved
> by simply using different names:
>
> string str = ...;
> foreach (splitByOneOf(str, "; ")) { ... }
> foreach (splitter(str, "; ")) { ... }
>
>
I personally use a predicate, isOneOf(some range). It's curried, to it
produces the 'real' predicate which returns true when its input is in the
range.

It's something like this:

bool delegate(ElementType!R) isOneOf(R)(R range) if (isInputRange!R)
{
    auto r = array(range);
    sort(r);
    return (ElementType!R e) { return !find(assumeSorted(r), e).empty;};
}

It's been a long time since I used std.algo.find. Is it efficient to sort
what will be most of the time a very short array? Maybe !find(range,e) is
enough.

Usage:

     splitBy!(isOneOf(";,/"))(rangeToBeSplitted)



One nice outcome is that we can then reuse the same pattern in other
> signatures.


Indeed, and for many things: filtering, stopping iterations (takeWhile,
unfoldWhile), splitting...
And, of course, taking the negation not!isOneOf => isNotIn(";,/")


Philippe
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20100517/b5c84846/attachment.html>


More information about the Digitalmars-d mailing list