Table of Contents
- Run one task, but block
- Run one thing, but don’t block, use callback
- The onSuccess and onFailure callback methods
- Creating a method to return a Future[T]
- How to use multiple Futures in a for loop
- A future and ExecutionContext
- Callback methods
- For-comprehensions (combinators: map, flatMap, filter, foreach, recoverWith, fallbackTo, andThen)
- See Also
- The Scala Cookbook
You want a simple way to run one or more tasks concurrently in a Scala application, including a way to handle their results when the tasks finish. For instance, you may want to make several web service calls in parallel, and then work with their results after they all return.Back to top
Future gives you a simple way to run an algorithm concurrently. A future starts running concurrently when you create it and returns a result at some point, well, in the future. In Scala, it’s said that a future returns “eventually.”
Problem: In the process of learning Scala, or trying to understand a particular problem, you want to examine the source code and bytecode the Scala compiler generates from your original source code.
Solution: You can use several different approaches to see how your Scala source code is translated:
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.”
You want to be able to run a shell command from within the Scala REPL, such as listing the files in the current directory.
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:
Over time I’ve discovered a number of things that I have no memory of from the years 2014 to 2016, when I was at my sickest with the mast cell disease. Apparently I created this image and wrote these words on August 24, 2015. (Here’s a link to the original post, if you’re into that sort of thing.)
Scala problem: You want to define an
equals method for your class so you can compare object instances to each other.
If you’re new to Scala, a first thing to know is that object instances are compared with
"foo" == "foo" // true "foo" == "bar" // false "foo" == null // false null == "foo" // false 1 == 1 // true 1 == 2 // false 1d == 1.0d // true case class Person(name: String) Person("Jess") == Person("Jessie") // false
This is different than Java, which uses
== for primitive values and
equals for object comparisons.
“Writing a book is an adventure. To begin with it is a toy and an amusement. Then it becomes a mistress, then it becomes a master, then it becomes a tyrant. The last phase is that just as you are about to be reconciled to your servitude, you kill the monster and fling him to the public.”
~ Winston Churchill
A funny thing about writing books, or at least writing books with O’Reilly in 2013, is that I never received a final copy of the Scala Cookbook in PDF format. Fortunately I have the original Word docs, which is what they used at the time, so I carry those around on my laptop.
I’ll guess that nobody in the U.S. Congress has read it, but the Scala Cookbook is in the Library of Congress, which is kinda cool.
I doubt that most people know it, but authors always enjoy receiving letters of “thanks” like this one. Writing is often a lonely, solitary business, and to hear that your work has helped other people is always satisfying. If there’s a book you’ve read that you really enjoyed, I encourage you to write the author a brief letter of thanks. They’ll appreciate it more than you’ll know.