Bug 9507 – std.range.transposed behaves poorly with jagged ranges of ranges
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-02-13T13:32:00Z
Last change time
2015-02-18T03:40:31Z
Keywords
pull
Assigned to
nobody
Creator
hsteoh
Comments
Comment #0 by hsteoh — 2013-02-13T13:32:56Z
If you pass a jagged range of ranges to std.range.transposed, once one of the subranges is consumed, .front will cause an invalid access to the empty subrange's .front, causing a runtime error.
However, the returned range's .empty will still be false, even though you can't safely use .front anymore!
So std.range.transposed should either return empty if *any* of its ranges are empty, or else, its .front should be modified so that empty subranges will be skipped over (or should there be an option to specify a default element?).
Comment #1 by bearophile_hugs — 2013-03-03T17:35:46Z
(In reply to comment #0)
> If you pass a jagged range of ranges to std.range.transposed, once one of the
> subranges is consumed, .front will cause an invalid access to the empty
> subrange's .front, causing a runtime error.
>
> However, the returned range's .empty will still be false, even though you can't
> safely use .front anymore!
>
> So std.range.transposed should either return empty if *any* of its ranges are
> empty, or else, its .front should be modified so that empty subranges will be
> skipped over (or should there be an option to specify a default element?).
This shows some usages the stadard Haskell function transpose, I'd like the D version to do something similar. No need for a default element:
Prelude> import Data.List (transpose)
Prelude Data.List> transpose [[1,2,3],[4,5,6],[7,8,9]]
[[1,4,7],[2,5,8],[3,6,9]]
Prelude Data.List> let a = [[7,4,2,8,7],[8,0,8],[3],[1,6,0,7,7,2,0],[8,9,3,1],[6],[6]]
Prelude Data.List> transpose a
[[7,8,3,1,8,6,6],[4,0,6,9],[2,8,0,3],[8,7,1],[7,7],[2],[0]]
Prelude Data.List> transpose [[1],[],[3,4]]
[[1,3],[4]]