Mgr (Diskussion | Beiträge) (Wiederhergestellt zur letzten Änderung von Mgr) |
Mgr (Diskussion | Beiträge) K (Wiederhergestellt zur letzten Änderung von Neingeist) |
||
| Zeile 73: | Zeile 73: | ||
</pre> | </pre> | ||
Nu aber wirklich noch in Perl: | |||
<pre> | |||
#!/usr/bin/perl | |||
@flo = ( "dividuum", "syb", "fiji", "flowhase" ); | |||
for $i (0..$#flo) { | |||
eval "\$flo$i = \$flo\[$i\]"; | |||
print "\$flo$i eq \"" . eval("\$flo$i") . "\"\n"; | |||
} | |||
</pre> | |||
Gute alte Bash: | |||
<pre> | |||
#!/bin/sh | |||
flo="dividuum syb fiji flowhase" | |||
n=0 | |||
for f in $flo | |||
do | |||
eval flo$n=$f | |||
echo -n "flo$n == " | |||
eval echo \$flo$n | |||
n=$(($n+1)) | |||
done | |||
</pre> | |||
Realisiert in PHP, Lisp, Perl, XML, Bash und XSLT und immernoch kürzer als die C-Version: | |||
{| cellpadding=5 | |||
| | |||
<pre> | |||
<?php | |||
$flo = array( "dividuum", "syb", "fiji", "flowhase" ); | |||
$l = fopen("flo.lisp", "w"); $p = fopen("flo.pl", "w"); | |||
fwrite($l, '(progn (format t "~a" "<flo>")'); | |||
fwrite($p,' | |||
print <<EOF | |||
<xsl:stylesheet version="1.0" | |||
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |||
xmlns="http://www.w3.org/TR/xhtml1/strict"> | |||
<xsl:template match="/">'); | |||
for($i=0;$i<count($flo);$i++) { | |||
fwrite($l, "(format t \"~a~a~a\" \"<flo$i>\" \"$flo[$i]\" \"</flo$i>\")"); | |||
fwrite($p, "flo$i == <xsl:value-of select=\"flo/flo$i\"/>\n"); | |||
} | |||
fwrite($l, '(format t "~a" "</flo>"))'); | |||
fwrite($p,' | |||
</xsl:template> | |||
</xsl:stylesheet> | |||
EOF'."\n"); | |||
fclose($l); fclose($p); | |||
system("sbcl --noinform --load flo.lisp --eval '(quit)' > flo.xml"); | |||
system("perl flo.pl > flo.xslt"); | |||
system("xsltproc flo.xslt flo.xml"); | |||
?> | |||
</pre> | |||
| | |||
| | |||
http://static.php.net/www.php.net/images/news/php-logo.gif | |||
[[Bild:Lisplogo_warning_128.png]] | |||
http://www.mail-archive.com/copenhagen@perl.org/msg00162/logo_mermaid.png | |||
|} | |||
== Bloatiger Angebercode, der auch noch etwas völlig anderes macht == | == Bloatiger Angebercode, der auch noch etwas völlig anderes macht == | ||
Version vom 4. November 2005, 10:46 Uhr
Beiträge, die die Anforderungen erfüllen
#!/usr/bin/env ruby
flo = [ "dividuum", "syb", "fiji", "flowhase" ]
# flos sind keine arrayss1!
for i in 0..flo.size-1
eval "flo#{i} = flo[#{i}]"
puts "flo#{i} == \"" + eval("flo#{i}") + "\""
end
Dieses Perl (*duck*) kann ja keiner lesen!1) Daher:
Dreckig:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
const int flolen = 4;
static char *flos[] = {"dividuum","syb","fiji","flowhase"};
static char *proghead = "#include <stdio.h>\n"
"int main(void){\n";
static char *progend = " return(0);\n"
"}\n";
int main(void){
int i;
FILE *o;
o=fopen("flo2.c","w");
fputs(proghead,o);
for(i=0;i<flolen;i++){
fprintf(o," char *flo%d = \"%s\";\n",i,flos[i]);
fprintf(o," printf(\"flo%%d ist %%s\\n\",%d,flo%d);\n",i,i);
}
fputs(progend,o);
fclose(o);
system("/usr/bin/gcc -o flos flo2.c;./flos;rm -f flo2.c flos");
return(0);
}
Nu aber wirklich noch in Perl:
#!/usr/bin/perl
@flo = ( "dividuum", "syb", "fiji", "flowhase" );
for $i (0..$#flo) {
eval "\$flo$i = \$flo\[$i\]";
print "\$flo$i eq \"" . eval("\$flo$i") . "\"\n";
}
Gute alte Bash:
#!/bin/sh flo="dividuum syb fiji flowhase" n=0 for f in $flo do eval flo$n=$f echo -n "flo$n == " eval echo \$flo$n n=$(($n+1)) done
Realisiert in PHP, Lisp, Perl, XML, Bash und XSLT und immernoch kürzer als die C-Version:
<?php
$flo = array( "dividuum", "syb", "fiji", "flowhase" );
$l = fopen("flo.lisp", "w"); $p = fopen("flo.pl", "w");
fwrite($l, '(progn (format t "~a" "<flo>")');
fwrite($p,'
print <<EOF
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns="http://www.w3.org/TR/xhtml1/strict">
<xsl:template match="/">');
for($i=0;$i<count($flo);$i++) {
fwrite($l, "(format t \"~a~a~a\" \"<flo$i>\" \"$flo[$i]\" \"</flo$i>\")");
fwrite($p, "flo$i == <xsl:value-of select=\"flo/flo$i\"/>\n");
}
fwrite($l, '(format t "~a" "</flo>"))');
fwrite($p,'
</xsl:template>
</xsl:stylesheet>
EOF'."\n");
fclose($l); fclose($p);
system("sbcl --noinform --load flo.lisp --eval '(quit)' > flo.xml");
system("perl flo.pl > flo.xslt");
system("xsltproc flo.xslt flo.xml");
?>
|
http://static.php.net/www.php.net/images/news/php-logo.gif
|
Bloatiger Angebercode, der auch noch etwas völlig anderes macht
Und nun nochmal Ruby mit viel bloatigem Metaprogramming-Foo (Und etwas völlig anderes machend. Anm. v. mgr):
#!/usr/bin/env ruby
class Flo
def self.metaclass; class << self; self; end; end
def self.traits(*arr)
return @traits if arr.empty?
attr_accessor *arr
arr.each do |a|
metaclass.instance_eval do
define_method( a ) do |val|
@traits ||= {}
@traits[a] = val
end
end
end
class_eval do
define_method( :initialize ) do
self.class.traits.each do |k,v|
instance_variable_set("@#{k}", v)
end
end
end
end
traits :name, :skill, :notebook
def to_s
s = "#{self.class} is actually #{self.name} and is good at #{self.skill}\n\tHe loves hacking on his #{self.notebook}"
end
end
class Flo0 < Flo
name "dividuum"
skill "DOOM"
notebook "old Dell"
end
class Flo1 < Flo
name "Peter"
skill "math"
notebook "IBM Thinkpad"
end
class Flo2 < Flo
name "Fiji"
skill "WOW"
notebook "i have no idea"
end
class Flo3 < Flo
name "flowhase"
skill "hoppeln"
notebook "hasIbook"
end
a = [ Flo0.new, Flo1.new, Flo2.new, Flo3.new ]
a.each { |f| puts f }
Und nochmal das, was das Ruby-Programm "mit viel bloatigem Metaprogramming-Foo" macht, allerdings wiederum in Common Lisp gegossen:
(Warnung: (Zumindest) als Lispprogramm ist dieses Beispiel ziemlich unsinning, und z.B. die PRINT-OBJECT-Methode widerspricht der gängigen Konvention.)
Fußnoten
1) Anmerkung zu: "Dieses Perl (*duck*) kann ja keiner lesen!"
Stimmt doch gar nicht! Ist ganz einfach:
map printf("%s = %s\n", [flo0..flo3]->[$_], [dividuum, syb, fiji, flowhase]->[$_]), 0..$#{@{[flo0..flo3]}};
(Kommentar von mgr: Genau, "0..$#{@{[flo0..flo3]}}" ... q.e.d. Danke für das gute Beispiel. Aber ernsthaft, es ging hier eben gerade *nicht* um Einzeiler, die will niemand.)
Und auch in python kann man Einzeiler schreiben:
print "\n".join(["%s = %s" % (k, v) for k, v in {"flo0":"dividuum", "flo1":"syb", "flo2":"fiji", "flo3":"flowhase"}.items()])

