Thread overview
[Issue 11555] New: std.algorithm.reverse should return the just-reversed range
Nov 21, 2013
Chuck Allison
Nov 21, 2013
Chuck Allison
November 19, 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11555

           Summary: std.algorithm.reverse should return the just-reversed
                    range
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: andrei@erdani.com


--- Comment #0 from Andrei Alexandrescu <andrei@erdani.com> 2013-11-19 10:48:14 PST ---
From communication with Chuck Allison:

Bearophile mentioned that .reverse was deprecated. The problem with that is that std.algorithm.reverse doesn’t return anything, breaking code like:

auto compose_n(Fun)(Fun[] funs) pure {
    alias T = ReturnType!Fun;
    return (T x) => reduce!((sofar,f) => f(sofar))(x,funs.dup.reverse);  // <==
uses .reverse
}

I now about std.functional.compose, but that’s not the point. For functional programming, we need values returned for cases like this.

Thoughts?

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 19, 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11555


Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement


-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 19, 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11555


bearophile_hugs@eml.cc changed:

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


--- Comment #1 from bearophile_hugs@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: -------
November 21, 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11555


Chuck Allison <chuck@freshsources.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |chuck@freshsources.com


--- Comment #2 from Chuck Allison <chuck@freshsources.com> 2013-11-20 21:28:59 PST ---
I like the reverse vs. reversed idea from Python. Leave reverse as-is and provide reversed that only returns a new result (no side effects on backing sequence). That way I won't have to do .dup in my example. Win-win.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 21, 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11555



--- Comment #3 from bearophile_hugs@eml.cc 2013-11-21 03:19:53 PST ---
(In reply to comment #2)
> I like the reverse vs. reversed idea from Python. Leave reverse as-is and provide reversed that only returns a new result (no side effects on backing sequence). That way I won't have to do .dup in my example. Win-win.

I prefer reverse() to just return the range despite its uncleanness, or
reverse().release to return it.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 21, 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11555



--- Comment #4 from Chuck Allison <chuck@freshsources.com> 2013-11-21 07:07:10 PST ---
Okay then. If I had a vote, I'd go for your first option.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
November 21, 2013
https://d.puremagic.com/issues/show_bug.cgi?id=11555



--- Comment #5 from Andrei Alexandrescu <andrei@erdani.com> 2013-11-21 10:38:57 PST ---
We can't quite return a new range - this is not the way std.algorithm operates (no allocation, no creation of new ranges).

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