Posts in the “scala” category

A small Scala 2 project converted to Dotty (Scala 3)

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).

How to use Scala’s sortInPlaceBy method on mutable sequences (ArrayBuffer, ArrayDeque)

When I first looked at the sortInPlaceBy method that was introduced on mutable sequences in Scala 2.13, I couldn’t figure out exactly what it was supposed to do.

Unable to find any examples of “scala sortInPlaceBy” on planet Earth this evening (February 23, 2020), I had to resort to some actual work, and looked at the Scaladoc.

Reading the Scaladoc

This is what I see when I look at the Scaladoc for sortInPlaceBy on the ArrayBuffer:

def sortInPlaceBy[B](f: (A) => B)(implicit ord: Ordering[B]): ArrayBuffer.this.type

You can’t see by looking at that method what A is, so I scrolled up to the top of the page and saw this at the beginning of the Scaladoc:

How to use partially applied functions in Scala

Problem: You want to eliminate repetitively passing variables into a Scala function by (a) passing common variables into the function to (b) create a new function that’s pre-loaded with those values, and then (c) use the new function, passing it only the unique variables it needs.

Solution: The classic example of a partially applied function begins with a simple sum function:

Eugenio Moggi, monads, and functional programming

After reading this tweet, I wanted to take more of a formal look at monads in functional programming. So after some research on my own, I asked ChatGPT to summarize the work of Eugenio Moggi on monads, and got the following information, which I have edited slightly.

Eugenio Moggi’s work on monads in computer programming, particularly in his 1989 paper titled “Notions of Computations and Monads (PDF),” introduced a foundational concept that has had a profound impact on functional programming and the handling of computational effects.

Here is a summarized overview of his work:

Create nested DIV tags with Jsoup, Scala 3, and Scala-CLI

As a note to self, while working on my “Generate the Table Of Contents code for this website,” I just needed to do create some “nested DIV” content using Jsoup, Scala 3, and Scala-CLI, and with the help of ChatGPT, I came up with this working code:

Scala: How to create a None with a specific data type

As I note in How to use fold on a Scala Option, if you’re using Scala and need to create a None of a specific data type, either of these two approaches will solve the problem:

val none = Option.empty[Int]    // Option[Int]
val none: Option[Int] = None    // Option[Int]

Both of those approaches create the None with the type Option[Int], as shown in the Scala REPL:

scala> val none = Option.empty[Int]
val none: Option[Int] = None

scala> val none: Option[Int] = None
val none: Option[Int] = None

A short holiday “functional programming book” sale

As a brief note, the paperback versions of my Scala and functional programming books are all currently on sale. Each book is at least 10% off right now (December 27, 2023). This sale will end on January 2, 2024.

Here are links to the books on Amazon:

(Note that the links are “Amazon affiliate links,” which means that I get a small commission on each sale, which helps me offset these price reductions.)

Scala numeric data types: bit sizes, ranges, and docs (Byte, Short, Int, Long, Float, Double, Char)

Scala FAQ: What are the Scala numeric data types? How many bits do they use to store their data, and what is the range of those data types?

Courtesy of the excellent book, Programming in Scala, here is a list and description of the Scala data types, including bit sizes and data ranges:

Data Type  Definition

Boolean    true or false

Byte       8-bit signed two's complement integer (-2^7 to 2^7-1, inclusive)
           -128 to 127

Short      16-bit signed two's complement integer (-2^15 to 2^15-1, inclusive)
           -32,768 to 32,767

Int        32-bit two's complement integer (-2^31 to 2^31-1, inclusive)
           -2,147,483,648 to 2,147,483,647

Long       64-bit two's complement integer (-2^63 to 2^63-1, inclusive)
           -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Float      32-bit IEEE 754 single-precision float
           1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative)

Double     64-bit IEEE 754 double-precision float
           4.94065645841246544e-324 to 1.79769313486231570e+308 (positive or negative)

Char       16-bit unsigned Unicode character (0 to 2^16-1, inclusive)
           0 to 65,535

String     a sequence of Chars

Free “Introduction to Functional Programming” video course!

Dateline December 19, 2023: I’m pleased to report that the initial videos for my 100% FREE Introduction to Functional Programming video training course are now online!

In this course I expect to have about 50 videos total, and 14 are recorded, eight are being released today, and I’m editing and reviewing the other six.

As always, I want to provide a big Thank You to Ziverge for sponsoring these free videos. It takes a long time to create videos like this, and I couldn’t do this without them!

If you’re ready, the free training videos start at this link:

sbt: How to show the file/directory with 'doc' and 'package' commands

When you use sbt to build Scala projects — at least with version 1.4.4 in November, 2020 — if you use commands like doc, package, packageDoc, and probably assembly, sbt doesn’t show the directory where your output JAR files are written.

SBT: How to show file output locations

I don’t know why it doesn’t show those output locations — maybe people who use sbt all the time don’t want to see those things, such as if they have sbt doc or sbt package commands in a shell script — but if you want to see the directory and name of the files that sbt creates, use the show command before other commands like doc and package, like this:

How to reduce Ammonite REPL ‘type’ output verbosity

As a brief note to self, when you need to reduce the Scala Ammonite REPL verbosity, specifically the output that shows “type” information when you declare new variables, I just found that this command helps:

repl.pprinter() = repl.pprinter().copy(defaultHeight = 0)

From what I can tell from the docs, the defaultHeight defaults to 5, and this reduces it to 0. So when you have Ammonite type output that looks like res1 in this example:

Sample SBT build.sbt file from the Kyo project

The Scala SBT build tool uses a file named build.sbt to define your build. The SBT syntax can be a little hard to understand without examples, so I try to collect good examples here when I see them.

Seeing good examples rather than scrolling through reams of documentation has helped me many times!

To that end, this is a build.sbt SBT build file example from the Kyo project: