digitalmars.D.learn - Appenders and Arrays
- default0 (38/38) Sep 01 2015 Hello
- Steven Schveighoffer (13/47) Sep 01 2015 put into an slice does not append, it fills in the front. This is
- default0 (5/67) Sep 01 2015 Thanks a lot for the clear explanation!
- Daniel Kozak via Digitalmars-d-learn (14/44) Sep 01 2015 So it is something like this?:
- Steven Schveighoffer (3/52) Sep 01 2015 I'm not following your code. What is the question?
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (8/65) Sep 01 2015 code
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"? Further, while the following compiles fine but runs really odd, the following does not compile: int main() { import std.stdio; import std.range; char[] c; c ~= 'a'; c ~= 'b'; c.put('c'); writeln(c); return 0; } C:\dmd\src\phobos\std\range.d(9,9): Error: static assert "Cannot put a char into a char[]." (Test) I am puzzled by 1) Why I cannot put a char into a char[] (even though I can totally append them) 2) Why put removes elements from arrays Hopefully somebody can help me clear my confusion about this (yes in the meantime I can just use Appenders, but you know, still wondering).
Sep 01 2015
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]);Further, while the following compiles fine but runs really odd, the following does not compile: int main() { import std.stdio; import std.range; char[] c; c ~= 'a'; c ~= 'b'; c.put('c'); writeln(c); return 0; } C:\dmd\src\phobos\std\range.d(9,9): Error: static assert "Cannot put a char into a char[]." (Test) I am puzzled by 1) Why I cannot put a char into a char[] (even though I can totally append them)That seems like a bug. put has specific code to deal with putting characters into character ranges. Please file https://issues.dlang.org/enter_bug.cgi -Steve
Sep 01 2015
On Tuesday, 1 September 2015 at 17:20:49 UTC, Steven Schveighoffer wrote:On 9/1/15 12:49 PM, default0 wrote:Thanks a lot for the clear explanation! Issue has been created here: https://issues.dlang.org/show_bug.cgi?id=14998Hello 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]);Further, while the following compiles fine but runs really odd, the following does not compile: int main() { import std.stdio; import std.range; char[] c; c ~= 'a'; c ~= 'b'; c.put('c'); writeln(c); return 0; } C:\dmd\src\phobos\std\range.d(9,9): Error: static assert "Cannot put a char into a char[]." (Test) I am puzzled by 1) Why I cannot put a char into a char[] (even though I can totally append them)That seems like a bug. put has specific code to deal with putting characters into character ranges. Please file https://issues.dlang.org/enter_bug.cgi -Steve
Sep 01 2015
Dne 1.9.2015 v 19:20 Steven Schveighoffer via Digitalmars-d-learn napsal(a):On 9/1/15 12:49 PM, default0 wrote: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; }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]);
Sep 01 2015
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):I'm not following your code. What is the question? -SteveOn 9/1/15 12:49 PM, default0 wrote: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; }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]);
Sep 01 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:codeDne 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 aboveI think Daniel is asking whether .put() on a slice is the equivalent of assigning to front() and then popFront(). The answer is yes. http://ddili.org/ders/d.en/ranges.html#ix_ranges.slice,%20as%20OutputRange AliI'm not following your code. What is the question? -SteveSo 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; }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]);
Sep 01 2015