Summary: std.algorithm.reversed()
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos

--- Comment #0 from 2011-05-18 18:20:56 PDT ---
std.algorithm has a reverse() function:

(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];

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:
------- You are receiving this mail because: -------

Reply via email to