Tuesday, May 25, 2010

A Java printf quine

The challenge: can you write a printf statement in Java that prints the statement itself?

The answer: a resounding yes (see output on ideone.com)
System.out.printf("System.out.printf(%1$c%2$s%1$c,34,%1$c%2$s%1$c);",34,"System.out.printf(%1$c%2$s%1$c,34,%1$c%2$s%1$c);");
My, my, my. Just look at it!!! Have you ever seen such symmetrical self-generating concentrated beauty???

This is without a doubt one of my masterpieces in Java.

Saturday, May 22, 2010

I don't like regex! I love it!

System.out.println("du hast mich".replaceAll("(?<=^(.*)) ", ", $1 "));
// prints "du, du hast, du hast mich"

System.out.println("hold thrill kiss kill me".replaceAll("(?= .* (.*))", " $1,"));
// prints "hold me, thrill me, kiss me, kill me"

System.out.println("Mmm ".replaceAll(".(?=.*?( )?(?<=(^.*) ))", "$2$1"));
// prints "Mmm Mmm Mmm Mmm"

Thursday, May 20, 2010

On "meta-formatting" experiments

I've been way too obsessed with stackoverflow to write in the blog, but here's a fun little snippet:
String[] arr = { "a", "b", "c" };
final int N = arr.length;
String format = new String(new char[N])
    .replace("\0", ":")
    .replaceAll(".(?=(.)?)", "%s$1");
System.out.println(format);      // prints "%s:%s:%s"
System.out.format(format, arr);  // prints "a:b:c"
Not knowing any better, I call this "meta-formatting", i.e. the programmatic generation of formatting strings. It's really not that much different than "meta-regexing", i.e. the programmatic generation of... do I really have to say this?

And here's a slightly more complicated example:
String[] arr = { "alpha", "omega", "xxx", "yyy" };
final int N = arr.length;
String format = new String(new char[N/2])
    .replaceAll(".", "(%7s : %-7s)%n");
System.out.format(format, arr);
// prints: (  alpha : omega  )
//         (    xxx : yyy    )
And another one for giggles:
String[] arr = { "a", "b", "c", "d" };
final int N = arr.length;
String format = new String(new char[N])
    .replaceAll("(?<=(^.*)).", "%s$1")
    .replaceAll("(?!^)(?=%)", " = ")
    .replaceAll("\0", "+%<s");
System.out.format(format, arr);
// prints "a = b+b = c+c+c = d+d+d+d"

Monday, May 10, 2010

[TF004] NaN is not a Number (true/false?)

(true/false?) NaN is acronym for "Not a Number", therefore it's not a Number.

(Please don't kill me for this...)

Saturday, May 1, 2010

Java simple assignment operator semantics

I'll elaborate on this more once I have the time, but I was surprised by the semantics of a simple assignment in Java. I knew it's not "right-then-left" (then assign), but rather "left-then-right" (then assign). It turns out, though, that this isn't as straightforward as I initially thought.

Here are some snippets:
int[] arr = { };
arr[-1] = arr[-2];   
// Throws ArrayIndexOutOfBoundsException: -2

int[] arr = { };
arr[arr[-1]] = arr[arr[-2]];
// Throws ArrayIndexOutOfBoundsException: -1
int[] arr = null;
arr[-1] = (arr = new int[0])[0];
// Throws ArrayIndexOutOfBoundsException: 0

int[] arr = null;
arr[-1] = (arr = new int[1])[0];
// Throws NullPointerException