Bug 24048 – Can't sort after filter

Status
NEW
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-07-18T15:16:42Z
Last change time
2024-12-01T16:41:34Z
Assigned to
No Owner
Creator
Grim Maple
Moved to GitHub: phobos#10526 →

Comments

Comment #0 by grimmaple95 — 2023-07-18T15:16:42Z
This code fails to compile ``` auto a = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]; a.filter!(x => x > 2).sort!((x, y) => x > y).writeln; ``` because you can't sort over filter result. Adding `.array` before `.sort` "fixes" the problem. Probably, it should work without the `array` inbetween
Comment #1 by nick — 2023-07-18T17:16:16Z
filter returns an input range. sort requires a random access range and works in place and has good time complexity. How do you want to sort an input range?
Comment #2 by dfj1esp02 — 2023-07-18T18:54:33Z
It would do inputRange.array.sort!predicate, that's how sort usually works on input ranges.
Comment #3 by grimmaple95 — 2023-07-18T19:23:35Z
(In reply to Nick Treleaven from comment #1) > filter returns an input range. > sort requires a random access range and works in place and has good time > complexity. > How do you want to sort an input range? As a user, I shouldn't care about what `filter` returns. I should care about getting a filtered & sorted array/range. As for `sort`, in my user opinion, if it can't do its job "in place", then it could as much do it out-of-place, because this code ``` auto a = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]; a.sort!((x, y) => x > y).writeln; ``` fails to compile just as much, and it's staright up inconvenient
Comment #4 by grimmaple95 — 2023-07-19T07:20:32Z
(In reply to Grim Maple from comment #3) > (In reply to Nick Treleaven from comment #1) > > filter returns an input range. > > sort requires a random access range and works in place and has good time > > complexity. > > How do you want to sort an input range? > > As a user, I shouldn't care about what `filter` returns. I should care about > getting a filtered & sorted array/range. > > As for `sort`, in my user opinion, if it can't do its job "in place", then > it could as much do it out-of-place, because this code > > ``` > auto a = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]; > a.sort!((x, y) => x > y).writeln; > ``` > > fails to compile just as much, and it's staright up inconvenient I meant to change `auto a` to `immutable a`. Then it won't compile, but should
Comment #5 by robert.schadek — 2024-12-01T16:41:34Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10526 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB