Monday, March 23, 2009

feelin' randy!

Shuffle! Chaos! Randomness! Surprise me! How many times... have you wanted to:
  • shuffle a list of mp3 to pass to mplayer
  • randomize a list of background images to eog
  • pick a random file from a directory
Once or twice? OK! These can all be boiled down to reading in lines from stdout, mixing 'em up and spewing them back out in a random order.

Check out this tiny haxe program called randy. It's self compiling:
% wget http://brianin3d-misc.googlecode.com/svn/trunk/random/haxe/Randy.hx
% chmod 755 Randy.hx
% ./Randy.hx
% echo -e "1\n2\n3" | ./randy
1
3
2

I wrote it just 4u!

Wednesday, March 11, 2009

got the macro replacement blues

As crazy as it may seem, I have implemented this type of silly macro replacement stuff about a million times:

public String replace( String message, Object ... argz ) {
int i = 0;
for ( Object arg : argz ) {
message = message.replaceAll(
"\\{" + ( i++ ) + "}"
, arg.toString()
);
}
return message;
}


Basicly it converts something like:

"this is some {0}, Mr. {1}... some real {0}" + "mess" and "Man"

to something like:

this is some mess, Mr. Man... some real mess

The only "tricky" part is that you have to escape the opening curly with a backslash and then escape the backslash with a backslash...

Sigh...

Sunday, March 8, 2009

UpperCamel: compiling haxe to a native executable

UpperCamel is a little program for going back and forth between
camel case and underlined uppercase.

Typically camel case is used for class names, variables, and method names.

Whereas underlined uppercase is used for constants, statics, etc.
     Sample Input     | Sample Output
------------------+-------------------
UpperCamel | UPPER_CAMEL
ThisIsNeatoEh | THIS_IS_NEATO_EH
UPPER_CAMEL | upperCamel
MIX-and-Match | mixAndMatch

Somewhat useful... The main point is to show how to compile haxe to create a native executable:
% haxe -main UpperCamel -neko upperCamel.n
% nekotools boot upperCamel.n
% echo hello_world | ./upperCamel
helloWorld

Neat, huh?
% file upperCamel
upperCamel: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.8, dynamically linked (uses shared libs), stripped
% ldd upperCamel
linux-gate.so.1 => (0x4001a000)
libneko.so => /usr/lib/libneko.so (0x4002e000)
libpthread.so.0 => /lib/libpthread.so.0 (0x4004d000)
libc.so.6 => /lib/libc.so.6 (0x40065000)
libdl.so.2 => /lib/libdl.so.2 (0x401a8000)
libgc.so.1 => /usr/lib/libgc.so.1 (0x401ad000)
libm.so.6 => /lib/libm.so.6 (0x401e1000)
/lib/ld-linux.so.2 (0x40000000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x40207000)

The code for UpperCamel.hx has a bit of shell script which makes it easy to compile for Unix-env type folks:
% wget http://brianin3d-misc.googlecode.com/svn/trunk/UpperCamel/UpperCamel.hx
% chmod 755 UpperCamel.hx
% ./UpperCamel.hx
% file ./upperCamel
./upperCamel: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.6.8, dynamically linked (uses shared libs), stripped

Haxe to binary executable is a nice path for whipping up custom commands when you need them with a really low price of admission!

Go, Haxe!