[Issue 11555] std.algorithm.reverse should return the just-reversed range

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Nov 19 11:17:12 PST 2013


https://d.puremagic.com/issues/show_bug.cgi?id=11555


bearophile_hugs at eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs at eml.cc


--- Comment #1 from bearophile_hugs at eml.cc 2013-11-19 11:17:09 PST ---
In functional programming you usually don't mutate values, so a similar
function just returns reversed items, without touching the original data. In
Phobos this is done by retro(). D is not just a functional language, and often
in D you need to reverse items in-place. So we have Phobos reverse() that does
that.

In Python procedures that work in-place return None (like a void), and
functions that return a modified copy return it. So In Python we have sort()
and sorted(), the first returns None and sorts in place, while the seconds
created a new sorted list, leaving the original iterable untouched, and returns
it.

In D we have something intermediate, where sort() works in-pace, but it returns
a special sorted range, and you need release to get the original data sorted.
That range is useful for binary seach but it's also useful to remember sort
works in-place.

The D built-ins sort and reverse work in place and also return the data.

Having a reversed that reverses and also returns the iterable is very handy in
UCSF chains, I have needed it several times.

So I don't see a perfect solution to such contrasting needs.

A possible solution is to just to design reverse that works as the built-in one
and returns the reversed data. And accept the little design wart.

But perhaps a little better design is to do as sort() (the following reverse
works in-place):

[1, 2, 3].reverse().release.writeln;


See also Issue 7666

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


More information about the Digitalmars-d-bugs mailing list