digitalmars.D - Output ranges and arrays
- Olivier Pisano (21/21) Nov 12 2010 Hi,
- Steven Schveighoffer (8/27) Nov 12 2010 Expected. If you want an appendable array as an output range, use
- Lars T. Kyllingstad (5/32) Nov 12 2010 Here's a discussion from earlier this year:
- Olivier Pisano (5/9) Nov 12 2010 Thanks to you and Steven.
Hi, I am starting to play with output ranges and have trouble understanding how they do work on arrays. Consider the following code : import std.array; import std.range; import std.stdio; void main(string[] argv) { auto a = [1, 2, 3]; a.put(4); writefln("%s", a); } One could expect the call to put() to append 4 to the array so the array content would be [1, 2, 3, 4]. Instead of this, I get "[2, 3]" to be printed. So I guess put() is translated to r.front = e; r.popFront(); as written in std.range.put documentation. Is it the expected behaviour or is it a bug ? Cheers, Olivier.
Nov 12 2010
On Fri, 12 Nov 2010 09:22:20 -0500, Olivier Pisano <olivier.pisano laposte.net> wrote:Hi, I am starting to play with output ranges and have trouble understanding how they do work on arrays. Consider the following code : import std.array; import std.range; import std.stdio; void main(string[] argv) { auto a = [1, 2, 3]; a.put(4); writefln("%s", a); } One could expect the call to put() to append 4 to the array so the array content would be [1, 2, 3, 4]. Instead of this, I get "[2, 3]" to be printed. So I guess put() is translated to r.front = e; r.popFront(); as written in std.range.put documentation. Is it the expected behaviour or is it a bug ?Expected. If you want an appendable array as an output range, use std.array.Appender. auto a = appender([1,2,3]); a.put(4); writefln("%s", a.data); -Steve
Nov 12 2010
On Fri, 12 Nov 2010 15:22:20 +0100, Olivier Pisano wrote:Hi, I am starting to play with output ranges and have trouble understanding how they do work on arrays. Consider the following code : import std.array; import std.range; import std.stdio; void main(string[] argv) { auto a = [1, 2, 3]; a.put(4); writefln("%s", a); } One could expect the call to put() to append 4 to the array so the array content would be [1, 2, 3, 4]. Instead of this, I get "[2, 3]" to be printed. So I guess put() is translated to r.front = e; r.popFront(); as written in std.range.put documentation. Is it the expected behaviour or is it a bug ?Here's a discussion from earlier this year: http://www.digitalmars.com/d/archives/digitalmars/D/ std.array.put_doesn_t_put_106871.html -Lars
Nov 12 2010
Le 12/11/2010 15:40, Lars T. Kyllingstad a écrit :Here's a discussion from earlier this year: http://www.digitalmars.com/d/archives/digitalmars/D/ std.array.put_doesn_t_put_106871.html -LarsThanks to you and Steven. I now understand why such a "weird" behavior. Cheers, Olivier
Nov 12 2010