www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Output ranges and arrays

reply Olivier Pisano <olivier.pisano laposte.net> writes:
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
next sibling parent "Steven Schveighoffer" <schveiguy yahoo.com> writes:
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
prev sibling parent reply "Lars T. Kyllingstad" <public kyllingen.NOSPAMnet> writes:
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
parent Olivier Pisano <olivier.pisano laposte.net> writes:
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

 -Lars

Thanks to you and Steven. I now understand why such a "weird" behavior. Cheers, Olivier
Nov 12 2010