The Artima Developer Community
Sponsored Link

Scala Buzz
Looping with foreach and extracting tuples in a List

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Zemian Deng

Posts: 49
Nickname: zdeng
Registered: Jan, 2008

Zemian Deng is the creator of SweetScala web framework
Looping with foreach and extracting tuples in a List Posted: Nov 25, 2008 8:54 PM
Reply to this message Reply

This post originated from an RSS feed registered with Scala Buzz by Zemian Deng.
Original Post: Looping with foreach and extracting tuples in a List
Feed Title: thebugslayer
Feed URL: http://www.jroller.com/thebugslayer/feed/entries/atom?cat=%2FScala+Programming
Feed Description: Notes on Scala and Sweet web framework
Latest Scala Buzz Posts
Latest Scala Buzz Posts by Zemian Deng
Latest Posts From thebugslayer

Advertisement

Scala List#foreach has signature of def foreach(f : (A) => Unit) : Unit. This means we can pass a function to process each element in the list. eg:

scala> val ls = List("Mary", "had", "a", "little", "lamb")
scala> ls.foreach( e => println(e.toUpperCase) )
MARY
HAD
A
LITTLE
LAMB
The e => println(e.toUpperCase) part is Scala way of creating a anonymous function that capture each element into a parameter named e, which is separated from codes with => keyword. You may also use curly braces instead of parentheses after foreach method, especially if your function has multiple lines of codes.

What gets interesting is when each element in the list is a Tuple type. You normally need to extract, or refer to sub elements in the tuple like ._# in your function. One easy way to extract/expand tuple is using the a pattern matching rules when creating new variables like this:

scala> val ls = List(("a", 1), ("b", 2), ("c", 3))
scala> ls.foreach{ e =>
     |   val (c, n) = e
     |   println(n+" "+c)
     | }
1 a
2 b
3 c
So val (c, n) = e is the part I am talking about here. Now Scala will also let you do this extraction right where the function parameter is declared, but since foreach is only expecting single argument, you would need to add a case keyword to match and extract the sub elements. Otherwise, you will be declaring a anonymous function that has two parameters instead. Eg:
scala> val ls = List(("a", 1), ("b", 2), ("c", 3))
scala> ls.foreach{ case (c, n) =>
     |   println(n+" "+c)
     | }
1 a
2 b
3 c
This comes in handy when using method like List#zipWithIndex, as it gives you back a List of Tuple2, the original element and its index. Here is an example of usage:
scala> val ls = List("Mary", "had", "a", "little", "lamb")
scala> ls.zipWithIndex.foreach{ case (e, i) => println(i+" "+e) }
0 Mary
1 had
2 a
3 little
4 lamb
Happing programming!

Read: Looping with foreach and extracting tuples in a List

Topic: Coincidence? Previous Topic   Next Topic Topic: Binary Reading and Writing Combinators for Java

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use