Re: put string[] into a appender without loop?
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/
Re: put string[] into a appender without loop?
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 +, 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.
put string[] into a appender without loop?
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?
Re: put string[] into a appender without loop?
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?
Re: put string[] into a appender without loop?
On Sun, 21 Sep 2014 23:41:56 + 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 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? hm... '.put' should work. i.e. string s = `something`; auto ap = appender!string(); ap.put(s); // now ap.data returns `something` signature.asc Description: PGP signature
Re: put string[] into a appender without loop?
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. signature.asc Description: PGP signature
Re: put string[] into a appender without loop?
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.
Re: put string[] into a appender without loop?
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?
Re: put string[] into a appender without loop?
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.
Re: put string[] into a appender without loop?
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.