www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - put string[] into a appender without loop?

reply "AsmMan" <jckj33 gmail.com> writes:
I'd like to copy an array string into a appender!string() but I 
can't see how to do this without loop myself over the string 
array. Is there a native function or should I write it myself?
Sep 21 2014
next sibling parent reply "AsmMan" <jckj33 gmail.com> writes:
On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote:
 I'd like to copy an array string into a appender!string() but I 
 can't see how to do this without loop myself over the string 
 array. Is there a native function or should I write it myself?
call: auto app = appender!string(); string[] s = ["foo", "baa"]; app.put(s); give a: \src\phobos\std\conv.d(9,9): Error: static assert "immutable(char) cannot be emplaced from a string." (b) How do I fix it?
Sep 21 2014
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sunday, 21 September 2014 at 23:48:59 UTC, AsmMan wrote:
 On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote:
 I'd like to copy an array string into a appender!string() but 
 I can't see how to do this without loop myself over the string 
 array. Is there a native function or should I write it myself?
call: auto app = appender!string(); string[] s = ["foo", "baa"]; app.put(s); give a: \src\phobos\std\conv.d(9,9): Error: static assert "immutable(char) cannot be emplaced from a string." (b) How do I fix it?
put(app, s); This is not an ideal solution, since Appender may reallocate several times when appending the array items. Ideally, appender itself should take a range of strings, so that it can preallocate memory for them only once.
Sep 21 2014
parent reply "AsmMan" <jckj33 gmail.com> writes:
On Monday, 22 September 2014 at 00:09:22 UTC, Vladimir Panteleev 
wrote:
 On Sunday, 21 September 2014 at 23:48:59 UTC, AsmMan wrote:
 On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote:
 I'd like to copy an array string into a appender!string() but 
 I can't see how to do this without loop myself over the 
 string array. Is there a native function or should I write it 
 myself?
call: auto app = appender!string(); string[] s = ["foo", "baa"]; app.put(s); give a: \src\phobos\std\conv.d(9,9): Error: static assert "immutable(char) cannot be emplaced from a string." (b) How do I fix it?
put(app, s); This is not an ideal solution, since Appender may reallocate several times when appending the array items. Ideally, appender itself should take a range of strings, so that it can preallocate memory for them only once.
this give undefined identifier: 'put' error. (std.array is already included, buffer.put(string) doesn't give same error) The copy of an array doesn't happen often as string but do suggest to I want something else instead of appender?
Sep 21 2014
parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Monday, 22 September 2014 at 00:18:03 UTC, AsmMan wrote:
 this give undefined identifier: 'put' error. (std.array is 
 already included, buffer.put(string) doesn't give same error)
You need to import std.range.
 The copy of an array doesn't happen often as string but do 
 suggest to I want something else instead of appender?
No, I was just thinking aloud about how Appender could be improved in the future. To minimize allocations right now, you could write your own put-like function which calls Appender.reserve.
Sep 21 2014
parent "AsmMan" <jckj33 gmail.com> writes:
On Monday, 22 September 2014 at 00:30:44 UTC, Vladimir Panteleev 
wrote:
 On Monday, 22 September 2014 at 00:18:03 UTC, AsmMan wrote:
 this give undefined identifier: 'put' error. (std.array is 
 already included, buffer.put(string) doesn't give same error)
You need to import std.range.
Thanks, I was thinking std.array is enough
 The copy of an array doesn't happen often as string but do 
 suggest to I want something else instead of appender?
No, I was just thinking aloud about how Appender could be improved in the future. To minimize allocations right now, you could write your own put-like function which calls Appender.reserve.
this is not critical (for now, I'm not doing any optmization) but I'll save the idea.
Sep 21 2014
prev sibling next sibling parent reply "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Sun, Sep 21, 2014 at 11:41:56PM +0000, AsmMan via Digitalmars-d-learn wrote:
 I'd like to copy an array string into a appender!string() but I can't
 see how to do this without loop myself over the string array. Is there
 a native function or should I write it myself?
Try this: import std.array : appender; import std.algorithm : joiner, copy; string[] arr = ["ab", "cd", "efg"]; auto app = appender!string(); arr.joiner.copy(app); assert(app.data == "abcdefg"); T -- One reason that few people are aware there are programs running the internet is that they never crash in any significant way: the free software underlying the internet is reliable to the point of invisibility. -- Glyn Moody, from the article "Giving it all away"
Sep 21 2014
parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Sunday, 21 September 2014 at 23:50:50 UTC, H. S. Teoh via 
Digitalmars-d-learn wrote:
 On Sun, Sep 21, 2014 at 11:41:56PM +0000, AsmMan via 
 Digitalmars-d-learn wrote:
 I'd like to copy an array string into a appender!string() but 
 I can't
 see how to do this without loop myself over the string array. 
 Is there
 a native function or should I write it myself?
Try this: import std.array : appender; import std.algorithm : joiner, copy; string[] arr = ["ab", "cd", "efg"]; auto app = appender!string(); arr.joiner.copy(app); assert(app.data == "abcdefg"); T
FYI, that's probably scary expensive in terms of encoding/decoding. Using the "free" std.range.put should take care of everything, natively.
Sep 22 2014
prev sibling next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Sun, 21 Sep 2014 23:41:56 +0000
AsmMan via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

 I'd like to copy an array string into a appender!string() but I=20
 can't see how to do this without loop myself over the string=20
 array. Is there a native function or should I write it myself?
hm... '.put' should work. i.e. string s =3D `something`; auto ap =3D appender!string(); ap.put(s); // now ap.data returns `something`
Sep 21 2014
prev sibling next sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On Mon, 22 Sep 2014 02:51:41 +0300
ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com>
wrote:

sorry, forgot that. i misunderstood your question.
Sep 21 2014
prev sibling parent "kiran kumari" <kiranfabzen gmail.com> writes:
On Sunday, 21 September 2014 at 23:41:58 UTC, AsmMan wrote:
 I'd like to copy an array string into a appender!string() but I 
 can't see how to do this without loop myself over the string 
 array. Is there a native function or should I write it myself?
see more example http://techgurulab.com/course/java-quiz-online/
Sep 24 2014