February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to ixid | On Thursday, 4 February 2016 at 00:23:07 UTC, ixid wrote: > It would be nice to have a simple writeln that adds spaces automatically like Python's 'print' in std.stdio, perhaps called print. There are many implementations of string interpolation in D (that is what you want, basically). One of them is given in Phillipe's excellent book about templates: https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md#simple-string-interpolation . |
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dejan Lekic | On Thursday, 4 February 2016 at 13:46:46 UTC, Dejan Lekic wrote: > On Thursday, 4 February 2016 at 00:23:07 UTC, ixid wrote: >> It would be nice to have a simple writeln that adds spaces automatically like Python's 'print' in std.stdio, perhaps called print. > > There are many implementations of string interpolation in D (that is what you want, basically). One of them is given in Phillipe's excellent book about templates: https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/D-templates-tutorial.md#simple-string-interpolation . I have written an attempt at it but my point was that a print function would be a good addition to the standard library rather than asking someone to write an implementation for me. string makePrintString(T)(T length) { import std.conv : to; string s = "writeln("; foreach( i; 0 .. length) { s ~= "a[" ~ i.to!string ~ "]"; if(i != length - 1) s ~= ",\" \","; else s ~= ");"; } return s; } void print(A...)(A a) { static if(a.length) { mixin(makePrintString(a.length)); } else writeln; } |
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ola Fosheim Grøstad | On Thursday, 4 February 2016 at 11:04:15 UTC, Ola Fosheim Grøstad wrote:
> On Thursday, 4 February 2016 at 10:59:50 UTC, Mike Parker wrote:
>> IMO, while giving beginner's a helping hand is a great thing, I don't think it's a good basis to use as a design for a standard library.
>
> Yes, better to have a "beginners toolkit" starting-point-codebase and build a tutorial around it.
That would be a reasonable argument if such a thing existed and was included with the compiler.
import newbie;
void main() {
print("Jack", "Black");
}
Unfortunately there is no such thing and it is unlikely to exist in the next decade.
|
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | On Thursday, 4 February 2016 at 14:25:21 UTC, bachmeier wrote: > Unfortunately there is no such thing and it is unlikely to exist in the next decade. There is http://forum.dlang.org/post/mtsd38$16ub$1@digitalmars.com |
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to ixid | On 02/04/16 15:02, ixid via Digitalmars-d-learn wrote: > On Thursday, 4 February 2016 at 13:46:46 UTC, Dejan Lekic wrote: >> On Thursday, 4 February 2016 at 00:23:07 UTC, ixid wrote: >>> It would be nice to have a simple writeln that adds spaces automatically like Python's 'print' in std.stdio, perhaps called print. > I have written an attempt at it but my point was that a print function would be a good addition to the standard library rather than asking someone to write an implementation for me. > > string makePrintString(T)(T length) { > import std.conv : to; > > string s = "writeln("; > foreach( i; 0 .. length) { > s ~= "a[" ~ i.to!string ~ "]"; > if(i != length - 1) > s ~= ",\" \","; > else s ~= ");"; > } > > return s; > } > > void print(A...)(A a) { > static if(a.length) { > mixin(makePrintString(a.length)); > } else writeln; > } void print(A...)(A a) { foreach (N, ref e; a) write(e, N==A.length-1?"\n":" "); } artur |
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to ixid | On Thursday, 4 February 2016 at 00:23:07 UTC, ixid wrote: > It would be nice to have a simple writeln that adds spaces automatically like Python's 'print' in std.stdio, perhaps called print. It seems Andrei decided to add such function: http://forum.dlang.org/thread/n8vr0l$ist$1@digitalmars.com |
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Kagamin | On Thursday, 4 February 2016 at 15:10:18 UTC, Kagamin wrote:
> On Thursday, 4 February 2016 at 14:25:21 UTC, bachmeier wrote:
>> Unfortunately there is no such thing and it is unlikely to exist in the next decade.
>
> There is http://forum.dlang.org/post/mtsd38$16ub$1@digitalmars.com
The important feature is the one that it doesn't offer - it's not part of the default installation. Without that, it doesn't help new users much. It's not even listed on the "Getting Started" for that matter.
|
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
On 02/04/16 16:32, Artur Skawina wrote: > > void print(A...)(A a) { > foreach (N, ref e; a) > write(e, N==A.length-1?"\n":" "); > } BTW, that was *deliberately* written that way as a compromise between efficiency and template bloat. It can of course be done like void print(alias SEP=" ", alias EOL="\n", A...)(A a) { mixin(`write(`~iota(A.length).map!(a=>"a["~text(a)~"],")().join("SEP,")~"EOL);"); } but that seems too expensive, when the use is just in toy programs and debugging. artur |
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | On Thursday, 4 February 2016 at 17:34:33 UTC, Artur Skawina wrote:
> On 02/04/16 16:32, Artur Skawina wrote:
> but that seems too expensive, when the use is just in toy programs and debugging.
I hadn't really considered the relative cost-benefit, it's just a habit to try to hardcode things at compile time. =) It certainly seems to make sense to do it that way.
|
February 04, 2016 Re: print function | ||||
---|---|---|---|---|
| ||||
Posted in reply to ixid | On 02/04/16 18:53, ixid via Digitalmars-d-learn wrote:
> On Thursday, 4 February 2016 at 17:34:33 UTC, Artur Skawina wrote:
>> On 02/04/16 16:32, Artur Skawina wrote:
>> but that seems too expensive, when the use is just in toy programs and debugging.
>
> I hadn't really considered the relative cost-benefit, it's just a habit to try to hardcode things at compile time. =) It certainly seems to make sense to do it that way.
Just to clarify -- *all* versions work at CT; the static-foreach will be unrolled at CT, and the mixin argument will be fully evaluated at CT too. The only difference is in a) readability, b) the `write` template instantiations (and potential re-use).
Using the std lib `write*` templates has a huge cost; for any kind of /real/ programs, that care about performance at all, they are better avoided. (But it probably doesn't matter if you already heavily rely on GC and don't print much, other than that they add tons of code to the executable)
artur
|
Copyright © 1999-2021 by the D Language Foundation