Skip to content

MattHicks.com

Programming on the Edge

Menu
Menu

Loops in Scala

Posted on October 1, 2009 by mhicks

Lately I’ve been learning the ins and outs of the Scala language and I have to say, having programmed in Java for the past several years, that Scala is scratching where I itch. It is a lot to grasp and mind opening, but all-in-all I see Scala as what Java could have evolved to if they would have just been willing to ditch some backwards compatibility and fix some stuff rather than just adding more and more work-arounds.

Anyway, that’s not the point of this post. I wanted to post some cool loop examples I’ve been playing with in Scala.

In Java we have the standard incrementing for-loop:

for (int i = 0; i < 10; i++) {
...
}

In Scala we have something similar, but I believe more elegant:

for (i <- 0 until 10) {
...
}

However, with Java 1.5 we got the enhanced for-loop when we don’t care about indexes:

for (String s : listOfStrings) {
...
}

In Scala we do something similar, but with a more consistent syntax:

for (s <- listOfStrings) {
...
}

Or, we can go a step further with Scala since it’s a functional language we can get rid of the for loop entirely and use a friendly method available to us called “foreach” and pass each entry to another method:

listOfStrings.foreach(doSomething);

def doSomething(s:String):Unit = {
...
}

To me this is of significant appeal because we don’t end up with a bunch of embedded for-loop blocks in our methods we focus more on modular functionality to deal with the results of the iteration instead.

7 thoughts on “Loops in Scala”

  1. Anonymous says:
    December 3, 2010 at 9:44 am

    What happens if your incrementor e.g. is not i++ what happens if I wish to count up for example i+=2 OR I+= #some other integer

    Reply
  2. Matt Hicks says:
    December 3, 2010 at 4:21 pm

    Simply modify the call to Range to step by 2:

    for (i <- 0 until 10 by 2) {
    …
    }

    Reply
  3. John Lonergan says:
    December 23, 2011 at 11:49 pm

    Re performance.

    Comparing performance I see unexpected results – any comments on the following.

    Counting up with Int
    for ( i:Int <- 0 to 500000000) { }
    took 859ms

    Counting up with Long is massively slower ..
    for ( i: Long <- 0L to 500000000L) {}
    took 5000ms

    Counting down is much faster and about same timing with int or long

    for ( i:Int <- 500000000 to 0 ) {}
    took 341ms

    for ( i:Long <- 500000000L to 0L ) {}
    took 343ms

    CPU : Core i5 M520

    Reply
  4. Matt Hicks says:
    December 24, 2011 at 12:34 pm

    @John, that's interesting. I'll have to play around with that and see what's going on. Have you tried decompiling the code into Java and seeing what the code looks like and how it differs? That's what I would probably do to get a better idea of what the Scala compiler is doing behind the scenes.

    Reply
  5. Arjuns Gab says:
    June 8, 2012 at 4:12 am

    How do I do:
    float f;
    for(f=2.0; f<=20.0; f+0.25) {
    …
    }

    I dont see any room for the increment specially for float/double types ?

    Reply
  6. Matt Hicks says:
    June 8, 2012 at 8:22 pm

    @Arjuns, it's actually quite elegant:

    for (f <- 2.0 to 20.0 by 0.25) {
    …
    }

    Reply
  7. Unknown says:
    June 29, 2012 at 9:15 pm

    @John:

    You should have done:
    for (i: Long <- 500000000L to 0L by -1) {}

    instead of:
    for (i: Long <- 500000000L to 0L) {}

    Latter doesn't do anything because 500000000L is greater than 0L.

    for (i: Long <- 0L to 500000000L) {}
    took 5000ms

    for (i: Long <- 500000000L to 0L by -1) {}
    took 7000ms when benchmarked

    So counting down seems to be much slower than counting up

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Courio: E-Mail 2.0
  • Scribe 2.0: Fastest JVM Logger in the World!
  • Logging Performance
  • Publicity in Open-Source
  • Play Framework for Scala: An Evaluation

Recent Comments

  1. Luke Hutchison on Multithreaded Unzip?
  2. Luke Hutchison on Multithreaded Unzip?
  3. Unknown on Multithreaded Unzip?
  4. D L on Multithreaded Unzip?
  5. Matt Hicks on Multithreaded Unzip?

Archives

  • March 2019
  • February 2018
  • January 2017
  • June 2016
  • July 2015
  • February 2015
  • December 2014
  • September 2013
  • March 2013
  • February 2013
  • January 2013
  • November 2011
  • December 2010
  • October 2010
  • July 2010
  • June 2010
  • May 2010
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • March 2009
  • February 2009
  • September 2008
  • July 2008
  • May 2008
  • March 2008
  • January 2008
  • December 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007

Categories

  • adobe
  • android
  • apple
  • beans
  • benchmark
  • challenge
  • chat
  • comparison
  • courio
  • eclipse
  • example
  • flex
  • framework
  • games
  • hacks
  • helios
  • html
  • hyperscala
  • ios
  • java
  • javafx
  • javascript
  • jcommon
  • jgn
  • jmc
  • jseamless
  • jug
  • kickstarter
  • learning
  • linux
  • log4j
  • logging
  • mac
  • media
  • mediacenter
  • methodology
  • mobile
  • mythtv
  • nabo
  • open-source
  • opengl
  • opinion
  • personal
  • playframework
  • pojo
  • programming
  • publicity
  • rant
  • raspberrypi
  • review
  • scala
  • scribe
  • script
  • sgine
  • social
  • sudoku
  • sun
  • swing
  • tutorial
  • Uncategorized
  • webframework
  • website
  • windows
  • xjava
© 2025 MattHicks.com | Powered by Minimalist Blog WordPress Theme