Re: How about appender.put() with var args?
On 16/04/2015 18:25, Nick Treleaven wrote: I think this works: import std.range : only; app.put(only(foo, var, bar)); OK, that doesn't work, and an array doesn't either (which was my assumption): app.put([foo, var, bar]); // NG If it had, I thought only might be more efficient than chain, but I could be wrong.
Re: How about appender.put() with var args?
Márcio Martins: app.put(foo); app.put(var); app.put(bar); I'd like put() to accept a lazy range... Bye, bearophile
Re: How about appender.put() with var args?
On Wednesday, 15 April 2015 at 20:59:25 UTC, Steven Schveighoffer wrote: On 4/15/15 4:51 PM, Messenger wrote: On Wednesday, 15 April 2015 at 19:09:42 UTC, Márcio Martins wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); Sidetracking a bit, but when I started using Appender I was surprised to see that put didn't return a reference to the Appender itself. Had it done so, you could have chained your put calls very nicely. app.put(foo) .put(var) .put(bar) .put(more) .put(stuff); You can naturally write a small wrapper function that does this for you, but it still strikes me as odd. Sadly I imagine changing the return type would make the function signature mangle differently, breaking ABI compatibility. with(app) { put(var); put(bar); put(more); put(stuff); } -Steve With all the excitement about chaining and ufcs, the with statement is often overlooked.
Re: How about appender.put() with var args?
On Wednesday, 15 April 2015 at 20:40:04 UTC, Márcio Martins wrote: I guess chain could work in some cases, and are not that bad on the aesthetic side, I supposed. However having to include std.range just for that, and more importantly, all parameters having to be the same type, as opposed to just being appendable. Creating an array inline is also not an option as I generally don't like to trade aesthetics = efficiency. I think this works: import std.range : only; app.put(only(foo, var, bar)); http://dlang.org/phobos/std_range.html#.only
Re: How about appender.put() with var args?
On Thursday, 16 April 2015 at 03:53:56 UTC, Andrei Alexandrescu wrote: Sidetracking a bit, but when I started using Appender I was surprised to see that put didn't return a reference to the Appender itself. Had it done so, you could have chained your put calls very nicely. app.put(foo) .put(var) .put(bar) .put(more) .put(stuff); You can naturally write a small wrapper function that does this for you, but it still strikes me as odd. Sadly I imagine changing the return type would make the function signature mangle differently, breaking ABI compatibility. Does ~= chain? -- Andrei I'm not sure I understand. Appender!string app; app ~= hello ~= ~= kitty; -- Error: Cannot modify ' ' Is the order of evaluation not such that this becomes app ~= (hello ~= ( ~= kitty))?
Re: How about appender.put() with var args?
On Thu, 16 Apr 2015 21:58:43 +, Messenger wrote: Appender!string app; app ~= hello ~= ~= kitty; -- Error: Cannot modify ' ' Is the order of evaluation not such that this becomes app ~= (hello ~= ( ~= kitty))? yes, assign is right associative. signature.asc Description: PGP signature
Re: How about appender.put() with var args?
On Wednesday, 15 April 2015 at 20:44:07 UTC, Ali Çehreli wrote: On 04/15/2015 12:09 PM, =?UTF-8?B?Ik3DoXJjaW8=?= Martins\ marcio...@gmail.com\ wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); How about this instead? app.put(foo, var, bar); Agreed. If a different name like putAll() is acceptable: void putAll(A, T...)(A a, T items) { foreach (item; items){ a.put(item); } } // ... app.putAll(foo, var, bar); A hypothetical variadic put method would have the advantage over this in that it could could calculate the total length and preallocate for all arguments in one go.
Re: How about appender.put() with var args?
On Wednesday, 15 April 2015 at 19:16:55 UTC, Steven Schveighoffer wrote: On 4/15/15 3:09 PM, =?UTF-8?B?Ik3DoXJjaW8=?= Martins\ marcio...@gmail.com\ wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); How about this instead? app.put(foo, var, bar); This would be consistent with the writeln interface, and it could also reduce the syntax overhead of using appenders vs string concats. Was this overlooked or is there some issue I am not seeing? :) Cheers, -M What about: import std.format; app.formattedWrite(foo%sbar, var); -Steve Well, but wouldn't that incur the cost of parsing the format string, for no benefit?
Re: How about appender.put() with var args?
On 04/15/2015 12:09 PM, =?UTF-8?B?Ik3DoXJjaW8=?= Martins\ marcio...@gmail.com\ wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); How about this instead? app.put(foo, var, bar); Agreed. If a different name like putAll() is acceptable: void putAll(A, T...)(A a, T items) { foreach (item; items){ a.put(item); } } // ... app.putAll(foo, var, bar); Ali
Re: How about appender.put() with var args?
On Wednesday, 15 April 2015 at 19:41:13 UTC, Justin Whear wrote: Appender will take a range, so you can also do: app.put([foo, var, bar]); or app.put(chain(foo, var, bar)); But yes, a variadic put would be convenient so long as it wasn't ambiguous in some way. I guess chain could work in some cases, and are not that bad on the aesthetic side, I supposed. However having to include std.range just for that, and more importantly, all parameters having to be the same type, as opposed to just being appendable. Creating an array inline is also not an option as I generally don't like to trade aesthetics = efficiency.
Re: How about appender.put() with var args?
On Wednesday, 15 April 2015 at 19:09:42 UTC, Márcio Martins wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); Sidetracking a bit, but when I started using Appender I was surprised to see that put didn't return a reference to the Appender itself. Had it done so, you could have chained your put calls very nicely. app.put(foo) .put(var) .put(bar) .put(more) .put(stuff); You can naturally write a small wrapper function that does this for you, but it still strikes me as odd. Sadly I imagine changing the return type would make the function signature mangle differently, breaking ABI compatibility.
Re: How about appender.put() with var args?
Appender will take a range, so you can also do: app.put([foo, var, bar]); or app.put(chain(foo, var, bar)); But yes, a variadic put would be convenient so long as it wasn't ambiguous in some way.
Re: How about appender.put() with var args?
On 4/15/15 3:09 PM, =?UTF-8?B?Ik3DoXJjaW8=?= Martins\ marcio...@gmail.com\ wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); How about this instead? app.put(foo, var, bar); This would be consistent with the writeln interface, and it could also reduce the syntax overhead of using appenders vs string concats. Was this overlooked or is there some issue I am not seeing? :) Cheers, -M What about: import std.format; app.formattedWrite(foo%sbar, var); -Steve
Re: How about appender.put() with var args?
On Wednesday, 15 April 2015 at 20:59:25 UTC, Steven Schveighoffer wrote: with(app) { put(var); put(bar); put(more); put(stuff); } -Steve Awesome.
Re: How about appender.put() with var args?
On Wednesday, 15 April 2015 at 20:44:07 UTC, Ali Çehreli wrote: On 04/15/2015 12:09 PM, =?UTF-8?B?Ik3DoXJjaW8=?= Martins\ marcio...@gmail.com\ wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); How about this instead? app.put(foo, var, bar); Agreed. If a different name like putAll() is acceptable: void putAll(A, T...)(A a, T items) { foreach (item; items){ a.put(item); } } // ... app.putAll(foo, var, bar); Ali This is great! I would still call it just put(), close to writeln()'s interface. It shouldn't break any existing and compiling code, right?
Re: How about appender.put() with var args?
On 4/15/15 1:51 PM, Messenger wrote: On Wednesday, 15 April 2015 at 19:09:42 UTC, Márcio Martins wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); Sidetracking a bit, but when I started using Appender I was surprised to see that put didn't return a reference to the Appender itself. Had it done so, you could have chained your put calls very nicely. app.put(foo) .put(var) .put(bar) .put(more) .put(stuff); You can naturally write a small wrapper function that does this for you, but it still strikes me as odd. Sadly I imagine changing the return type would make the function signature mangle differently, breaking ABI compatibility. Does ~= chain? -- Andrei
Re: How about appender.put() with var args?
On 4/15/15 4:34 PM, =?UTF-8?B?Ik3DoXJjaW8=?= Martins\ marcio...@gmail.com\ wrote: On Wednesday, 15 April 2015 at 19:16:55 UTC, Steven Schveighoffer wrote: What about: import std.format; app.formattedWrite(foo%sbar, var); Well, but wouldn't that incur the cost of parsing the format string, for no benefit? The benefit is, you are not limited to strings :P But yeah, if it's just strings, you have not much benefit. However, I don't know what the cost is for parsing compared to the rest, it may not be too significant. I like Ali's idea, make it a general purpose putAll primitive, goes right in std.range.primitives. -Steve
Re: How about appender.put() with var args?
On 4/15/15 4:51 PM, Messenger wrote: On Wednesday, 15 April 2015 at 19:09:42 UTC, Márcio Martins wrote: Hi! I use Appender a lot, and find it ugly to write this all the time to efficiently construct strings: app.put(foo); app.put(var); app.put(bar); Sidetracking a bit, but when I started using Appender I was surprised to see that put didn't return a reference to the Appender itself. Had it done so, you could have chained your put calls very nicely. app.put(foo) .put(var) .put(bar) .put(more) .put(stuff); You can naturally write a small wrapper function that does this for you, but it still strikes me as odd. Sadly I imagine changing the return type would make the function signature mangle differently, breaking ABI compatibility. with(app) { put(var); put(bar); put(more); put(stuff); } -Steve