Scriptometer
bearophile
bearophileHUGS at lycos.com
Thu Nov 4 14:13:12 PDT 2010
Tomek S.:
> Remember, the aim is to write
> the smallest program possible, so optimize character count (contiguous
> whitespaces count as 1).
But I suggest to not overdo it. Minimizing char count doesn't justify writing space-free programs. So I suggest to add spaces and newlines where they belong to increase readability a little.
> void main(){}
void main() {}
> import std.stdio;
> void main(){writeln("Hello World");}
import std.stdio;
void main() {
writeln("Hello World");
}
> import std.stdio;
> void main(string[] a){if(a.length>1)writeln(a[1]);}
import std.stdio;
void main(string[] args) {
if (args.length > 1)
writeln(args[1]);
}
And similar formatting/spacing for all the successive examples.
> formatting - print integers in a simple formatted string:
> import std.stdio;
> void main(){int a=1,b=2;writefln("%s + %s = %s",a,b,a+b);}
writeln usage is shorter (see the Python version):
import std.stdio;
void main() {
int a=1, b=2;
writeln(a, " + ", b, " = ", a+b);
}
> system - call an external program and check the return value:
> import std.stdio,std.process;
> void main(){if(system("false")) stderr.writeln("false failed");
> writeln("done");}
The other programs (Python too) use echo, not normal printing, so I suggest (untested):
import std.stdio, std.process;
void main() {
if (system("false"))
stderr.writeln("false failed");
system("echo done");
}
Thank you for your work. Even if such little programs look simple and obvious, they aren't for a D newbie. I may even suggest to put them in the D examples or in the D wiki, etc (in Rosettacode site too I have written a ton of D implementations of many different kinds of programs, usually a little longer).
In my opinion the D home page has to show a nice link to Ideone and codepad because they allow to compile and run D code on the web.
Bye,
bearophile
More information about the Digitalmars-d
mailing list