[Issue 6035] New: std.algorithm.reversed()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 18 18:25:05 PDT 2011


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

           Summary: std.algorithm.reversed()
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-05-18 18:20:56 PDT ---
std.algorithm has a reverse() function:
http://www.digitalmars.com/d/2.0/phobos/std_algorithm.html#reverse

(reverse() returns void, this is correct and right, because it works in-place.
Returning the reversed array is the wrong thing to do.)

reverse duplicates what the array reverse attribute performs:

auto a = [1, 2, 3];
a.reverse;


But in functional-style code I want to create expressions, so I can't use a
function (a procedure) that returns void:

foreach (x; reversed(foo(array1))) {...


And sometimes I don't to work-in place, I need a reversed view of something
that I don't want to modify. So I suggest to introduce a
std.algorithm.reversed() (or std.range.reversed()) function, that lazily yieds
the reversed items, as in Python:

>>> a
['a', 'b', 'c', 'd']
>>> a = [1, 2, 3]
>>> reversed(a)
<listreverseiterator object at 0x01A02910>
>>> list(reversed(a))
[3, 2, 1]
>>> a
[1, 2, 3]


Returning a copy is also useful for immutable arrays, and generally it fits
better in functional-style programming.

See also enhancement issue 5076 that's about sorted(), another function usable
in expressions, that doesn't modify the original iterable.

-- 
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