D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5506 - StoppingPolicy.longest doesn't work well
Summary: StoppingPolicy.longest doesn't work well
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: Andrei Alexandrescu
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2011-01-30 04:59 UTC by bearophile_hugs
Modified: 2011-01-30 21:40 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-01-30 04:59:21 UTC
The purpose of std.range.StoppingPolicy.longest is to change the behaviour of zip to "Stop when the longest range is exhausted". But this code with DMD 2.051 shows it doesn't stop looping (problem found by tsukikage):


import std.c.stdio:printf;
import std.range: zip, StoppingPolicy;
void main() {
    int[] array = [1, 2];
    auto count = 0;
    foreach (p; zip(StoppingPolicy.longest, array))
        if (count++ < 30)
            printf("%d\n", p[0]);
        else
            break;
}


Output:
1
2
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

------------------------------

Additionally a small enhancement request. Instead of writing:
zip(StoppingPolicy.longest, array)

I think this is enough, and shorter, but a bit less clear, so I am not sure if this OK:
zip(Zip.longest, array)
Comment 1 David Nadlinger 2011-01-30 14:42:44 UTC
Oh, sorry, I didn't notice that you already assigned it to yourself, Andrei – I just submitted a patch via a GitHub pull request:

https://github.com/D-Programming-Language/phobos/pull/1
Comment 2 Andrei Alexandrescu 2011-01-30 21:40:53 UTC
(In reply to comment #1)
> Oh, sorry, I didn't notice that you already assigned it to yourself, Andrei – I
> just submitted a patch via a GitHub pull request:
> 
> https://github.com/D-Programming-Language/phobos/pull/1

The pull request is a great way. I just followed it. Thanks very much!