Posts in the “scala” category
SBT/build.sbt: One percent symbol or multiple percent symbol for Java and Scala dependencies?
As a brief note today, when you’re using SBT and its build.sbt file with Scala projects, use %%
in library dependencies when referring to Scala-specific libraries, and use only one %
character when referring to other JVM dependencies, like Java dependencies. Here are some examples of this syntax:
Scala SBT: How to “re-run with -deprecation” (or -feature)
Scala FAQ: When compiling a Scala application with SBT, I get warning messages like these:
$ sbt compile [warn] there were 6 deprecation warnings; re-run with -deprecation for details [warn] there were 4 feature warnings; re-run with -feature for details
How do I ’re-run with -deprecation’ or ’re-run with -feature’?
Scala: How to use startsWith tests in match/case expression
Scala FAQ: How can I use the startsWith
method on a Scala String
to match multiple possible patterns in a match
expression?
Solution
As shown in the following example, you can use the startsWith
method on a String
to match multiple possible patterns in a match
expression. startsWith
checks to see if a String
starts with the prefix (or substring) you specify, so although in these examples I use complete strings, you can also use regular expression patterns.
Example: startsWith + match expression
Scala: What is the Nothing data type?
Scala FAQ: What is the Nothing
type in Scala, and how do I use it?
Solution
In Scala, the Nothing
type is called a bottom type, which means it is a sub-type of every other type in the Scala type system. It is also specifically a data type that has no instances.
In practical use, Nothing
is used to indicate that a computation or function will never produce a result normally, either because it throws an exception, enters an infinite loop, or encounters some other abnormal termination.
Visually, this is what the Nothing
type looks like in the Scala type hierarchy (image courtesy of this scala-lang.org page):
Scala: Common uses of Nothing
Some common use cases of Nothing
in Scala include:
A Scala current date and time example
Scala date/time FAQ: How do I get the current date and time in Scala?
The following code demonstrates how to get the current time in Scala, and then further shows how to get other information, such as the current minute, using the Java SimpleDateFormat class:
Scala/Java/Kotlin: How to replace left and right brackets in a String (replaceFirst, replaceAll)
Scala/Java/Kotlin String
FAQ: How do I replace left and right brackets — the [
and ]
characters — in a String
when using methods like replaceFirst
and replaceAll
?
Solution
If you’re using Scala, Java, Kotlin, or other JVM languages, and need to replace left or right brackets in a String
, I found the following solution, which seems to work well with String
methods like replaceFirst
and replaceAll
.
A Scala Either, Left, and Right example (like Option, Some, and None)
Summary: This post is a discussion of the “Option
/Some
/None
Pattern” in Scala, specifically how to use the Either
/Left
/Right
data types instead of Option when you need to know the reason some code failed. As you may know, the None data type does not return failure/exception information, but if you use the Either
/Left
/Right
types, you can access that failure information through the Left
type.
The post is sponsored by my book, the 2nd Edition of the Scala Cookbook.
Scala for/yield examples (for-loop and yield syntax)
I just found some notes from when I first began working with Scala, and I was working with the yield
keyword in for
loops. If you haven't worked with something like yield
before, it will help to know how it works. Also, when you need to do searches for problems, or when you want to talk to other Scala developers, it will also help to know that when you use the for
/yield
keywords as shown in these examples, you’re creating something known as a for expression.
How a “for expression” works
Here's a statement of how the yield
keyword works in for
loops, from the book, Programming in Scala:
Scala List class examples: range, fill, tabulate, appending, foreach, more ...
Scala List FAQ: Can you share some Scala List
class examples?
The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.
In this tutorial, I'll share examples of the most common List
operations (methods).
How to add elements to a List in Scala (List, ListBuffer)
Scala List FAQ: How do I add elements to a Scala List
?
Solution
"How do I add elements to a Scala List
” is actually a trick question, because you can't add elements to a Scala List; it's an immutable data structure. If you’ve ever used the Java String
type, it’s just like that.
That being said, in the following sections I’ll show what you can do.
Prepending elements to Scala Lists
The most common way to “add” elements to a Scala List
is to create a new List
from an existing List
by prepending elements to the existing list. We do this all the time in functional programming in Scala, and the general approach looks like this in the Scala REPL:
A Scala 3 cheat sheet
This is a “cheat sheet” for the Scala 3 programming language. I initially wrote it for my book Learn Functional Programming The Fast Way, but I thought I’d share it here as well.
If you want to see many more details about Scala 3, see:
- The free, online Scala 3 Book (which I co-wrote)
- My book, Learn Scala 3 The Fast Way!
- The Scala Cookbook (2nd Edition)
- Functional Programming, Simplified
- My free 115-page e-book, Learning Recursion
ScalaJ-HTTP examples: GET, POST, handling redirects
If you ever need some good ScalaJ-HTTP examples, see the test files in the project, including this HttpBinTest.scala file. That file currently shows a number of good ScalaJ-HTTP examples, including GET, POST, and redirect examples with Scala.
See that page for a full list of examples, but for my own use, here are a few of them.
The Scala 3 if/then/else-if/else/end syntax and examples
Scala FAQ: Can you show an example of the Scala 3 if-then-else-if syntax?
Scala 3 solution
Here’s an example of the Scala 3 if-then/else-if/else syntax, as used as the body of a method:
Two ZIO, Scala CLI, and Scala 3 examples
If you’d like to get started working with the Scala ZIO functional programming (FP) library, here are two little ZIO 101 and ZIO 102 “Hello, world” examples that you can run with Scala CLI and Scala 3.
This post is sponsored by my new book,
Learn Functional Programming Without Fear.
FP: ZIO + Scala CLI example: “Hello, world” 101
First, here’s a complete ZIO “Hello, world” example that shows everything you need to get started using ZIO with Scala CLI:
Creating a Thread (and Runnable) in Scala
I ran into a strange problem this weekend where I noticed a huge difference between the way a Scala Future
and a Thread
worked for the exact same code. I think I’m pretty aware of the obvious differences between futures and threads, so this really caught me by surprise. If/when I figure out why there was such a difference in behavior for the given problem I’ll post that information here.
A Scala Thread example
While that problem will haunt me for a while, what I really want to post here today is a simple Scala Thread
example:
How to convert a Scala Array/List/Seq (sequence) to string with mkString
Scala collections FAQ: How can I convert a Scala array to a String? (Or, more, accurately, how do I convert any Scala sequence to a String.)
A simple way to convert a Scala array to a String is with the mkString method of the Array class. (Although I've written "array", the same technique also works with any Scala sequence, including Array, List, Seq, ArrayBuffer, Vector, and other sequence types.)
Scala if then else syntax (and returning a value from an if statement)
Scala FAQ: Can you share some examples of the Scala if/then/else syntax? Also, can you show a function that returns a value from an if/then/else statement?
In its most basic use, the Scala if/then/else syntax is very similar to Java: