Appenders and Arrays

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 1 22:58:51 PDT 2015


On 09/01/2015 02:16 PM, Steven Schveighoffer wrote:> On 9/1/15 3:13 PM, 
Daniel Kozak via Digitalmars-d-learn wrote:
 >>
 >>
 >> Dne 1.9.2015 v 19:20 Steven Schveighoffer via Digitalmars-d-learn
 >> napsal(a):
 >>> On 9/1/15 12:49 PM, default0 wrote:
 >>>> Hello
 >>>>
 >>>> A simple thing I stumbled across:
 >>>>
 >>>> int main()
 >>>> {
 >>>>      import std.stdio;
 >>>>      import std.range;
 >>>>
 >>>>      int[] d;
 >>>>      d ~= 10;
 >>>>      d ~= 20;
 >>>>      d.put(5);
 >>>>      writeln(d);
 >>>>
 >>>>      return 0;
 >>>> }
 >>>>
 >>>> Appenders work fine as output ranges, but arrays do not. The above 
code
 >>>> prints "20" (ie the 10 is removed). Is "put" not supposed to mean
 >>>> "append one element"?
 >>>
 >>> put into an slice does not append, it fills in the front. This is
 >>> because the "target" of a slice is the data it points at.
 >>>
 >>> Think of it as a buffer that you want to fill:
 >>>
 >>> int[20] buf;
 >>> int[] outputRange = buf[];
 >>> outputRange.put(10);
 >>> outputRange.put(20);
 >>>
 >>> assert(buf[0..2] == [10,20]);
 >> So it is something like this?:
 >>
 >> int main()
 >> {
 >>      import std.stdio;
 >>      import std.range;
 >>
 >>      int[] d;
 >>      d ~= [10];
 >>      d ~= [20];
 >>      d.front = 5;
 >>      d.popFront();
 >>      writeln(d);
 >>
 >>      return 0;
 >> }
 >>
 >
 > I'm not following your code. What is the question?
 >
 > -Steve

I think Daniel is asking whether .put() on a slice is the equivalent of 
assigning to front() and then popFront(). The answer is yes.

http://dlang.org/phobos/std_range_primitives.html#.put

http://ddili.org/ders/d.en/ranges.html#ix_ranges.slice,%20as%20OutputRange

Ali



More information about the Digitalmars-d-learn mailing list