iteration over a string

Timothee Cour thelastmammoth at gmail.com
Tue May 28 03:39:55 PDT 2013


On Tue, May 28, 2013 at 3:20 AM, bearophile <bearophileHUGS at lycos.com>wrote:

> Timothee Cour:
>
>  string a="ΩΩab";
>>> auto b1=a.map!(a=>"<"d~a~">"d).**array;
>>> writeln(b1);//["<Ω>", "<Ω>", "<a>", "<b>", "", ""]
>>> Why are there 2 empty strings at the end? (one per Omega if you vary the
>>>
>> number of such symbols in the string).
>>
>> The above is just weird; is that a bug?
>>
>
> I think it's a bug, it's shown here, I will put it in Bugzilla:
>
> import std.stdio: writeln;
> import std.algorithm: map;
> import std.array: array;
> void main() {
>     string a = "ΩΩab";
>     a.map!(a => "<"d ~ a ~ ">"d).writeln;
>     a.map!(a => "<"d ~ a ~ ">"d).array.writeln;
> }
>
>
> The output shows that maybe it's a problem of array():
>
> ["<Ω>", "<Ω>", "<a>", "<b>"]
>
> ["<Ω>", "<Ω>", "<a>", "<b>", "", ""]
>
>
>
>  For input ranges ranges, I don't understand why the compiler can't accept
>> the foreach(i,ai;a) syntax:
>>
>> it should behave as follows:
>> foreach(i , ai; a){expr}
>>
>> rewritten as:
>> for(size_t i=0, ai=a.front; !a.empty; a.popFront;){expr}
>>
>> but it doesn't compile (it only accepts foreach(ai;a){expr}
>>
>
> The idea must work in all cases. For opApply, built-in arrays, built-in
> associative arrays and for ranges. I think it causes some clashing. If you
> will find it causes no clashing, then it's a candidate to become an
> enhancement request.
>
> Bye,
> bearophile
>

thanks for filing 10191!

Let's dig more the indexed foreach proposal:

opApply => present with corresponding signature to foreach(i,ai;a) => use it
built-in arrays => its an randaccess iterator so already works
associative arrays => already works, compiler rewrites to key,value
ranges => if it's not an associative array, random access iterator, doesn't
have opApply with corresponding signature, then use the size_t index I
mentioned (existing code makes it compile error, this proposal allows it).

Where do you think it can cause clashing?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20130528/a582a019/attachment.html>


More information about the Digitalmars-d-learn mailing list