We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexDiscussionExhibition › Quines in Processing
Page Index Toggle Pages: 1
Quines in Processing (Read 1371 times)
Quines in Processing
May 25th, 2009, 5:31am
 
Being particularly bored on a Bank Holiday Monday, I was reading The Daily WTF and discovered what a quine is.

Being something of a fan of Zen I immediately opened up Processing thinking, "this should be a doddle in P5".

A few dead ends and then I then came up with this little number:

Code:

print(join(loadStrings("../sketch_may25a.pde"), ' '));


Then Ctrl-S, Ctrl-R.

As a discussion point and topical excercise for the egg heads, what other quines are possible in P5 The Quine page has a few in Java, but I'm sure Processing could be less verbose.

(And I'm not talking about hitting Ctrl-R with an empty document like this cheeky contest entry)
Re: Quines in Processing
Reply #1 - May 25th, 2009, 8:20am
 
I had thought it'd be cool to see some quines in Processing too. I gave it a try and never came up with anything more complex than your example which I considered kind of a cheat.
Re: Quines in Processing
Reply #2 - May 25th, 2009, 8:38am
 
No cheating!

Code:

String s="String s=;char c=34;print(s.substring(0,9)+c+s+c+s.substring(9));";char c=34;print(s.substring(0,9)+c+s+c+s.substring(9));


(Other than converting the example here.)

I'd like to see someone come up with one that printed the output to an applet. That would be in the true spirit of Processing.
Re: Quines in Processing
Reply #3 - May 26th, 2009, 4:50am
 
hmm... I'm not sure if quines would really be that interesting in processing compared to other languages, since all the quines for Java work in Processing. It would be cool if, in addition to producing its own source, the sketch drew a fractal.

Of course, then it wouldn't be a quine.
Re: Quines in Processing
Reply #4 - May 26th, 2009, 6:34am
 
Catherine wrote on May 26th, 2009, 4:50am:
all the quines for Java work in Processing
Not really, Java needs more fluff (main class, main(), etc.)

I made a couple of Quines for Lua long ago, I adapted one for Processing. It was slightly more tricky because we have only one kind of quote.

Quote:
char q = 34, c = 44, n = 10;
String[] a =
{
"char q = 34, c = 44, n = 10;",
"String[] a =",
"{",
"",
"};",
"String B() { String s = new String(); for (int i = 0; i < a.length; i++) { s += q + a[i] + q + c + n; } return s; }",
"void D() { for (int i = 0; i < a.length; i++) println(a[i]); }",
"void setup() { a[3] = B(); D(); exit(); }",

};
String B() { String s = new String(); for (int i = 0; i < a.length; i++) { s += q + a[i] + q + c + n; } return s; }
void D() { for (int i = 0; i < a.length; i++) println(a[i]); }
void setup() { a[3] = B(); D(); exit(); }

Re: Quines in Processing
Reply #5 - May 26th, 2009, 8:31am
 
A variant:
Code:
String tk = Character.toString((char) 0x40);
String eol = Character.toString((char) 0x0A);
char[] closingA = { 0x22, 0x20, 0x2B, 0x20, 0x6E, 0x20, 0x2B, 0x0A, 0x22 };
String closing = new String(closingA);
char q = 34, sc = 59, n = 10;
String p =
"String tk = Character.toString((char) 0x40);" + n +
"String eol = Character.toString((char) 0x0A);" + n +
"char[] closingA = { 0x22, 0x20, 0x2B, 0x20, 0x6E, 0x20, 0x2B, 0x0A, 0x22 };" + n +
"String closing = new String(closingA);" + n +
"char q = 34, sc = 59, n = 10;" + n +
"String p =" + n +
"@" + n +
"String fp = q + p.replaceAll(eol, closing) + q + sc;" + n +
"println(p.replace(tk, fp));" + n +
"exit();" + n +
"";
String fp = q + p.replaceAll(eol, closing) + q + sc;
println(p.replace(tk, fp));
exit();
Re: Quines in Processing
Reply #6 - May 26th, 2009, 12:56pm
 
I think I finally got it. Thanks for motivating me guys. It took me a long time to come up with but I like how it turned out. I used Processing to help me write a quine in Processing.

The quine Code:
byte[]a = {112, 114, 105, 110, 116, 40, 34, 98, 121, 116, 101, 91, 93, 97, 32, 61, 32, 123, 34, 32, 43, 32, 106, 111, 105, 110, 40, 115, 116, 114, 40, 97, 41, 44, 32, 34, 44, 32, 34, 41, 32, 43, 32, 34, 125, 59, 34, 32, 43, 32, 34, 92, 110, 34, 32, 43, 32, 106, 111, 105, 110, 40, 115, 116, 114, 40, 99, 104, 97, 114, 40, 97, 41, 41, 44, 34, 34, 41, 32, 43, 32, 34, 92, 110, 101, 120, 105, 116, 40, 41, 59, 34, 41, 59};
print("byte[]a = {" + join(str(a), ", ") + "};" + "\n" + join(str(char(a)),"") + "\nexit();");
exit();


And the helper Code:
String s = "print(\"byte[]a = {\" + join(str(a), \", \") + \"};\" + \"\\n\" + join(str(char(a)),\"\") + \"\\nexit();\");";
for(int a=0; a<s.length(); a++) {
print(byte(s.charAt(a)) + ", ");
}
print("\n\n" + s);
exit();


Maybe I'll try one that prints to the applet now as st33d suggested.
Re: Quines in Processing
Reply #7 - May 27th, 2009, 9:36am
 
It was easy enough to use the same method to do an applet one. It did get kind of ridiculous though. I would like to see a shorter version.

This Code:
byte[]a =
{118, 111, 105, 100, 32, 115, 101, 116, 117, 112, 40, 41, 32, 123, 10, 32,
32, 115, 105, 122, 101, 40, 52, 48, 48, 44, 56, 48, 48, 41, 59,
10, 32, 32, 80, 70, 111, 110, 116, 32, 102, 111, 110, 116, 32, 61,
32, 99, 114, 101, 97, 116, 101, 70, 111, 110, 116, 40, 34, 65, 114,
105, 97, 108, 34, 44, 32, 49, 48, 44, 32, 102, 97, 108, 115, 101,
41, 59, 10, 32, 32, 116, 101, 120, 116, 70, 111, 110, 116, 40, 102,
111, 110, 116, 41, 59, 10, 125, 10, 118, 111, 105, 100, 32, 100, 114,
97, 119, 40, 41, 32, 123, 10, 32, 32, 83, 116, 114, 105, 110, 103,
32, 115, 32, 61, 32, 34, 34, 59, 10, 32, 32, 115, 32, 43, 61,
32, 34, 98, 121, 116, 101, 91, 93, 97, 32, 61, 92, 110, 123, 34,
59, 10, 32, 32, 102, 111, 114, 40, 105, 110, 116, 32, 105, 61, 48,
59, 32, 105, 60, 97, 46, 108, 101, 110, 103, 116, 104, 59, 32, 105,
43, 43, 41, 32, 123, 10, 32, 32, 32, 32, 115, 32, 43, 61, 32,
97, 91, 105, 93, 59, 10, 32, 32, 32, 32, 105, 102, 40, 105, 33,
61, 97, 46, 108, 101, 110, 103, 116, 104, 45, 49, 41, 32, 115, 32,
43, 61, 32, 34, 44, 32, 34, 59, 10, 32, 32, 32, 32, 105, 102,
40, 105, 33, 61, 48, 32, 38, 38, 32, 105, 37, 49, 53, 61, 61,
48, 41, 32, 115, 32, 43, 61, 32, 34, 92, 110, 34, 59, 10, 32,
32, 125, 10, 32, 32, 115, 32, 43, 61, 32, 34, 125, 59, 92, 110,
34, 59, 10, 32, 32, 115, 32, 43, 61, 32, 106, 111, 105, 110, 40,
115, 116, 114, 40, 99, 104, 97, 114, 40, 97, 41, 41, 44, 34, 34,
41, 59, 10, 32, 32, 116, 101, 120, 116, 40, 115, 44, 32, 49, 48,
44, 32, 50, 48, 41, 59, 10, 125};
void setup() {
size(400,700);
PFont font = createFont("Arial", 10, false);
textFont(font);
}
void draw() {
String s = "";
s += "byte[]a =\n{";
for(int i=0; i<a.length; i++) {
s += a[i];
if(i!=a.length-1) s += ", ";
if(i!=0 && i%15==0) s += "\n";
}
s += "};\n";
s += join(str(char(a)),"");
text(s, 10, 20);
}

should work if you have Arial. Looks like the forum screws up the formatting but it should be okay when you copy and paste.
Page Index Toggle Pages: 1