Skip to content

Instantly share code, notes, and snippets.

@unascribed unascribed/z.groovy
Last active Sep 13, 2015

Embed
What would you like to do?
o='';r=new Random();args[0].chars.any{o+=it;if(it.letter)(r.nextGaussian()*5+10).times{o+=(char)(768+r.nextInt(48))}}print o
// By making this a Groovy script, the weight of
// a full Java-like class definition can be avoided
////
// Originally, this part of the answer used the 'def'
// keyword to infer the type of the variable, but it
// turns out you can define a variable in Groovy
// without any type at all.
buffer = '';
rand = new Random();
// There used to be a for loop here using def, but now it's a
// .any closure. .any saves a byte over .each.
////
// Groovy automatically defines an args variable for
// scripts, which we use here.
args[0].chars.any {
// As in Java, += works on a String. Previous versions of the
// answer used a StringBuffer, but a String works just as well.
buffer += it;
// Another Groovy extension; we can use the primitive char like
// a Character object, and use .letter instead of .isLetter()
if (it.letter)
// Since Gaussian is already a normal distribution with mean 0
// stddev 1, we can fix that with a quick multiply and add.
((rand.nextGaussian() * 5) + 10).times {
// I use the literal number '768' here, as it's the same size
// as a Java char literal, and Groovy doesn't have char literals.
buffer += (char)(768+rand.nextInt(48));
}
}
// Finally, we can take a quick shortcut here by using the
// parentheses-less syntax for print in Groovy. Using print
// instead of println saves a whole two bytes!
print buffer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.