D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 9507 - std.range.transposed behaves poorly with jagged ranges of ranges
Summary: std.range.transposed behaves poorly with jagged ranges of ranges
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2013-02-13 13:32 UTC by hsteoh
Modified: 2015-02-18 03:40 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description hsteoh 2013-02-13 13:32:56 UTC
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 bearophile_hugs 2013-03-03 17:35:46 UTC
(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]]
Comment 3 github-bugzilla 2014-12-06 03:17:37 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/1ddb5010b642ff729e0dcad104b1ee60fb1e9afa
Issue 9507: transposed() behaves poorly with jagged range of ranges

https://github.com/D-Programming-Language/phobos/commit/a58fb70753483eae56cee997aa9befb14ac9b0bb
Merge pull request #2786 from quickfur/issue9507

Issue 9507: transposed() behaves poorly with jagged range of ranges