Community contributed extensions

Learning Scala with Play

Life’s too short to not learn Scala. The Play Scala module is the easiest way to learn it. Thanks to the interactive way Play will compile, execute and report errors on your Scala code, you can immediatly start to experiment Scala.

Play with Scala

Play with Scala is an application part of this module that allow you to interactively code and execute Scala code from your preferred text editor.

To run it, go to $PLAY_HOME/modules/scala/samples-and-test/play-with-scala directory, and type:

$ play run

Then connect to http://localhost:9000 with your preferred web browser.

Now open the app/scrapbook.scala file included in the application with any text editor:

package play_with_scala {
 
    class Scrapbook {
        print("Howdy, open the app/scrapbook.scala file, and start to Play!") 
    }
 
}

The Scrapbook class body will be automatically executed each time you refresh the Web page. Every print or println statement result will be displayed right into the Web page.

For example, discard the dummy default print statement, and replace it with something more interesting:

package play_with_scala {
 
    class Scrapbook {
        val numbers = 0 to 10
        
        print(numbers)
        
        val sum = numbers.reduceLeft( (a,b) => a + b )
        
        print(sum)
        
        val strings = numbers.map( a => "Number:" + a )
        
        print(strings)
        
        val odds = numbers.filter( _ % 2 == 0 )
        
        print(odds)
    }
 
}

And refresh your brower:

You are not limited to code in the Scrapbook class, you can of course define any class or function your want:

package play_with_scala {
 
    class Scrapbook {
        val users = List(
            User("guillame", "[email protected]"),
            User("sadek", "[email protected]")
        )
                    
        val allEmails = (users:List[User]) => {
            for( user <- users ) yield user.email
        }
                    
        print( allEmails(users) )
    }
    
    case class User(name: String, email: String)
 
}

That will display:

Of course, if we make an error, Play will display a clear error page:

Resources

The Play with Scala application links to several interesting resources that can help you to discover and learn Scala.

  1. Scala for Java Refugees
  2. Programming scala
  3. Ninety-Nine Scala Problems
  4. And of course the official scala website