scala

Tutorials about the Scala programming language.

Second Edition of the Scala Cookbook alvin January 28, 2020 - 11:26am

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 3: The Dotty if/then/else-if/else/end syntax alvin January 27, 2020 - 11:45am
Table of Contents1 - Using `then` with if/else2 - Using `end if` with if/else3 - Closing a function with `end`4 - Discussion5 - Participate/contribute!6 - Attribution

As a brief note today, here’s an example of the Scala 3 “Dotty” if/then/else-if/else syntax, as used in a function:

def compare(a: Int, b: Int): Int =
    if a < b
        -1
    else if a == b
        0
    else
        1
Back to top

Using `then` with if/else

You can also use the then keyword after your if expressions, if you prefer:

Scala: How to use higher-order functions with Option (instead of match expressions) alvin January 24, 2020 - 8:30am
Table of Contents1 - Sample data2 - From match expressions to higher-order functions3 - Notes4 - Resources

I originally wrote a long introduction to this article about Scala Options, but I decided to keep that introduction for a future second article in this series. For this article I’ll just say:

  • idiomatic Scala code involves never using null values
  • because you never use nulls, it’s important for you to become an expert at using Option, Some, and None
  • initially you may want to use match expressions to handle Option values
  • as you become more proficient with Scala and Options, you’ll find that match expressions tend to be verbose
  • becoming proficient with higher-order functions (HOFs) like map, filter, fold, and many others are the cure for that verbosity
A small Scala 2 project converted to Dotty alvin January 20, 2020 - 3:40pm

If you want to see a somewhat larger example of Dotty source code than I’ve shown before, I just took a little time to convert a small Scala 2 project over to the new/current Dotty syntax (i.e., the Dotty syntax supported by the Dotty 0.21 release, circa January, 2020).

Think of the Scala collections’ map method as “transform” alvin January 16, 2020 - 6:26pm

I’ve written this before, but when I saw this “pseudocode to Scala code” example in the book Functional Thinking, I thought it was worth mentioning again: If you have trouble grokking the Scala map method, think of it as being named transform instead. It transforms an input collection to an output collection, based on the algorithm you supply.

For those coming from the OOP world, I think “transform” is a better word because it is more meaningful, at least initially.

Scala 2.13’s ‘pipe’ and ‘tap’ chaining operations alvin January 12, 2020 - 5:08pm

Scala 2.13 introduced two new “chaining operations” named pipe and tap. Here’s a quick look at how they work, plus a little extra fun at the end.

Benchmarking Scala Collections alvin January 10, 2020 - 9:49am

Back in 2016, Li Haoyi put together this nice article titled, Benchmarking Scala Collections.

Scaladoc-driven API design alvin December 16, 2019 - 9:08am

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

The PDF of Functional Programming, Simplified is currently on sale for $15 alvin December 12, 2019 - 7:34am

I normally don’t like to have sales on my books, but the short story is that the PDF version of Functional Programming, Simplified is currently on sale for $15.