D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5544 - all() and any() in Phobos
Summary: all() and any() in Phobos
Status: RESOLVED DUPLICATE of issue 4405
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-02-07 14:35 UTC by bearophile_hugs
Modified: 2011-02-07 15:08 UTC (History)
1 user (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-02-07 14:35:37 UTC
This is Python 2.6.6 code (shell):

>>> items = [1, 7, 22]
>>> all(x % 2 for x in items)
False
>>> items = [1, 7, 21]
>>> all(x % 2 for x in items)
True


The functions all() and any() are very handy.

With Phobos of DMD 2.051 if you want to translate that Python code to a D2 functional style you need something like:


import std.algorithm;
void main() {
    auto items = [1, 7, 22];
    bool r1 = reduce!q{a && b}(true, map!q{a % 2}(items));
    assert(!r1);
    items = [1, 7, 21];
    bool r2 = reduce!q{a && b}(true, map!q{a % 2}(items));
    assert(r2);
}


While using an all() it becomes shorter and more readable, something like (untested):

import std.algorithm;
void main() {
    auto items = [1, 7, 22];
    bool r1 = all!q{a % 2}(items);
    assert(!r1);
    items = [1, 7, 21];
    bool r2 = all!q{a % 2}(items);
    assert(r2);
}
Comment 1 Jonathan M Davis 2011-02-07 14:51:11 UTC
Bug#4405
Comment 2 bearophile_hugs 2011-02-07 15:08:53 UTC
Thank you for letting me know.

*** This issue has been marked as a duplicate of issue 4405 ***