Posts in the “scala” category

How to access a character in a Scala String (or, an element in a sequence)

When coming to Scala from Java, the syntax for accessing a character at a position in a Scala String is an interesting thing. You could use the Java charAt method:

scala> "hello".charAt(0)
res0: Char = h

However, the preferred (proper) approach to access a character in a String is to use Scala’s Array notation:

Functional Programming, Simplified: Updated for Scala 3 (book)

I’m currently updating my best-selling book, Functional Programming, Simplified, to cover Scala 3 and other technologies like ZIO and Cats Effect, and when that’s done, this will be the landing page for that book.

Until then, you can find the PDF for the new book that I’m creating here:

And you can find the Scala 2 version of the book here:

I’ll update this page as I have more information.

The easiest, simplest way to learn functional programming?

I hope that my new book, Learn Functional Programming Without Fear, is the easiest and simplest way to learn functional programming (FP). I didn’t initially intend for it to be an FP book, but as I was writing about pure functions, immutable (algebraic) variables, immutable data structures, and functional error-handling, I realized that these were the first four essential stepping stones to help you transition from OOP to FP, and to Scala/FP libraries like ZIO and Cats Effect.

Initially I was planning to write a book titled something like “Thinking with Types” or “Solving Problems with Pure Functions,” but once I realized where it was going, I decided to create a new functional programming book that’s much shorted than my own, Functional Programming, Simplified book.

What’s the easiest way to learn functional programming?

People occasionally ask me, “What’s the easiest way to learn functional programming?” If you look at all of the books on the right side of this image, I can tell you that reading all of those books wasn’t an easy way to learn functional programming (FP):

IMHO there’s a much easier way to learn the FP basics: I’ve made almost 40% of my book, Functional Programming, Simplified, freely available.

The lessons in the free PDF you can find at that URL include:

A Scala Adler-32 checksum algorithm

While fooling around recently with various computer programming algorithms, I ended up writing an implementation of the Adler-32 checksum algorithm in Scala. There isn’t too much to say about it, other than I hope I got it right. My results for the simple test below matched the results shown on the Adler-32 Wikipedia page, so that’s encouraging. :)

Here's the Scala source code for my Adler-32 checksum implementation:

A “Minority Report” Monte Carlo simulation in Scala

This article shares the source code for a Monte Carlo simulation that I wrote in Scala. It was inspired by the movie Minority Report, as well as my own experience.

Background

For the purposes of this simulation, imagine that you have three people that are each “right” roughly 80% of the time. For instance, if they take a test with 100 questions, each of the three individuals will get 80 of the questions right, although they may not get the same questions right or wrong. Given these three people, my question to several statisticians was, “If two of the people have the same answer to a given question, what are the odds that they are correct? Furthermore, if all three of them give the same answer to a question, what are the odds that they are right?”

Scala 3: Generic type parameters and variance explained (type system)

Note: This tutorial is an excerpt from the Scala Cookbook, 2nd Edition.

As you can tell from one look at the Scaladoc for the collections classes, Scala has a powerful type system. However, unless you’re the creator of a library, you can go a long way in Scala without having to go too far down into the depths of Scala types. But once you start creating libraries for other users, you will need to learn them. This chapter provides recipes for the most common type-related problems you’ll encounter.

Scala’s type system uses a set of symbols to express different generic type concepts, including the concepts of bounds, variance, and constraints.

A unique Patreon gift: My book covers on coffee mugs

If you’re looking for a unique gift for yourself or the Scala computer programmer in your life, I just created these coffee mug designs here on my Patreon account. There are two different designs, one with four book covers and another with three book covers, and both of them completely wrap around the mug.

Supporting my Patreon account helps to keep me writing and teaching about Scala, so I’m trying to find new and unique ways to thank my supporters. If you like coffee, tea, or any other beverage in a mug, I hope you’ll like these new designs.

Best FP book for OOP, Java, and Kotlin developers?

October, 2022: I just released my new book, Learn Functional Programming Without Fear, and my aim is to make it a terrific functional programming (FP) book for all OOP developers, especially Java and Kotlin programmers.

As a personal statement, I have no interest in pushing FP on anyone, but if you’re an OOP developer who is interested in FP, I hope this book is helpful. If you have some experience programming in Java, Kotlin, and OOP, I believe you’ll be able to read the whole book over a weekend, or in several nights during a week.

Update: It’s now December, 2022, and the book is still a “#1 New Release” on Amazon!

Why Java and Kotlin?

Why Java and Kotlin?:

  • For both of them, it’s because they’re JVM-based languages (or at least they were initially).
  • For Java developers, it’s because Java has incorporated several aspects of Scala over the last 12 years (including features like lambdas and immutable collections classes)
  • And for Kotlin developers, it’s because Kotlin and Scala are so similar. (My understanding is that Martin Odersky even consulted on the creation of Kotlin.) I’ve seen people say that if you know Scala, you can learn Kotlin in a week, and vice-versa.

FP for Java, Kotlin, and OOP developers

Scala News This Week: January 22 to January 28, 2023

Last week I started a This week in Scala page, and I’m going to see if I can do another one this week. Without further ado:

This week in Scala: January 15-21, 2023

I don’t know if I’ll keep this going or not, but I was looking for a way to monitor “What’s new in Scala,” so I thought I’d create a “This week in Scala” page. Here’s what’s new the week of January 15-21, 2023:

On the personal front:

Scala SBT and build.sbt syntax and examples

Note: This page is very much a work in progress.

This page shows a sample Scala SBT build.sbt file, including the last line, which handles the SBT 're-run with -deprecation for details' warning message. (If you get the 're-run with -deprecation' message, that last line hands the "-deprecation" option over to the compiler, so you can see the deprecation problems.)

SimpleTest, a simple “testing framework” for Scala

For the purposes of writing the Scala Cookbook (2nd Edition), I thought I wanted a little “testing framework.” At first when I tried to use existing frameworks, I found that they didn’t always work with the latest release of Scala 3.0 (formerly named Dotty). Then, after working with the libraries that were often kept up to date with Scala 3.0, I further decided that I just wanted to put my tests inside Scala @main methods — I didn’t want to have some code under src/main and other code under src/test in my Scala/sbt projects.

So, in the end, I created my own testing “framework,” which I named SimpleTest. It’s really not a framework, it’s just a very little library that lets me run tests in @main methods.