Need help with basic functional programming

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 22 10:22:41 PDT 2014


On Tuesday, 22 July 2014 at 16:50:47 UTC, Eric wrote:
> private void getNumber(MCInputStreamRange buf)
> {
>     auto s = buf.until("a <= '0' || a >= '9'");
>     curTok.kind = Token_t.NUMBER;
>     curTok.image = to!string(s);
> }
>
> The problem is that "until" seems to not stop at the end of the 
> number,
> and instead continues until the end of the buffer.  Am I doing 
> something
> wrong here?

You've forgotten the exclamation mark: buf.until!(...)
Without it, the string is not the predicate, but the sentinel
value. I.e. the range stops when it sees the characters "a <= '0'
|| a >= '9'".

By the way, do you really mean to stop on '0' and '9'? Do you
perhaps mean "a < '0' || a > '9'"?

>  Also, what is the fastest way to convert a range to a string?

The fastest to type is probably text(r) (or r.text). The fastest
for me to come up with is r.to!string, which does exactly the
same. I don't know about run time, but text/to!string is
hopefully fine.


More information about the Digitalmars-d-learn mailing list