[Issue 4405] New: all function - returns whether predicate is true for all elements in a range

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jun 28 20:19:46 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4405

           Summary: all function - returns whether predicate is true for
                    all elements in a range
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: jmdavisProg at gmail.com


--- Comment #0 from Jonathan M Davis <jmdavisProg at gmail.com> 2010-06-28 20:19:42 PDT ---
Two very useful functions to have would what could be called any() and all().
They are, in a sense the || and && of a predicate against all elements a range.
any() would take a predicate and a range and return whether any element in that
range satisfied the predicate, shortcutting if it found one. all() would take a
predicate and a range and return whether all elements in that range satisfied
the predicate, shortcutting if it found one which didn't.

std.algorithm currently has canFind(), which is effectively any. One of its
versions takes a predicate and a range and returns whether any of the elements
satisfies the predicate. So, we basically have any() albeit by a different name
and with the original intent of returning whether a particular element could be
found in a range. Its definition is flexible enough that it does the job.

However, we have no function which would be all(). You can get similar behavior
with various functions, but none of them take a predicate and return whether
all elements of a range satisfy the predicate. The closest at present would be
to use the negation of the predicate with canFind(). But not only is that like
having to use || with ! to implement && (which while possible is rather
annoying and often forces you to make your logic less clear), but it's likely
to be less efficient to since you may have to wrap one predicate in another to
get its negation. There may be other implementation details which affect
efficiency as well, though I can't think of any off the top of my head at the
moment.

In any case, it would be highly desirable if an all() function were added to
std.algorithm.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list