Showing posts with label for-comprehension. Show all posts
Showing posts with label for-comprehension. Show all posts
Tuesday, February 2, 2010
Blocks within for-comprehensions
In another example of uniformity it is possible to use blocks within a for-comprehension when creating a generator or making an assignment. The basic form of a for-comprehension is
The generator, guard, assignment and result are all expressions which means they can all be blocks or simple statements. Most commonly you will see them as simple statements:
But since they are expressions they can be more complex:
- for {i <- generator
- if guard
- j = assignment } yield result
The generator, guard, assignment and result are all expressions which means they can all be blocks or simple statements. Most commonly you will see them as simple statements:
- scala> for {i <- 1 to 10
- | if i % 2 == 0
- | j = i } yield j
- res50: scala.collection.immutable.IndexedSeq[Int] = IndexedSeq(2, 4, 6, 8, 10)
But since they are expressions they can be more complex:
- scala> for {
- | i <- {
- | val start = nextInt(3)
- | val end = nextInt(10)+start
- | start to end
- | }
- | if {
- | val cut = nextInt(3)+1
- | i % cut == 0
- | }
- | j = {
- | val x = i+1
- | x / 2
- | }
- | } yield {
- | // do a debug println
- | println(j)
- | j
- | }
- 1
- 1
- 2
- 3
- res53: scala.collection.immutable.IndexedSeq[Int] = IndexedSeq(1, 1, 2, 3)
Labels:
for-comprehension,
Scala
Tuesday, January 26, 2010
Guard Sugar
This topic is a simple tip for a cleaner syntax for guards. Guards/filters are statements in for-comprehensions and case statements that guard or filter matches. Often you will see them as follows:
However you have the option to apply a little sugar to the statements cleaning them up a little:
As you can see the brackets are optional. That is because in both cases the brackets are not required for the parser to determine the start and end of the guard statements. They are added so that the "normal" syntax used in the typical if statements will compile.
- scala> for (i <- 1 to 10; if (i % 2 == 1)) yield i
- res0: scala.collection.immutable.IndexedSeq[Int] = IndexedSeq(1, 3, 5, 7, 9)
- scala> util.Random.nextInt(10) match {
- | case i if(i>5) => println("hi")
- | case _ => println("low")
- | }
- low
However you have the option to apply a little sugar to the statements cleaning them up a little:
- scala> for (i <- 1 to 10; if i % 2 == 1) yield i
- res2: scala.collection.immutable.IndexedSeq[Int] = IndexedSeq(1, 3, 5, 7, 9)
- scala> util.Random.nextInt(10) match {
- | case i if i>5 => println("hi")
- | case _ => println("low")
- | }
- hi
As you can see the brackets are optional. That is because in both cases the brackets are not required for the parser to determine the start and end of the guard statements. They are added so that the "normal" syntax used in the typical if statements will compile.
- scala> 10 match { case i if i == 1 || i == 10 => "obviously this is a match"}
- res4: java.lang.String = obviously this is a match
- /*
- In case statements you can split the if almost any way you want because it is very clearly bound by the if and the => that is required for all case statements with a guard
- */
- scala> 10 match { case i if i == 1 ||
- | i == 10 => "obviously this is a match"}
- res5: java.lang.String = obviously this is a match
- scala> 10 match {
- | case i
- | if
- | i == 1
- | ||
- | i == 10
- | => "matched"
- | }
- res6: java.lang.String = matched
- /*
- For-comprehensions are not as flexible since it is harder for the compiler to determine where the guard ends. So try to keep it on one line or otherwise make it a function. That is probably good advice for case statements as well.
- */
- scala> for {
- | x <- 1 to 10
- | if
- | x > 10
- | || x == 2 } yield x
- < console>:5: error: '<-' expected but integer literal found.
- || x == 2 } yield x
- ^
- < console>:5: error: illegal start of simple expression
- || x == 2 } yield x
- ^
- scala> for {
- | x <- 1 to 10
- | if x == 1 || x == 10 } yield x
- res7: scala.collection.immutable.IndexedSeq[Int] = IndexedSeq(1, 10)
Labels:
beginner,
filter,
for,
for-comprehension,
guard,
match,
matching,
Scala,
syntactic sugar
Wednesday, January 6, 2010
Matching in for-comprehensions
At a glance a for-comprehension appears to be equivalent to a Java for-loop, but it is much much more than that. As shown in post: for-comprehensions, for-comprehensions can have guards which filter out which elements are processed:
They can be used to construct new collections:
They can contain multiple generators:
What has not been covered is that the assignments also does pattern matching:
This is not surprising as this also occurs during normal assignment. But what is interesting is that the pattern matching can act as a guard as well. See Extractor examples and Assignment and Parameter Objects for more information of pattern matching and extractors.
Now just for fun here is a similar example but using symbols instead of strings for the key values:
- scala> for ( x <- 1 to 10; if (x >4) ) println(x)
- 5
- 6
- 7
- 8
- 9
- 10
They can be used to construct new collections:
- scala>?for(?i?<-?List(?"a",?"b",?"c")?)?yield?"Word:?"+i
- res1:?List[java.lang.String]?=?List(Word:?a,?Word:?b,?Word:?c)
They can contain multiple generators:
- scala> for {x <- 1 to 10
- | if(x%2 == 0)
- | y <- 1 to 5} yield (x,y)
- res1: scala.collection.immutable.IndexedSeq[(Int, Int)] = IndexedSeq((2,1), (2,2), (2,3), (2,4), (2,5), (4,1), (4,2), (4,3), (4,4), (4,5), (6,1), (6,2), (6,3), (6,4), (6,5), (8,1), (8,2), (8,3), (8,4), (8,5), (10,1), (10,2), (10,3), (10,4), (10,5))
What has not been covered is that the assignments also does pattern matching:
- scala> for ( (x,y) <- (6 to 1 by -2).zipWithIndex) println (x,y)
- (6,0)
- (4,1)
- (2,2)
This is not surprising as this also occurs during normal assignment. But what is interesting is that the pattern matching can act as a guard as well. See Extractor examples and Assignment and Parameter Objects for more information of pattern matching and extractors.
- scala> val args = Array( "h=2", "b=3")
- args: Array[java.lang.String] = Array(h=2, b=3)
- scala> val Property = """(.+)=(.+)""".r
- Property: scala.util.matching.Regex = (.+)=(.+)
- scala> for {Property(key,value) <- args } yield (key,value)
- res0: Array[(String, String)] = Array((h,2), (b,3))
- scala> Map(res0:_*)
- res1: scala.collection.immutable.Map[String,String] = Map(h -> 2, b -> 3)
- scala> res1("h")
- res3: String = 2
Now just for fun here is a similar example but using symbols instead of strings for the key values:
- scala> val args = Array( "h=2", "b=3")
- args: Array[java.lang.String] = Array(h=2, b=3)
- scala> val Property = """(.+)=(.+)""".r
- Property: scala.util.matching.Regex = (.+)=(.+)
- scala> for {Property(key,value) <- args } yield (Symbol(key),value)
- res0: Array[(Symbol, String)] = Array(('h,2), ('b,3))
- scala> Map(res0:_*)
- res1: scala.collection.immutable.Map[Symbol,String] = Map('h -> 2, 'b -> 3)
- scala> res1('h)
- res2: String = 2
Labels:
for-comprehension,
intermediate,
matching,
regex,
regular expression,
Scala,
symbol
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
Friday, August 7, 2009
Creating XML
Scala allows you to embed XML directly into a program and provides several ways to manipulate it. Today we will look at writing XML.
- scala> val xml = <root>
- | <child>text</child>
- | </root>
- xml: scala.xml.Elem =
- <root>
- <child>text</child>
- </root>
- scala> val data = "a string"
- data: java.lang.String = a string
- // you can embed logic and variables in the xml by surrounding with {}
- scala> val xml = <root>{data}</root>
- xml: scala.xml.Elem = <root>a string</root>
- scala> val xml = <root>{ for( i <- 1 to 3 ) yield {<xml i={i.toString}/>} } </root>
- xml: scala.xml.Elem = <root><xml i="1"></xml><xml i="2"></xml><xml i="3"></xml> </root>
- scala> val xml = <root>{ for( i <- 1 to 3 ) yield <child>{i}</child> } </root>
- xml: scala.xml.Elem = <root><child>1</child><child>2</child><child>3</child> </root>
- // save xml to file. Note Scala 2.8 is changin save API
- // and will require the encoding and DocType information
- scala> scala.xml.XML.save( "/tmp/doc.xml", xml)
Labels:
beginner,
for,
for-comprehension,
Scala,
xml
Monday, August 3, 2009
Basic Collection Operations
In this example I show some common operations that can be performed on all collections. The examples use list but any Iterable can be used.
- scala> val list = List(1,2,3,4)
- list: List[Int] = List(1, 2, 3, 4)
- scala> list.mkString(",")
- res0: String = 1,2,3,4
- scala> list.mkString("(",",",")")
- res1: String = (1,2,3,4)
- scala> list.reduceLeft ( (next,cumulative) => cumulative + next )
- res2: Int = 10
- scala> for( e <- list ) println ( e )
- 1 2 3 4
- scala> for( e <- list ) {
- | println ( e )
- | }
- 1 2 3 4
- scala> for( e <- list ) yield e+1
- res5: List[Int] = List(2, 3, 4, 5)
- scala> for ( e <- list; if( e % 2 == 0 ) ) yield e
- res6: List[Int] = List(2, 4)
Labels:
beginner,
collections,
for,
for-comprehension,
list,
Scala
Subscribe to:
Comments (Atom)