(flœ) |
Keine Bearbeitungszusammenfassung |
||
Zeile 13: | Zeile 13: | ||
#!/usr/bin/env ruby | #!/usr/bin/env ruby | ||
flo = [ "dividuum", "syb", "fiji", "flowhase", "florolf" ] | flo = [ "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ] | ||
# flos sind keine arrayss1! | # flos sind keine arrayss1! | ||
Zeile 27: | Zeile 27: | ||
|<pre> | |<pre> | ||
(eval-when (:compile-toplevel :load-toplevel :execute) | (eval-when (:compile-toplevel :load-toplevel :execute) | ||
(defvar *flo-sequence* #("dividuum" "syb" "fiji" "flowhase" "florolf"))) | (defvar *flo-sequence* #("dividuum" "syb" "fiji" "flowhase" "florolf" "piratenflo"))) | ||
(defmacro make-individual-flo-variables () | (defmacro make-individual-flo-variables () | ||
Zeile 50: | Zeile 50: | ||
#include <errno.h> | #include <errno.h> | ||
#include <unistd.h> | #include <unistd.h> | ||
const int flolen = | const int flolen = 6; | ||
static const char *flos[] = {"dividuum","syb","fiji","flowhase","florolf"}; | static const char *flos[] = {"dividuum","syb","fiji","flowhase","florolf", "piratenflo"}; | ||
static const char *proghead = "#include <stdio.h>\nint main(void){\n"; | static const char *proghead = "#include <stdio.h>\nint main(void){\n"; | ||
static const char *progend = " return(0);\n}\n"; | static const char *progend = " return(0);\n}\n"; | ||
Zeile 74: | Zeile 74: | ||
#!/usr/bin/perl | #!/usr/bin/perl | ||
@flo = ( "dividuum", "syb", "fiji", "flowhase", "florolf" ); | @flo = ( "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ); | ||
for $i (0..$#flo) { | for $i (0..$#flo) { | ||
Zeile 87: | Zeile 87: | ||
#!/usr/bin/perl | #!/usr/bin/perl | ||
@flo = ( "dividuum", "syb", "fiji", "flowhase", "florolf" ); | @flo = ( "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ); | ||
for $i (0..$#flo) { | for $i (0..$#flo) { | ||
Zeile 99: | Zeile 99: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$flo = array( "dividuum", "syb", "fiji", "flowhase", "florolf" ); | $flo = array( "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ); | ||
foreach ($flo as $i => $x) { | foreach ($flo as $i => $x) { | ||
Zeile 114: | Zeile 114: | ||
#!/bin/sh | #!/bin/sh | ||
flo="dividuum syb fiji flowhase florolf" | flo="dividuum syb fiji flowhase florolf piratenflo" | ||
n=0 | n=0 | ||
Zeile 133: | Zeile 133: | ||
<pre> | <pre> | ||
<?php | <?php | ||
$flo = array( "dividuum", "syb", "fiji", "flowhase", "florolf" ); | $flo = array( "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ); | ||
$l = fopen("flo.lisp", "w"); $p = fopen("flo.pl", "w"); | $l = fopen("flo.lisp", "w"); $p = fopen("flo.pl", "w"); | ||
Zeile 171: | Zeile 171: | ||
#!/usr/bin/env ruby | #!/usr/bin/env ruby | ||
flo = [ "dividuum", "syb", "fiji", "flowhase", "florolf" ] | flo = [ "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ] | ||
# flos sind keine arrayss1! | # flos sind keine arrayss1! | ||
Zeile 182: | Zeile 182: | ||
'cause JavaScript rocks: | 'cause JavaScript rocks: | ||
<pre> | <pre> | ||
flo = new Array("dividuum", "syb", "fiji", "flowhase", "florolf"); | flo = new Array("dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo"); | ||
for (i in flo) { | for (i in flo) { | ||
eval("flo"+i+" = flo["+i+"]"); | eval("flo"+i+" = flo["+i+"]"); | ||
Zeile 197: | Zeile 197: | ||
lua_State *L; | lua_State *L; | ||
char *flos[] = {"dividuum", "syb", "fiji", "flowhase", "florolf"}; | char *flos[] = {"dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo"}; | ||
char buf[1024]; | char buf[1024]; | ||
Zeile 207: | Zeile 207: | ||
luaL_openlibs(L); | luaL_openlibs(L); | ||
for(i=0; i < | for(i=0; i < 6; i++) { | ||
sprintf(buf, "flo%d", i); | sprintf(buf, "flo%d", i); | ||
lua_pushstring(L, flos[i]); | lua_pushstring(L, flos[i]); | ||
Zeile 214: | Zeile 214: | ||
p=buf; | p=buf; | ||
for(i=0; i < | for(i=0; i < 6; i++) | ||
p += sprintf(p, "print(\"flo%d = \" .. flo%d);", i, i); | p += sprintf(p, "print(\"flo%d = \" .. flo%d);", i, i); | ||
Zeile 279: | Zeile 279: | ||
:skill "konfusion" | :skill "konfusion" | ||
:notebook "Thinkpad") | :notebook "Thinkpad") | ||
(def-flo-class |Flo5| | |||
:name "piratenflo" | |||
:skill "pirat" | |||
:notebook "I-have-no-idea") | |||
(let ((flos (list (make-instance '|Flo0|) | (let ((flos (list (make-instance '|Flo0|) | ||
Zeile 284: | Zeile 290: | ||
(make-instance '|Flo2|) | (make-instance '|Flo2|) | ||
(make-instance '|Flo3|) | (make-instance '|Flo3|) | ||
(make-instance '|Flo4|)))) | (make-instance '|Flo4|) | ||
(make-instance '|Flo5|)))) | |||
(dolist (flo flos) | (dolist (flo flos) | ||
(print flo))) | (print flo))) | ||
Zeile 299: | Zeile 306: | ||
VALUE flo_init(VALUE self) | VALUE flo_init(VALUE self) | ||
{ | { | ||
char *flo[] = {"dividuum", "syb", "fiji", "flowhase", "florolf"}; | char *flo[] = {"dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo"}; | ||
int i; | int i; | ||
for(i = 0; i < | for(i = 0; i < 6; i++) | ||
{ | { | ||
char varname[ | char varname[7]; | ||
sprintf(varname, "@flo%i", i); // Insecure! | sprintf(varname, "@flo%i", i); // Insecure! | ||
rb_iv_set(self, varname, rb_str_new2(flo[i])); | rb_iv_set(self, varname, rb_str_new2(flo[i])); | ||
Zeile 347: | Zeile 354: | ||
Stimmt doch gar nicht! Ist ganz einfach: | Stimmt doch gar nicht! Ist ganz einfach: | ||
<pre> | <pre> | ||
map printf("%s = %s\n", [flo0.. | map printf("%s = %s\n", [flo0..flo5]->[$_], [dividuum, syb, fiji, flowhase, florolf, piratenflo]->[$_]), 0..$#{@{[flo0..flo5]}}; | ||
</pre> | </pre> | ||
(Kommentar von [[Benutzer:Mgr|mgr]]: Genau, "0..$#{@{[flo0.. | (Kommentar von [[Benutzer:Mgr|mgr]]: Genau, "0..$#{@{[flo0..flo5]}}" ... q.e.d. Danke für das gute Beispiel. | ||
Aber ernsthaft, es ging hier eben gerade *nicht* um Einzeiler, die will niemand.) | Aber ernsthaft, es ging hier eben gerade *nicht* um Einzeiler, die will niemand.) | ||
Zeile 355: | Zeile 362: | ||
Und auch in python kann man Einzeiler schreiben: | Und auch in python kann man Einzeiler schreiben: | ||
<pre> | <pre> | ||
print "\n".join(["%s = %s" % (k, v) for k, v in {"flo0":"dividuum", "flo1":"syb", "flo2":"fiji", "flo3":"flowhase","flo4":"florolf"}.items()]) | print "\n".join(["%s = %s" % (k, v) for k, v in {"flo0":"dividuum", "flo1":"syb", "flo2":"fiji", "flo3":"flowhase","flo4":"florolf", "flo5":"piratenflo"}.items()]) | ||
</pre> | </pre> | ||
[[Kategorie:Wissen]] | [[Kategorie:Wissen]] | ||
[[Kategorie:Entropia]] | [[Kategorie:Entropia]] |
Version vom 7. Februar 2012, 15:19 Uhr
Wir sind die Flo, Widerstand ist zwecklos!
Beiträge, die die Anforderungen erfüllen
#!/usr/bin/env ruby flo = [ "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ] # 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:
Dreckiges C:
#include <stdio.h> #include <stdlib.h> #include <errno.h> #include <unistd.h> const int flolen = 6; static const char *flos[] = {"dividuum","syb","fiji","flowhase","florolf", "piratenflo"}; static const char *proghead = "#include <stdio.h>\nint main(void){\n"; static const 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", "florolf", "piratenflo" ); for $i (0..$#flo) { eval "\$flo$i = \$flo\[$i\]"; print "\$flo$i eq \"" . eval("\$flo$i") . "\"\n"; }
Perl:
#!/usr/bin/perl @flo = ( "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ); for $i (0..$#flo) { ${"flo$i"} = $flo[$i]; print "\$flo$i eq \"" . ${"flo$i"} . "\"\n"; }
PHP:
<?php $flo = array( "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ); foreach ($flo as $i => $x) { ${"flo$i"} = $flo[$i]; echo "\$flo$i = \"" . ${"flo$i"} . "\"\n"; } ?>
Gute alte Bash:
#!/bin/sh flo="dividuum syb fiji flowhase florolf piratenflo" 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", "florolf", "piratenflo" ); $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 | grep -v xml"); ?> |
http://static.php.net/www.php.net/images/news/php-logo.gif http://www.mail-archive.com/copenhagen@perl.org/msg00162/logo_mermaid.png |
Noch einmal in Ruby:
#!/usr/bin/env ruby flo = [ "dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo" ] # flos sind keine arrayss1! flo.each_with_index do | f, i | instance_variable_set "@flo#{i}", f puts "flo#{i} = #{instance_variable_get "@flo#{i}"}" end
'cause JavaScript rocks:
flo = new Array("dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo"); for (i in flo) { eval("flo"+i+" = flo["+i+"]"); dump("flo"+i+" == "+eval("flo"+i)+"\n"); }
Wir moegen Lua:
#include <lua.h> #include <lualib.h> #include <lauxlib.h> lua_State *L; char *flos[] = {"dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo"}; char buf[1024]; void main() { char *p; int i; L = luaL_newstate(); luaL_openlibs(L); for(i=0; i < 6; i++) { sprintf(buf, "flo%d", i); lua_pushstring(L, flos[i]); lua_setglobal(L, buf); } p=buf; for(i=0; i < 6; i++) p += sprintf(p, "print(\"flo%d = \" .. flo%d);", i, i); luaL_dostring(L, buf); }
Bloatiger Angebercode, der auch noch etwas völlig anderes macht
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.)
Ru^W Objektorientiertes C
// Das aendern: #include "/usr/local/lib/ruby/1.8/i386-freebsd5/ruby.h" VALUE flo_init(VALUE self) { char *flo[] = {"dividuum", "syb", "fiji", "flowhase", "florolf", "piratenflo"}; int i; for(i = 0; i < 6; i++) { char varname[7]; sprintf(varname, "@flo%i", i); // Insecure! rb_iv_set(self, varname, rb_str_new2(flo[i])); } } VALUE flo_each(VALUE self) { int i; VALUE vars = rb_funcall(self, rb_intern("instance_variables"), 0); VALUE varname = rb_ary_shift(vars); while(varname != Qnil) { rb_yield(rb_ary_new3(2, varname, rb_iv_get(self, RSTRING(varname)->ptr))); varname = rb_ary_shift(vars); } return(Qnil); } int main() { VALUE flo; ruby_init(); flo = rb_define_class("Flo", rb_cObject); rb_define_method(flo, "initialize", flo_init, 0); rb_define_method(flo, "each", flo_each, 0); rb_eval_string("Flo.new.each { |name,value| puts \"#{name.gsub(/^@/, '')} = #{value}\" }"); return(0); }
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..flo5]->[$_], [dividuum, syb, fiji, flowhase, florolf, piratenflo]->[$_]), 0..$#{@{[flo0..flo5]}};
(Kommentar von mgr: Genau, "0..$#{@{[flo0..flo5]}}" ... 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","flo4":"florolf", "flo5":"piratenflo"}.items()])