scala

Tutorials about the Scala programming language.

Scala match/case expressions (syntax, examples) alvin July 16, 2019 - 12:03pm
Table of Contents1 - Scala match expressions2 - Aside: A quick look at Scala methods3 - Using a match expression as the body of a method4 - Handling alternate cases5 - Using if expressions in case statements6 - Even more ...

This is a lesson on Scala match/case expressions from my book, Hello, Scala.

Back to top

Scala match expressions

Scala has a concept of a match expression. In the most simple case you can use a match expression like a Java switch statement:

// i is an integer
i match {
    case 1  => println("January")
    case 2  => println("February")
    case 3  => println("March")
    case 4  => println("April")
    case 5  => println("May")
    case 6  => println("June")
    case 7  => println("July")
    case 8  => println("August")
    case 9  => println("September")
    case 10 => println("October")
    case 11 => println("November")
    case 12 => println("December")
    // catch the default with a variable so you can print it
    case _  => println("Invalid month")
}

As shown, with a match expression you write a number of case statements that you use to match possible values. In this example I match the integer values 1 through 12. Any other value falls down to the _ case, which is the catch-all, default case.

Scala: What do “effect” and “effectful” mean in functional programming?

Table of Contents1 - Effects are related to monads2 - Not a side effect, but the main effect3 - Effectful functions return F[A] rather than [A]4 - Summary5 - Notes

When you get started with functional programming (FP) a common question you’ll have is, “What is an effect in functional programming?” You’ll hear advanced FPers use the words effects and effectful, but it can be hard to find a definition of what these terms mean.

How to append when writing to a text file in a Java or Scala application alvin July 15, 2019 - 7:21pm

Quick tip: To append when writing to a text file in a Scala or Java application, create your FileWriter with the append flag set to true, like this:

val bw = new BufferedWriter(new FileWriter(new File("/tmp/file.out"), true))
bw.write("Hello, world\n")
bw.close
Experiment with passing Scala methods as function parameters, signature overloading alvin July 14, 2019 - 1:24pm

As a brief note to self, I was experimenting with passing methods as parameters to Scala functions — in particular trying to overload those method signatures to see what would work and what would not work — and I came up with these simple function signature overloading tests:

My Scala Sed project: More features, returning strings alvin July 11, 2019 - 7:13pm
Table of Contents1 - Basic use2 - Using a Map3 - Match expressions4 - Sed limitations5 - My Sed project6 - Bonus: Factories and HOFs

My Scala Sed project is still a work in progress, but I made some progress on a new version this week. My initial need this week was to have Sed return a String rather than printing directly to STDOUT. This change gave me more ability to post-process a file. After that I realized it would really be useful if the custom function I pass to Sed had two more pieces of information available to it:

  • The line number of the string Sed passed to it
  • A Map of key/value pairs the helper function could use while processing the file

Note: In this article “Sed” refers to my project, and “sed” refers to the Unix command-line utility.

Back to top

Basic use

In a “basic use” scenario, this is how I use the new version of Sed in a Scala shell script to change the “layout:” lines in 55 Markdown files whose names are in the files-to-process.txt file:

Scala shell scripts hang indefinitely on MacOS 10.13 and 10.14 (won’t run)

If you run into a problem where a Scala shell script won’t run on MacOS — it hangs indefinitely without doing anything — hopefully this bug report will help. The solution is to change this line at the beginning of the Scala shell script:

exec scala -savecompiled "$0" "$@"

to this:

exec scala -nocompdaemon -savecompiled "$0" "$@"

I just had this problem with Scala 2.12.x and Java 8 running on MacOS 10.14.4, and I can confirm that adding -nocompdaemon solved the problem for me.

A Scala method to write a list of strings to a file

As a brief note today, here’s a Scala method that writes the strings in a list — more accurately, a Seq[String] — to a file:

def writeFile(filename: String, lines: Seq[String]): Unit = {
    val file = new File(filename)
    val bw = new BufferedWriter(new FileWriter(file))
    for (line <- lines) {
        bw.write(line)
    }
    bw.close()
}
How to run shell commands from the Scala REPL alvin June 27, 2019 - 8:55pm

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 14.4, “How to run a shell command from the Scala REPL.”

Problem

You want to be able to run a shell command from within the Scala REPL, such as listing the files in the current directory.

Solution

Run the command using the :sh REPL command, then print the output. The following example shows how to run the Unix ls -al command from within the REPL, and then show the results of the command: