Bug 11555 – std.algorithm.reverse should return the just-reversed range

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-11-19T10:48:14Z
Last change time
2018-02-27T16:37:20Z
Keywords
preapproved, pull
Assigned to
No Owner
Creator
Andrei Alexandrescu

Comments

Comment #0 by andrei — 2013-11-19T10:48:14Z
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?
Comment #1 by bearophile_hugs — 2013-11-19T11:17:09Z
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
Comment #2 by chuck — 2013-11-20T21:28:59Z
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.
Comment #3 by bearophile_hugs — 2013-11-21T03:19:53Z
(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.
Comment #4 by chuck — 2013-11-21T07:07:10Z
Okay then. If I had a vote, I'd go for your first option.
Comment #5 by andrei — 2013-11-21T10:38:57Z
We can't quite return a new range - this is not the way std.algorithm operates (no allocation, no creation of new ranges).
Comment #6 by greensunny12 — 2018-02-10T22:31:11Z
What's the status of this? The way I see it: 1) There's now retro 2) reverse should have been in std.array 3) The naming is bad (reverse and reversed would have been better) But I doubt we can change any of this, except for moving reverse to std.array and doing a public import in std.algorithm. Thoughts?
Comment #7 by andrei — 2018-02-11T15:40:15Z
Retro changes the type and has a different charter. I think the current placement is fine because reverse works on bidir ranges, not just arrays. Name is in keeping with C++ and other languages. Let's just have it return the reversed range, it helps with composition.
Comment #8 by greensunny12 — 2018-02-11T15:50:44Z
> Let's just have it return the reversed range, it helps with composition. Easy enough: https://github.com/dlang/phobos/pull/6162
Comment #9 by github-bugzilla — 2018-02-11T17:58:32Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/3acaa9d53ac9d89e2f403ee13f865765f79149cd Fix Issue 11555 - std.algorithm.reverse should return the just-reversed range https://github.com/dlang/phobos/commit/2645dba46c4bb71c66868b6e12af1634af8f053a Merge pull request #6162 from wilzbach/fix-11555 Fix Issue 11555 - std.algorithm.reverse should return the just-reversed range merged-on-behalf-of: Andrei Alexandrescu <[email protected]>