www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Scriptometer

reply bearophile <bearophileHUGS lycos.com> writes:
This is a comparison, how much good various languages are for scripting
purposes:
http://rigaux.org/language-study/scripting-language/

This test may be good both to see if there is something essential missing in
Phobos2 and to see how D2 is fit for those purposes (the list contains some
implementations in C, C#, etc too).

Bye,
bearophile
Nov 03 2010
next sibling parent reply Kagamin <spam here.lot> writes:
bearophile Wrote:

 This is a comparison, how much good various languages are for scripting
purposes:
 http://rigaux.org/language-study/scripting-language/
 
 This test may be good both to see if there is something essential missing in
Phobos2 and to see how D2 is fit for those purposes (the list contains some
implementations in C, C#, etc too).

I remember llvm's configure script...
Nov 03 2010
parent Kagamin <spam here.lot> writes:
Kagamin Wrote:

 I remember llvm's configure script...

Oh, no, not llvm's, but mingw's.
Nov 03 2010
prev sibling parent reply =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 03-11-2010 o 21:27:00 bearophile <bearophileHUGS lycos.com>  =

napisa=B3(a):

 This is a comparison, how much good various languages are for scriptin=

 purposes:
 http://rigaux.org/language-study/scripting-language/
 This test may be good both to see if there is something essential  =

 missing in Phobos2 and to see how D2 is fit for those purposes (the li=

 contains some implementations in C, C#, etc too).

Nice page. I submitted some "scripts" so D will be featured on it. BTW, I was stuck on "sed in place" -- remove #-comments from a file = (modifying the file, i.e. in place): http://rigaux.org/language-study/scripting-language/#sed_in_place Any idea how to pull it off nicely? -- = Tomek
Nov 03 2010
next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Tomek S.:

 Nice page. I submitted some "scripts" so D will be featured on it.

You may show them here too.
 BTW, I was stuck on "sed in place" -- remove #-comments from a file  
 (modifying the file, i.e. in place):
 http://rigaux.org/language-study/scripting-language/#sed_in_place
 Any idea how to pull it off nicely?

Adding a struct like the Python fileinput to std.stdio of Phobos? http://docs.python.org/library/fileinput.html Bye, bearophile
Nov 03 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
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
Nov 04 2010
parent bearophile <bearophileHUGS lycos.com> writes:
retard:

 I should also mention that you can change all of these languages quite a 
 bit with a special library crafted for this purpose. If you want to use D 
 for bash style scripting, it's very possible to provide a clean set of 
 shortcut methods and use method "chaining" (e.g. directory.findAllMatching
 ("foo").filter("bar").copy("floppy drive") ) 
 
 Since a trivial library would change the results that much, I find this 
 scriptometer quite pointless..

Are those shortcuts currently present in the D standard library? No, they aren't. So it's not a pointless comparison. The purpose of that comparison is to show how much good a language+library is for those scripting purposes. Python is good because it has batteries included. Perl is "good" because it has CPAN. Both languages are usable for those scripting purposes. Bye, bearophile
Nov 04 2010
prev sibling next sibling parent reply Lutger <lutger.blijdestijn gmail.com> writes:
Tomek Sowiński wrote:

 Dnia 03-11-2010 o 21:27:00 bearophile <bearophileHUGS lycos.com>
 napisał(a):
 
 This is a comparison, how much good various languages are for scripting
 purposes:
 http://rigaux.org/language-study/scripting-language/
 This test may be good both to see if there is something essential
 missing in Phobos2 and to see how D2 is fit for those purposes (the list
 contains some implementations in C, C#, etc too).

Nice page. I submitted some "scripts" so D will be featured on it. BTW, I was stuck on "sed in place" -- remove #-comments from a file (modifying the file, i.e. in place): http://rigaux.org/language-study/scripting-language/#sed_in_place Any idea how to pull it off nicely?

Here is a quick attempt: import std.file, std.regex; void main(string[] args) { args[1].write( args[1].readText().replace( regex("#.*", "g"), "") ); }
Nov 03 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Lutger:

 Here is a quick attempt:
 
 import std.file, std.regex;
 
 void main(string[] args)
 {
     args[1].write( args[1].readText().replace( regex("#.*", "g"), "") );
 }

It's cute. Compared to the Python version it doesn't work lazily (by lines). I have tried on Windows that D program and the Python program on a text file that has Unix newlines. The Python program has produced an output with Windows newlines, while the D program has produced an output with Unix newlines. I think Python is more correct here. Bye, bearophile
Nov 03 2010
parent Jesse Phillips <jessekphillips+D gmail.com> writes:
bearophile Wrote:

 Lutger:
 
 Here is a quick attempt:
 
 import std.file, std.regex;
 
 void main(string[] args)
 {
     args[1].write( args[1].readText().replace( regex("#.*", "g"), "") );
 }

It's cute. Compared to the Python version it doesn't work lazily (by lines). I have tried on Windows that D program and the Python program on a text file that has Unix newlines. The Python program has produced an output with Windows newlines, while the D program has produced an output with Unix newlines. I think Python is more correct here. Bye, bearophile

Disagree completely. The task was to remove comment lines, not properly format line endings for the OS running. I don't think laziness has any benefit here.
Nov 03 2010
prev sibling next sibling parent =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
bearophile napisa=B3(a):

 Nice page. I submitted some "scripts" so D will be featured on it.


Good idea. I must say it was fun writing "scripts" in D. They could use = = community sandblasting before publishing. Remember, the aim is to write = = the smallest program possible, so optimize character count (contiguous = whitespaces count as 1). smallest - the smallest running program doing nothing: void main(){} hello world - print a simple string on stdout: import std.stdio; void main(){writeln("Hello World");} argv - access command line parameters (no segmentation fault accepted, n= or = silent exception, so some languages must explicitly check the presence o= f = the argument): import std.stdio; void main(string[] a){if(a.length>1)writeln(a[1]);} env - access environment variable: import std.stdio,std.process; void main(){writeln(getenv("HOME"));} test file exists - return exit code error (non zero) if a file does not = = exist: import std.file; int main(){return !exists("/etc/mtab");} test file readable - return exit code error (non zero) if a file is not = = readable: import std.stdio; void main(){File("/etc/mtab");} formatting - print integers in a simple formatted string: import std.stdio; void main(){int a=3D1,b=3D2;writefln("%s + %s =3D %s",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");} sed in place - remove #-comments from a file (modifying the file, i.e. i= n = place): import std.file,std.regex; void main(string[] a){a[1].write(a[1].readText().replace(regex("#.*", = "g"), ""));} compile what must be - find and compile .c files into .o when the .o is = = old or absent: import std.stdio,std.file,std.process; void main(){ foreach(c;listdir("","*.c")){ auto o=3Dc[0..$-1]~'o'; if(lastModified(o,0)<lastModified(c)) { writefln("compiling %s to %s",c,o); system("gcc -c -o '"~c~"' '"~o~"'"); } } } grep - grep with -F -i -h handling, usage, grep'ing many files: import std.stdio,std.array,std.regex; int main(string[] a){ auto o=3D["-h":0,"-F":0,"-i":0]; while(!(a=3Da[1..$]).empty) { if(auto b=3Da[0] in o) *b=3D1; else break; } if(!a.length||o["-h"]){ writeln("usage: grep [-F] [-i] regexp [files...]"); return 1; } auto e=3Do["-F"]?a[0].replace(regex(r"\W","g"),r"\$&"):a[0]; foreach(f;a[1..$]) foreach(l;File(f).byLine()) if(!l.match(regex(e,o["-i"]?"i":"")).empty) writeln(f,':',l); return 0; } -- = Tomek
Nov 04 2010
prev sibling next sibling parent =?iso-8859-2?B?VG9tZWsgU293afFza2k=?= <just ask.me> writes:
Dnia 04-11-2010 o 22:13:12 bearophile <bearophileHUGS lycos.com>  =

napisa=B3(a):

 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.

 import std.stdio;
 void main(){writeln("Hello World");}

import std.stdio; void main() { writeln("Hello World"); }

I admit I even wanted to use write, not writeln, to save 2 chars. Hell, = = this is WAR; goal justifies means. :-) -- = Tomek
Nov 04 2010
prev sibling next sibling parent retard <re tard.com.invalid> writes:
Thu, 04 Nov 2010 23:09:47 +0100, Tomek Sowiński wrote:

 Dnia 04-11-2010 o 22:13:12 bearophile <bearophileHUGS lycos.com>
 napisał(a):
 
 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.

 import std.stdio;
 void main(){writeln("Hello World");}

import std.stdio; void main() { writeln("Hello World"); }

I admit I even wanted to use write, not writeln, to save 2 chars. Hell, this is WAR; goal justifies means. :-)

FWIW, the other code on the site didn't seem that unreadable. Cheating is not fair. I should also mention that you can change all of these languages quite a bit with a special library crafted for this purpose. If you want to use D for bash style scripting, it's very possible to provide a clean set of shortcut methods and use method "chaining" (e.g. directory.findAllMatching ("foo").filter("bar").copy("floppy drive") ) Since a trivial library would change the results that much, I find this scriptometer quite pointless..
Nov 04 2010
prev sibling next sibling parent retard <re tard.com.invalid> writes:
Thu, 04 Nov 2010 21:32:14 -0400, bearophile wrote:

 retard:
 
 I should also mention that you can change all of these languages quite
 a bit with a special library crafted for this purpose. If you want to
 use D for bash style scripting, it's very possible to provide a clean
 set of shortcut methods and use method "chaining" (e.g.
 directory.findAllMatching ("foo").filter("bar").copy("floppy drive") )
 
 Since a trivial library would change the results that much, I find this
 scriptometer quite pointless..

Are those shortcuts currently present in the D standard library? No, they aren't. So it's not a pointless comparison. The purpose of that comparison is to show how much good a language+library is for those scripting purposes. Python is good because it has batteries included. Perl is "good" because it has CPAN. Both languages are usable for those scripting purposes.

Such a layer is more or less doable in just about any sane language, in very short time. This "meter" shouldn't be the guideline for the standard library if we are discussing a general purpose language such as D. Remember Tangobos - this one is much simpler. This one library can improve the score on that test much more than any specific language feature. Unlike the great language shootout, it's unlikely that many have the motivation to spend the second time studying this.
Nov 04 2010
prev sibling next sibling parent spir <denis.spir gmail.com> writes:
On Thu, 04 Nov 2010 21:52:04 +0100
Tomek Sowi=C5=84ski <just ask.me> wrote:

 Good idea. I must say it was fun writing "scripts" in D. They could use =

 community sandblasting before publishing.

This list is a great source of D idioms for newcomers. Thank you very much = (I'll keep it for reference.) Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 05 2010
prev sibling parent spir <denis.spir gmail.com> writes:
On Thu, 04 Nov 2010 17:13:12 -0400
bearophile <bearophileHUGS lycos.com> wrote:

 Thank you for your work. Even if such little programs look simple and obv=

xamples or in the D wiki, etc=20 +++ Denis -- -- -- -- -- -- -- vit esse estrany =E2=98=A3 spir.wikidot.com
Nov 05 2010