this.Blog.Find(entry => entry.IsHelpful);
 Monday, July 14, 2008
Generating a random string in Groovy

A while ago, I wrote a post about using LINQ in C# to generate a random confirmation number.  On my current gig we are using Groovy to write test scripts for SOAP UI acceptance test cases (incidentally, SOAP UI is a great web service testing tool that probably deserves its own blog post). 

In order to generate a dummy random string, I used an algorithm similar to the one I used in the LINQ post and wanted to show how it would be implemented in Groovy.

// create the list of available characters
def availChars = []
('A'..'Z').each { availChars << it.toString() }
// even it out to about the same odds of getting a char or a number
3.times { (0..9).each { availChars << it.toString() } }

def generateRandomString = { length -> 
  def max = availChars.size    
  def rnd = new Random()
  def sb = new StringBuilder()
  length.times { sb.append(availChars[rnd.nextInt(max)]) }
  sb.toString()
} 

// print it out 10 times to see the randomness
10.times { println generateRandomString(8) }

If you're not familiar with Groovy, the -> symbol in the generateRandomString declaration marks a closure that takes one argument. In this case, it takes the desired length of the returned string. The last line invokes the closure n times. I really like (similar to Ruby) how you can pass function blocks as iterators over collections.  It makes creating a loop both simple and intuitive (e.g. 10.times).  And the syntantical sugar of creating a ranged list (e.g. 'A'..'Z') is another nice feature of the language.



Kick it on DotNetKicks.com
Monday, July 14, 2008 2:21:50 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  Groovy

 Saturday, June 14, 2008
Collection Initializers

Scripting languages like Ruby and Groovy recognize the importance of collections like lists and maps, treating them almost like first class objects.  By doing so, they make it really easy to create collections like these and populate them all in one step.

For example, in Ruby here is how this is done:

# create a list
lst = [1, 2, 3, 4, 5]
# create a map
map = { "one" => 1, "two" => 2, "three" => 3 }

Not surprisingly, we see very similar syntax in Groovy...

// create a list
def lst = [1, 2, 3, 4, 5]
// create a map
def map = ["one":1, "two":2, "three":3]

And in case you missed it, one of the changes in C# 3.0 was the ability to initialize collections similar to the way it's done in these scripting languages (though the syntax is not *quite* as clean):

// create a list
var lst = new List<int> { 1, 2, 3, 4, 5 };
// create a map
var map = new Dictionary<string, int> { {"one", 1}, {"two", 2}, {"three", 3} };

IMO, the new collection initializers (along with property initializers) are one of the nicest changes in the new version of the .Net framework.  And since I'm feeling nice, I won't show how this is accomplished in native Java.  :-D


Kick it on DotNetKicks.com
Saturday, June 14, 2008 8:23:10 PM (Eastern Standard Time, UTC-05:00)  #    Comments [0]  C# | Groovy | Ruby