match
Table of Contents
I originally wrote a long introduction to this article about Scala Options, but I decided to keep that introduction for a future second article in this series. For this article I’ll just say:
- idiomatic Scala code involves never using null values
- because you never use nulls, it’s important for you to become an expert at using
Option
,Some
, andNone
- initially you may want to use match expressions to handle
Option
values - as you become more proficient with Scala and Options, you’ll find that match expressions tend to be verbose
- becoming proficient with higher-order functions (HOFs) like
map
,filter
,fold
, and many others are the cure for that verbosity
Here’s a little Scala 3 (Dotty) enum
match/case expression example:
This is a lesson on Scala match/case expressions from my book, Hello, Scala.
Back to topScala 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.
Table of Contents
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 topBasic 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:
A good reason to use sealed traits and classes in Scala
This scala-lang.org documentation page shares a good reason to use “sealed” traits and classes: When you created sealed traits, the compiler can easily tell all of the subtypes of your class or trait, and as just one benefit, you don’t need to add a default, “catch-all” case in your Scala match
expressions.
Scala: How to use regex pattern matching in a match expression
Scala FAQ: How can I use regular expression (regex) pattern matching in a match
expression (a Scala match/case expression)?
As I wrote in my Scala sed class post earlier today, Jon Pretty’s Kaleidoscope project lets you use string pattern-matching code in Scala match
expressions. This enables regex pattern-matching code like this:
- Read more about A little Scala `sed` class
- Log in to post comments
A few times during the past year I got tired of trying to remember the Unix/Linux sed
syntax while wanting to make edits to many files, so this weekend I wrote a little sed
-like Scala class.
- Read more about Scala: How to use fold on an Option (syntax)
- Log in to post comments
Scala/Java: How to write a pattern that matches a minimum to maximum number of specified characters
If you’re using Java or Scala and need to write a pattern that matches a range of characters, where those characters occur between a minimum and maximum number of times in the pattern, the following example shows a solution I’m currently using.
The idea is that the pattern "[a-zA-Z0-9]{1,4}"
means, “Match a string that has only the characters a-z, A-Z, and 0-9, where those characters occur a minimum of one time and a maximum of four times.” The following tests in the Scala REPL shows how this works: