scala

Tutorials about the Scala programming language.

Functional programming in Scala: Kindle eBook, coming soon alvin October 16, 2017 - 6:21pm

In the “Good News” department, apparently a long time ago when I was pretty sick with the MCAS, I wrote a series of Scala scripts to help convert a LaTeX document into an Amazon Kindle eBook. As a result, my book on functional programming in Scala should be available as a Kindle eBook later this week.

Scala: When the sum of a list is a very large number alvin October 11, 2017 - 8:55am

As a brief note, I knew that the sum function I wrote in my book on Scala and functional programming would return a wrong value if the sum of the integers in a list was greater than Int.MaxValue, and I was curious how the built-in sum method on lists handled the same problem. It turns out that it works in the same way.

So, if you need to sum a list of integers that may exceed Int.MaxValue, you might need to write a sum function that calculates and returns the sum as a Long. (The same is true for Float and Double.)

Scala: Examples of for-expressions being converted to map and flatMap alvin October 3, 2017 - 6:03pm

Without any explanation, here are a couple of source code examples from my book, Learning Functional Programming in Scala. The only thing I’ll say about this code is that I created it in the process of writing that book, and the examples show how the Scala compiler translates for-expressions into map and flatMap calls behind the scenes.

Passing an anonymous function to SwingUtilities.invokeLater (in Scala) alvin October 3, 2017 - 5:47pm

As a quick note, if you ever need to call the invokeLater of the Java SwingUtilities class in Scala, you can pass it an anonymous function (lambda) like this:

SwingUtilities.invokeLater(() => {
    // your gui-updating code here, such as:
    for (c <- newsControllers) c.updateContent()
})

(I haven’t worked with Java 8 lambdas too much, but I assume that the Java syntax is similar to that.)

A milestone day alvin September 27, 2017 - 5:10pm

September 26, 2017 is a little bit of a celebration day for me. It’s the day I reached the “No new content” milestone of my book on Scala and functional programming. At this point I’ll keep editing the book contents, and I really need to work on its formatting, but I don’t have any plans to write any new lessons.

Working on the book’s index alvin September 26, 2017 - 11:49am

This past week I started working on the index for my book on Scala and functional programming. In retrospect I wish I had written the book using LaTeX (or some other technology) rather than Markdown; I would have started this process long ago.

Pure functions, black holes, and miracles alvin September 21, 2017 - 7:21pm

A note on pure functions, black holes, and miracles. From my book, Learning Functional Programming in Scala.

Future Board (like Flipboard, with Scala Futures) alvin September 19, 2017 - 5:07pm

I’m working on a little app for my Scala & functional programming book I currently call “Future Board.” It works a little like Flipboard in getting news headlines from different sources, but it uses Scala Futures and a few other functional programming techniques.

“Monad transformers are not too intuitive” alvin September 19, 2017 - 9:50am

“Monad transformers are not too intuitive, especially in Scala, and are known to produce hard to understand code structure.”

~ Debasish Ghosh, Functional and Reactive Domain Modeling

A look at how exceptions work with Scala Futures and the onComplete ‘Failure’ case alvin September 18, 2017 - 12:53pm

Here’s a little example of how exceptions work with Scala Futures, specifically looking at the onComplete ‘Failure’ case.

In this example I start three Futures that run for different lengths of time, and the shortest-running Future throws an exception: