Posts in the “scala” category

Scala: Generating a sequence/list of all ASCII printable characters

I ran into a couple of interesting things today when trying to generate random alphanumeric strings in Scala, which can be summarized with the code shown below. Skipping the details of what I was working on, here are several examples that show how to generate lists of alphanumeric/ASCII characters in Scala:

How to catch the ctrl-c keystroke in a Scala command line application

As a brief note about catching the [ctrl][c]/ctrl-c signal in a Scala command-line application, I just used this approach on Mac (Unix) system, and was able to catch the ctrl-c signal:

/**
  * The ability to cancel this code with `ctrl-c` inside of 
  * sbt requires you to have this setting in build.sbt:
  * 
  *     fork in run := true
  */
object SlowSocketServer extends App {
    val serverResponseDelayTimeMs = 0
    val serverPort = 5150

    Runtime.getRuntime().addShutdownHook(new Thread {
        override def run = {
            System.out.println("Shutdown hook ran")
            // if (socket != null) socket.close()
            Thread.sleep(1000)
        }
    })
    
    // more code here ...

I was running this application with sbt, and as long as I used that fork setting in the build.sbt file, the ctrl-c keystroke killed my application without also killing sbt, which is what I wanted. This method also printed out the "Shutdown hook ran" string before it died, as desired.

Java BufferedReader, readLine, and a Scala while loop

I generally try to avoid this coding style these days, but, if you want to see how to use a Java BufferedReader and its readLine method in a Scala while loop, here you go:

@throws(classOf[IOException])
def readFileToStringArray(canonFilename: String): Array[String] = {
    val bufferedReader = new BufferedReader(new FileReader(canonFilename))
    val lines = new ArrayBuffer[String]()
    var line: String = null
    while ({line = bufferedReader.readLine; line != null}) {
        lines.add(line)
    }
    bufferedReader.close
    lines.toArray
}

For this example, these are the important lines to look at:

val lines = new ArrayBuffer[String]()
var line: String = null
while ({line = bufferedReader.readLine; line != null}) { 
    lines.add(line)
}

The most important part of that code — and the biggest difference from Java — is how you need to handle the readLine method inside the Scala while loop, specifically using it in a block of code that’s wrapped in curly braces, where that block of code returns a Boolean value. (The semi-colon inside that block of code helps me include two lines of Scala code inside the curly braces.)

I don’t have much time to explain this code tonight, so I hope it makes sense as is. In summary, if you wanted to see how to use a Java BufferedReader and its readLine method inside a Scala while loop, I hope this example is helpful.

Scala Ammonite REPL: How to add/paste multiline input

As a quick note, if you need to paste multiple lines of input at one time to the Ammonite REPL, paste your code inside a block. This is particularly helpful when you need to paste something like a companion class and companion object into the Ammonite REPL:

@ {
    // your code starts
    class Foo {
        // code here ...
    }
    object Foo {
        // code here ...
    }
    // your code ends
}

As shown, just include your code inside the opening and closing curly braces.

(Almost) The simplest possible Play Framework and Scala WebSocket example

In this article I show how to write a WebSocket server using the Play Framework and Scala. My goal is to create the simplest possible WebSocket server so you can see how the basics work.

To demonstrate the WebSocket server I also created an HTML/JavaScript client. It isn’t quite as simple as the server, but I decided to keep some extra code in the client in case you have problems and need some debugging help.

Creating a JSON method in a Play Framework Controller

This is a short example of how to create a Play Framework Controller method that returns JSON. I’ve written this brief tutorial assuming you already know the basics of Play, i.e., what a Controller is, where the routes file, and how to run Play.

As you’ll see, it’s pretty simple. (Or, as a friend says, “It’s obvious ... once you know how to do it.”)

Never run/create Scala futures in the constructor of an object

I had a problem with a Scala Future example a couple of days ago, and Guillaume Martres helped me through it. He also mentioned one thing about Scala futures that I thought I should share here:

“Rule of thumb is: never run futures in the constructor of an object, this can easily lead to deadlock as you try calling a method of the object while the object is being initialized ... moving everything inside a main method is fine.”

I’ve seen that type of comment in other discussions related to the time required to create constructors, and when I saw what he wrote there it really hit home.

Scala 3: Use @main for main methods, don’t use App

I was just reminded that the “object MyApp extends App” syntax is basically deprecated in Dotty. The correct approach in Scala 3 is to use an @main method, or to manually define a main method.

In my case I was trying to run an old Scala 2 futures example with Dotty without updating the App syntax, and ran into a problem. Once I switched to using the @main method everything worked as expected.

Many thanks to Guillaume Martres for his help!

How to load multiple library dependencies in SBT

sbt FAQ: How do I load multiple library dependencies in sbt? That is, what is the correct syntax to have multiple dependencies in the sbt build.sbt file?

Solution: Use a Seq with the libraryDependencies syntax in your build.sbt file:

Scala: How to extract parts of a string that match a regex

Scala String FAQ: How can I extract one or more parts of a string that match the regular-expression patterns I specify?

Solution

Define the regular-expression patterns you want to extract from your String, placing parentheses around them so you can extract them as “regular-expression groups.” First, define the desired pattern:

val pattern = "([0-9]+) ([A-Za-z]+)".r

Next, extract the regex groups from the target string:

val pattern(count, fruit) = "100 Bananas"

Comparisons of Java’s Optional and Scala’s Option

As a brief note today, here are a couple of comparisons of Java’s Optional and Scala’s Option.

First, here’s a Scala makeInt method written with Option in Scala 3:

def makeInt(s: String): Option[Int] = 
    try
        Some(s.toInt)
    catch
        case e: NumberFormatException => None

and here’s the same method written with Optional in Java:

SBT: Use %%% to import Scala.js and Scala-Native libraries

I was just reminded when reading this article that you have to use three % symbols when adding Scala.js and Scala-Native library dependencies in sbt. Specifically you’ll find that this libraryDependencies line does not work for adding a Scala.js and Scala-Native library dependency:

"com.github.scopt" %% "scopt" % "3.6.0"       // ERROR, DOESN’T WORK

This is the correct sbt syntax for Scala.js and Scala-Native libraries:

+= "com.github.scopt" %%% "scopt" % "3.6.0"   // WORKS
                      ---

As the author of that article notes, this syntax is equivalent to this build.sbt line:

"com.github.scopt" % "scopt_native0.2_2.11" % "3.6.0"
                      --------------------

Again I underlined the relevant syntax in that example.

If you’re trying to add a Scala.js or Scala-Native library dependency to an sbt build.sbt file, I hope this example is helpful.

How to create a large test Map in Scala (converting a sequence to a map)

If you ever need to create a large test Scala Map, I just used this approach and it worked fine:

val map = (for (i <- 1 to 5_000_000) yield (i, 10*i)).toMap

The initial results in the Scala REPL look like this:

val map: Map[Int, Int] = HashMap(4584205 -> 45842050, 2231874 -> 22318740, ...

The code works by creating a series of tuples with the yield expression, where each key is i, and each value is 10*i. Here are a couple of other ways to convert a Scala sequence to a map:

val map = Vector.range(0,1_000_000).map(i => (i, i*10)).toMap
val map = Vector.range(0,1_000_000).map(i => i -> i*10).toMap