Posts in the “scala” category

A Scala case class ‘copy’ method example

Scala FAQ: When you create a case class in Scala, a copy method is generated for your case class. What does this copy method do?

In short, it lets you make a copy of an object, where a “copy” is different than a clone, because with a copy you can change fields as desired during the copying process. The copy method is important in functional programming, where values (val) are immutable.

Scala SBT tool history commands

When you're using the SBT tool with your Scala projects, it can be helpful to list your "sbt history", the history of your SBT commands. Courtesy of the SBT tool itself, here are the history commands you can use from the sbt command line:

The Scala spectrum: OOP to Pure FP

Scala is a great language in many ways. One great feature is that you can use it as a “Better Java” (i.e., as an OOP language), and you can also use it as a pure FP language. While some people prefer one extreme or the other (not unlike extremist Republicans and Democrats in the U.S.), I appreciate that this lets you find a “Middle Way” of using the best features of both approaches.

Scala REPL: How to show more methods on a class/object in the REPL

When you're working in the Scala REPL and want to see what methods are available on a class/object, you can create an instance of an object, follow that with the "." character, and then press the [Tab] key. This process, known as "tab completion" in the REPL, gives you a preliminary list of methods that can be called on the object.

Here's what this looks like when we try it on an Int object:

Scala FP idiom: Methods should not have side effects

An functional programming idiom, and therefore a Scala idiom, is that functions and methods should not have side effects. As written in the book Programming in Scala:

A method's only act should be to compute and return a variable.

underscore.io books

The people at underscore.io have made their books on Scala and functional programming free (or “donationware,” if you prefer). I’ve found the Advanced Scala with Cats book to be particularly good, and well worth a donation.

(Full disclosure: I didn’t pay for the Advanced Scala book when I first downloaded it, then went back and tried to pay for it, but the Gumroad website wouldn’t let me do that.)

Just trying to be a reporter

After the 0.1.2 release of Learning Functional Programming in Scala, it occurs to me that I need to be more explicit about my goals for the book. Some people seem to think that I’m trying to “sell” functional programming. That’s not the case at all. I’m just trying to be a reporter and explain what I’ve learned about FP after reading dozens (hundreds?!) of articles and many books on FP, learning Haskell, trying to apply these techniques to my own code, etc. I’ll explain this further in the next release of the book.

Scala advanced language features

Intellij IDEA has a reall nice help-tip hover tool that helps to explain some of Scala’s advanced language features. In this case the code Monad[M[_]] is a higher-kinded type, which I hope to explain more in my new book.

ScalaTest 105: Adding more tests and a test suite

Problem: You want to add more unit tests and a main test suite to your ScalaTest tests.

Solution

To add more unit tests to your project, just create new test classes. For instance, to add a set of TDD-style tests for the Topping class, just create a ToppingTests class in the src/test/scala/com/acme/pizza directory:

Scala methods: dots, spaces, and parentheses

If you've started using Scala, you've seen that there are some cool things in the Scala syntax compared to Java. For instance, in Java you'd execute a method on an object like this:

object.method();

But in Scala you definitely don't need the semi-colon:

object.method()

and then you can omit the parentheses: