Posts in the “scala” category

Is Scala DICEE?

If you’ve never heard of the term DICEE, it was coined by Guy Kawasaki. Mr. Kawasaki was a developer evangelist for the original Macintosh team in the 1980s, and used the term in at least one subsequent book to refer to great products.

“DICEE” is an acronym that stands for Deep, Indulgent, Complete, Elegant, and Emotive:

How I came to write the Scala Cookbook

The funny thing about writing the Scala Cookbook is that it started as a whim. I was just about to leave for a vacation at the beach, and right before I turned off the computer a thought flashed in my mind, “I should contact the people at O’Reilly about writing a cookbook for Scala.” I then had a doubt that they would actually do it, but I applied the “What the heck” rule — i.e., “What the heck, what do I have to lose?” — and sent the email.

I dug around the internet for a few minutes, found the correct O’Reilly email address, sent them a message, turned off the computer, and drove to the beach. While I was at the beach the publisher wrote and said, “Love it, send me a full proposal!”

So if you’re thinking about doing something, but are afraid or uncertain about doing it ... apply the “What the heck” rule, and give it a shot. :)

A little Scala program to count lines of source code in the Scala Cookbook

As a little mini-project I wanted to count the number of lines of source code in the Second Edition of the Scala Cookbook as compared to the First Edition. To do this I wrote the following Scala program/script to count the lines between the ---- and .... sections in the AsciiDoc files that represent the old and new versions of the book:

How to convert a Set to a SortedSet in Scala

Without much explanation today, here are different ways to create a sorted set from a Set in Scala:

val set = Set(1, 6, 2, 12, 7, 3, 11)
val ss = collection.immutable.SortedSet[Int]() ++ set
val ss = collection.immutable.TreeSet[Int]() ++ set
val ss = collection.mutable.SortedSet(set.toList: _*)

Here’s what this looks like in the REPL:

scala> val set = Set(1, 6, 2, 12, 7, 3, 11)
set: scala.collection.immutable.Set[Int] = Set(1, 6, 2, 12, 7, 3, 11)

scala> val ss = collection.immutable.SortedSet[Int]() ++ set
ss: scala.collection.immutable.SortedSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12)

scala> val ss = collection.immutable.TreeSet[Int]() ++ set
ss: scala.collection.immutable.TreeSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12)

scala> val ss = collection.mutable.SortedSet(set.toList: _*)
ss: scala.collection.mutable.SortedSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12)

I haven’t done any performance comparisons on these approaches, I just thought I’d share them here today.

Note that if you want to convert a Scala Set to a sorted sequence, you can also use this approach:

val sortedSeq = set.toSeq.sorted

Second Edition of the Scala Cookbook

It’s super-early in the process, but the Second Edition of the Scala Cookbook is slowly coming to life. I’m currently updating all of the content for Scala 2.13, and when the book is finished it will be updated for Scala 3.

This morning (January 28, 2020) the folks at O’Reilly released the first two chapters of the new, updated book on the O’Reilly Learning Platform. If you have an O’Reilly account you can start reading the new chapters here. If not, you can view the catalog page here.

Scala: How to create a range of characters (list, sequence)

Scala range FAQ: How can I easily create a range of characters in Scala, such as a range of alpha or alphanumeric characters?

I learned recently that you can easily create a range of characters (a Range) as shown in the following examples. Here’s how you create a basic 'a' to 'z' range:

How to flatten a List of Lists in Scala with flatten

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 10.15, “How to Flatten a List of Lists in Scala with flatten

Problem

You have a list of lists (a sequence of sequences) and want to create one list (sequence) from them.

Solution

Use the flatten method to convert a list of lists into a single list. To demonstrate this, first create a list of lists:

Scala: How to add new methods to existing classes

Update: This article was written for Scala 2.9. Things changed a little bit in Scala 2.10, so see this new article, Creating implicit methods in Scala 2.10, for correct examples for 2.10 and newer versions of Scala.

A cool thing about implicit conversions in Scala is that they let you add new methods to existing classes, including existing Java and Scala classes such as String, File, and so on.

How to define a Scala method to take an implicit parameter

UPDATE: This example shows how to create an implicit method in Scala 2.9 or older. You can use a slightly simpler approach with Scala 2.10 and newer, which I've documented in this Scala 2.10 implicit class example.

I'm not going to do much writing here today, but instead I'll just demonstrate how an implicit method argument works with implicit fields in Scala. Without any further ado, here's some code:

Scala Option, Some, None syntax examples

Today I’m sharing some examples of the Scala Option/Some/None syntax. These examples will show how to use an Option for the var fields in a Scala class. Then I’ll show how to set those Option fields, and then get the values from the Option fields.

To get started, we’ll need a little case class to represent an Address:

How to convert from a Scala BigDecimal to Java BigDecimal

Scala FAQ: How do I convert from a Scala BigDecimal instance to a Java BigDecimal?

Call the bigDecimal method on your Scala BigDecimal instance, as shown in this example in the REPL:

scala> val sb = scala.math.BigDecimal(12345)
sb: scala.math.BigDecimal = 12345

scala> val jb = sb.bigDecimal
jb: java.math.BigDecimal = 12345

As you can see, invoking sb.bigDecimal returns a java.math.BigDecimal.

How to convert between Scala and Java maps

Scala/Java integration problem: You need to share a Scala map with a Java method, or access a Java map in Scala code.

Solution

To use a Java map in Scala code, import the mapAsScalaMap method from Scala’s JavaConversions package, and perform the conversion.

Simply trying to use a Java HashMap with something like a Scala for loop won’t work. To demonstrate this, first create a simple Java HashMap:

Scaladoc-driven API design

I was working on some new code for my functional programming in Scala book today. At one point I thought everything looked okay, so I decided to generate some Scaladoc to see what certain things looked like. Admittedly I’m a bit tired today, but when I saw that Scaladoc I thought, “Good grief, Al, what sort of ugly API have you created?”

For some reason, seeing the Scaladoc helped me easily see the errors of my way. I’m not sure that I’ll ever be promoting a “Scaladoc-driven API design” process, but seeing the Scaladoc generated from my code sure helped today.

~ a note from August 30, 2017