Showing posts with label range. Show all posts
Showing posts with label range. Show all posts
Monday, March 8, 2010
Lazy man's random test data
A quick tip for generating some random testdata.
Note: This is a poor man's solution to using ScalaCheck. If you can handle the dependency I would really recommend using that library.
Note: This is a poor man's solution to using ScalaCheck. If you can handle the dependency I would really recommend using that library.
- scala> object Options extends Enumeration {
- | val ONE, TWO, THREE, FOUR = Value
- | }
- defined module Options
- /*
- Randomly select zero or more elements from the options enumeration
- */
- scala> Options.values.filter {_ => util.Random.nextBoolean} mkString ", "
- res2: String = TWO, FOUR
- /*
- Select a random string.
- Warning: there is no restriction on the characters so control characters are likely
- */
- scala> util.Random.nextString(10)
- res5: String = ??????????
- /*
- ASCII string is oftern more useful for test data. This selects a random string up to 13 characters long
- */
- scala> util.Random.nextASCIIString(13)
- res6: java.lang.String = RVPD\#_HqJ8:o
- /*
- This creates a sequence of 10 random strings
- */
- scala> 1 to 10 map {_ => util.Random.nextASCIIString(13)}
- res7: scala.collection.immutable.IndexedSeq[java.lang.String] = IndexedSeq(;E8|Q8H8RI;Q=, vM-X;"ksBr\:c, SKyz{uXNQ5E]X, =Jd8_ll08)s%e, gRCs)6wj%C-YF, `x;2Zru?l*c%@, XE*/Rx9:qPfpm, s|u,e.un+-Xm(, M,TpX9Dq-6$+^, w;exER�|}Ya)
Tuesday, August 11, 2009
For-comprehensions
The for-comprehension construct is a very powerful way of iterating over collections. In its most basic form it is the java
for( var: collection){}
loop. As with all flow constructs in Scala, the scala for loop (or more correctly for-comprehension) can return a value. In the case of the for-comprehension it returns Unit (similar to void in Java terms) or a Sequence if yield is used.- scala> val range = 1 to 5
- range: Range.Inclusive = Range(1, 2, 3, 4, 5)
- // no return value if there is no 'yield' keyword
- scala> for( i <- 1 to 10 ) { i + 1 }
- // if there is a yield a collection is returned
- // the type of collection depends on the input
- // here a Range is returned
- scala> for( i <- range ) yield i+1
- res1: RandomAccessSeq.Projection[Int] = RangeM(2, 3, 4, 5, 6)
- // here a list is returned
- scala> for( i <- list( "a", "b", "c") ) yield "Word: "+i
- res1: List[java.lang.String] = List(Word: a, Word: b, Word: c)
- // you can filter the elements that visited in the loop
- scala> for( i <- range; if( i % 2 == 0) ) yield i
- res2: Seq.Projection[Int] = RangeFM(2, 4)
- // this is more about creating ranges than loops
- scala> for ( i <- 20 until (10,-2) ) yield i
- res3: RandomAccessSeq.Projection[Int] = RangeM(20, 18, 16, 14, 12)
- // you can string together multiple "generators"
- scala> for( i <- range; j <- range) yield (i,j)
- res4: Seq.Projection[(Int, Int)] = RangeG((1,1), (1,2), (1,3), (1,4), (1,5), (2,1), (2,2), (2,3), (2,4), (2,5), (3,1), (3,2), (3,3), (3,4), (3,5), (4,1), (4,2), (4,3), (4,4), (4,5), (5,1), (5,2), (5,3), (5\
- ,4), (5,5))
- // you can also declar variables as part of the loop declaration
- scala> for( i <- range; j <- 1 to i; k = i-j) yield k
- res5: Seq.Projection[Int] = RangeG(0, 1, 0, 2, 1, 0, 3, 2, 1, 0, 4, 3, 2, 1, 0)
- // with round brackets '(' and ')' multiple lines will require semi-colons
- scala> for (
- | i <- range;
- | j <- 1 to i;
- | k = i-j) yield k
- res6: Seq.Projection[Int] = RangeG(0, 1, 0, 2, 1, 0, 3, 2, 1, 0, 4, 3, 2, 1, 0)
- // with curly brackets '{' and '}' multiple lines you do not require semi-colons
- scala> for {
- | i <- range
- | j <- 1 to i
- | k = i-j}
- | yield{
- | k
- | }
- res7: Seq.Projection[Int] = RangeG(0, 1, 0, 2, 1, 0, 3, 2, 1, 0, 4, 3, 2, 1, 0)
- scala> for( i <- "enjoy" ) print(i)
- enjoy
Subscribe to:
Comments (Atom)