Last active
September 13, 2015 04:58
-
-
Save unascribed/500f6acaed7d01657abd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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