Posts in the “scala” category

Finding Akka Typed difficult to learn?

If you’re finding Akka Typed hard to learn, fear not, it took me almost two days to get the simplest of their Scala examples working. And I had already written the original Scala Cookbook, and already knew how to use Akka Classic!

As a remedy to this problem, I wrote about how to use Akka Typed in the 2021 version of the Scala Cookbook. In the book I show some of the simplest possible Akka Typed examples, so you won’t have to waste two days of your life trying to get a simple example running.

Scala 3: Multiple packages in one file (syntax, examples)

As a brief note today, if you want to have multiple packages and classes in one file in Scala 3, this syntax should do the trick for you:

package com.acme.foo:
    class Foo:
        override def toString = "Foo"

package com.acme.bar:
    import com.acme.foo.Foo
    class Bar:
        val foo = Foo()

package com.acme.common:
    import com.acme.bar.Bar
    @main def multiPackageTest =
        val b = Bar()
        println(b.foo)

My Scala 3 SimpleTest source code (mini testing framework)

In the process of writing the Scala Cookbook for Scala 3, there were days when it was hard to find a working testing framework. As a result, I decided to write my own Scala 3 testing framework. Calling it a framework is a bit of a joke, as it’s only about 88 lines of source code. I named that library SimpleTest, and you can find it at github.com/alvinj/SimpleTest.

So I can easily find the source code later, I’m putting Version 0.3 of the source code here:

A Scala “Word of the day” shell script

I have a 19" monitor on the counter between my kitchen and living room, and it’s powered by a Raspberry Pi. I use the Linux Phosphor screen saver to show a scrolling “news and stock ticker” on the display, which I’ve programmed to show news from several different sources (Atom and Rss feeds, along with other news and data sources). An old version of the display looks like this:

My Raspberry Pi news ticker display

Today I added a new “Word of the day” feature to the display, and as with all of the other code, I wrote a Scala shell script to generate the output.

How to learn Scala cheaply and freely

Dateline July 15, 2021:

The book is about 250 pages long, and I could easily charge $25 for it. But I want to make them available at as low a cost as possible to make it as inexpensive to learn Scala as possible.

Note that this book was released in October, 2018, and covers Scala 2. If you want a free version of a similar book, the online version of the Scala 3 Book is completely free.

How to learn Scala 3: Resources

A lot of times people ask me, “How can I learn Scala 3?” Or, “I have this Scala 3 question, can you answer it for me?” I don’t mind doing that when I can, but there are still a lot of things I don’t know off the top of my head.

So, in an effort to help people find Scala 3 resources, here’s a quick list:

Also, if you’re using Scala 3 and have a specific question about it, I recommend asking that question here:

If you have a more general question about Scala 3, or even an idea on how to improve it, I recommend asking those types of questions here:

In summary, if you need some direct links to Scala 3 learning resources, I hope those are helpful.

Akka Typed: How to shut down Akka actors

Akka Actors Problem: You want to see the proper way to shut down Akka Typed actors.

(Note: This tutorial is written for Akka 2.6.)

Solution

There are two main ways to shut down Akka Typed actors:

  • Send them a “graceful shutdown” message, and let them shut themselves down by calling Behaviors.stopped

  • As a parent, stop a child actor with actorContext.stop(actorRef)

Akka Actors: How to delegate the work to the children

Akka Actors Problem: You know that an actor that “blocking” is bad for your Akka system, so you want to create actors that don’t block.

Solution

When using Akka actors, the mantra is always, “Delegate, delegate, delegate.” It’s important that high-level actors delegate work to lower-level actors, so the high-level actors can be free to receive new messages and respond to them immediately. This example shows how to implement non-blocking actors.

Akka Typed: Finding Actors with the Receptionist

Akka Problem: In some situations you can’t pass an ActorRef to another actor, so you want to see how to “look up” an Akka Typed actor so you can send a message to it.

Solution

There are at least two ways to find other Akka Typed actors, and both involve using the Akka Receptionist. The example shown here in the solution shows how to find an actor asynchronously, and the example shown in the Discussion shows how to use the ask method to find another actor synchronously.

In both examples I’ll use the idea of creating an actor system that works like Amazon Echo devices. The basic idea is that a device like this has ears to listen to you, a mouth to speak to you, and a brain to do all of its work. In these examples the Brain actor will need to discover the Mouth actor.

Version 0.1.4 of “Functional Programming, Simplified”

Version 0.1.4 of “Functional Programming, Simplified” is now available. The latest changes are:

- 13 new chapters on the StateT monad and monad transformers
- Ten new chapters on Domain Modeling
- Two new chapters on ScalaCheck
- New appendices on Anonymous Functions, and using def vs val in traits
- The PDF now has small 113 chapters, four appendices, and is 983 pages long

To accompany this latest update, the book is on sale for a few days.