Bug 8467 – A different third signature for std.algorithm.count

Status
RESOLVED
Resolution
WONTFIX
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-07-29T19:33:00Z
Last change time
2012-07-30T09:09:08Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2012-07-29T19:33:55Z
The third signature of std.algorithm.count() is: http://dlang.org/phobos/std_algorithm.html#count size_t count(alias pred = "true", Range)(Range r); So this program is equivalent to using walkLength, and in my opinion this is not so useful: import std.algorithm: count; void main() { assert(count([0, 0, 1]) == 3); } I think a more useful default for that third form is something similar to: size_t count(alias pred = "a", Range)(Range r); So it counts the true values: import std.algorithm: count; void main() { assert(count([0, 0, 1]) == 1); } But keep in mind that unlike Python in D even empty arrays are sometimes "true", etc: import std.algorithm: count; void main() { assert(count!"a"([(new int[1])[0..0]]) == 1); }
Comment #1 by andrei — 2012-07-30T04:31:15Z
Nice idea, but breaks existing code.
Comment #2 by bearophile_hugs — 2012-07-30T09:09:08Z
(In reply to comment #1) > Nice idea, but breaks existing code. Thank you for the answer. I think around there isn't much code uses that count() with no argument for its template, so not a lot of code is broken by this change. If you are not interested in this enhancement, then I have opened a related enhancement request, Issue 8472