scala

Tutorials about the Scala programming language.

Generating Play Framework 2 CRUD forms with Cato

Update: The source code for the Cato CRUD Generator is now available from Github. See the end of this article for more information.

Different ways to create random strings in Scala

When it comes to generating random strings with the scala.util.Random class, I haven't figured out yet how to use the nextString method. I've tried using it in several different ways, but I always get a string of question marks as output, like this:

Scala - Using a for loop with embedded if statements

If you ever need to use a Scala for loop (for comprehension) with one or more embedded if statements, I hope the following example is helpful:

Setting the classpath in a Scala script (Scala shell script)

Scala script FAQ: How do I set the CLASSPATH in a Scala shell script?

If you need to set the CLASSPATH when executing a Scala script, the following example code at the top of your script should do the trick for you:

#!/bin/sh
exec scala -classpath "lib/htmlcleaner-2.2.jar:lib/scalaemail_2.9.1-1.0.jar:lib/stockutils_2.9.1-1.0.jar" "$0" "$@"
!#

As you can see from that code, I'm adding three jar files to my classpath at the beginning of my Scala shell script.

Scala java.lang.NoSuchMethodError: scala.Predef$.augmentString error

It's been a while since I've seen a problem related to using different versions of Scala, but I just ran into it now.

A Scala String chomp (or chop) function

Scala String FAQ: Does Scala have a String method like chomp or chop that you find in Perl?

If you're using Scala and want a String chomp or chop method you're used to in languages like Perl to remove end of line characters, it looks like the stripLineEnd method will do what you want, as shown in the REPL:

Scala - matching a regex pattern against an entire String

Scala regex FAQ: How can I match a regex pattern to an entire string in Scala?

This morning I needed to write a little Scala code to make sure a String completely matches a regex pattern. I started off by creating a Scala Regex instance, and then realized the Regex class doesn't have a simple method to determine whether a String completely matches a pattern.

How to populate sample data when a Play Framework application starts up

If you ever need an example of how to do something when a Play Framework application starts up, check out the Zentasks application in the Play distribution samples directory. You'll find the following code in the Global.scala file in the app subdirectory of the project. It shows how to use the onStart method of the Global object to populate some sample data when the Play Framework app starts up.

Here's the source code for that class:

Scala - calling foreach on a Seq to populate sample data

I just saw the following Scala source code in a Play Framework 2 sample application, and it struck me as a nice example of how to call the foreach method on a Seq to populate some sample data:

Scala - How to create a list of alpha or alphanumeric characters

While looking for code on how to create a random string in Scala, I came across this article, which shows one approach for creating a random string. For my brain today, it also shows a nice way to create a list of alpha or alphanumeric characters.

For instance, to create a list of alphanumeric characters, use this approach:

Syndicate content