Posts in the “scala” category

How to use ScalaCheck in the SBT console

If you add ScalaCheck to an SBT project like this:

libraryDependencies += "org.scalacheck" %% "scalacheck" % "1.13.4" % "test"

it’s only available in the SBT “test” scope. This means that when you start a Scala REPL session inside of SBT with its console command, the ScalaCheck library won’t be available in that scope.

To use ScalaCheck with the SBT console (REPL), don’t use its console command — use test:console instead. A complete example looks like this:

$ sbt

> test:console

scala> import org.scalacheck.Gen.choose

Note that after you type test:console your project may be compiled, so that step may take a few moments.

In summary, use SBT’s console command to start a “normal” Scala REPL inside SBT, and use test:console to start a REPL that you can run tests inside of. (Note that this same advice also applies to using ScalaTest or specs2.)

Writing at the beach

I’ve made some good progress on my new book on Scala and functional programming recently. For whatever reason I had been having writer’s block, so I came out to the beach for a little while to help clear out my brain, and today in particular was very productive. For a while now I’ve known how the book would end, but I was having a problem getting from where I was to the end, and I got through most of that today.

In a slightly related note, here’s a blurry photo of a military ship out on the ocean.

First draft of my Scala/FP book, coming soon

Health (and other things) permitting, I hope to have a first draft of my book on Scala and functional programming (now titled, Learning Functional Programming in Scala) completed by the end of May.

It may only be in an alpha or beta state by then, but I’m debating about making it available as an Amazon ebook for a low cost at that time. I’ll be going back to work almost immediately after that, so if I don’t release it now, it may be another year before I can really finish it.

Update: The first 600 pages of my book, Learning Functional Programming in Scala, are now available as a free PDF download.

STRef, or StateRef (using a State monad)

Here’s a link to a page by James Earl Douglas that I don’t quite understand yet, but also don’t want to forget. Here’s his intro to the problem, and then the image shows his solution.

Problem: You have a mutable variable (a var in Scala) that is both read from and written to outside of a tightly-scoped block.

Solution: Remodel the block as functions that take an initial value of the variable, and return both the final value of the variable and the original return value of the expression.

Scalameta: Meta programming (reflection) for Scala

If you’re interested in “meta” programming in Scala, check out the Scalameta project. It’s described on its website like this:

“Scalameta is a clean-room implementation of a metaprogramming toolkit for Scala, designed to be simple, robust and portable. We are striving for scalameta to become a successor of scala.reflect, the current de facto standard in the Scala ecosystem.”

“Scalameta provides functionality that's unprecedented in the Scala ecosystem. Our killer feature is abstract syntax trees that capture the code exactly as it is written — with all the original formatting and attention to minor syntactic details.”

Serializing and deserializing XML in Scala

Problem: You want to convert the data in a Scala class to its equivalent XML, or convert an XML representation of a class to an instance of the class.

Solution

There are two primary approaches to solving this problem:

A Scala “Null Object” example

A Null Object is an object that extends a base type with a null or neutral behavior. Here’s the Scala version of the Java example Wikipedia uses to demonstrate this:

trait Animal {
  def makeSound()
}

class Dog extends Animal {
  def makeSound() { println("woof") }
}

class NullAnimal extends Animal {
  def makeSound() {}
}

As you can imagine, later in your application you might have some code like this:

An Akka Actors Ping-Pong example

Here’s a 30-second “ping-pong” demo using Akka Actors:

The source code

If you want the source code, you can get it from the GitHub link shown at the end of this post. First, here’s a quick description of it.

The code is in two files, PingPong.scala and PingPongPanel.scala.

PingPong.scala contains three actors: