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:

Scala String examples: 100+ examples, methods, anonymous functions (lambdas)

[toc]

This page contains a collection of over 100 Scala String examples, including string functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a Scala String reference page or cheat sheet. (I do show the output of most examples.)

Scala: How to use ‘fold’ on an Option (syntax, examples)

If you want to get a value out of a Scala Option type, there are a few ways to do it. I’ll show those approaches in this article, and then discuss the approach of using the fold method on an Option (which I’ve seen discussed recently).

1) Getting the value out of an Option, with a backup/default value

As a first look at getting values out of an Option, a common way to extract the value out of a Scala Option is with a match expression:

Scala 3: Functions: Options and Functional Error Handling (Part 1)

NOTE: This is a chapter from my book, Learn Scala 3 The Fast Way. Due to a mistake, this lesson was not included in the book.

When you write functions, things can go wrong. In other languages you might throw exceptions or return null values, but in Scala you don’t do those things. (Technically you can, but other people won’t be happy with your code.)

Instead what we do is work with error-handling data types. To demonstrate this I’ll create a function to convert a String to an Int.

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 fastest way to learn functional programming (for Java/Kotlin/OOP developers)

When I was writing my new functional programming book — Learn Functional Programming Without Fear — a few alternate titles for the book were:

  • Learn Functional Programming the Fast Way!
  • The Fastest Way to Learn Functional Programming
  • From Object-Oriented Programming to Functional Programming
  • Helping Object-Oriented Programmers Learn Functional Programming
  • Functional Programming for Objected-Oriented Programmers

That’s because I found out — almost by accident — that the fastest way for object-oriented programming (OOP) developers to learn functional programming (FP) goes like this:

For OOP developers: The smallest, simplest functional programming book

Some of the computer programming books on the right side of this image are amazing, and I would never discourage anyone from reading the great ones.

But if you’re an object-oriented programming (OOP) developer who wants to start understanding functional programming (FP) over a weekend or a few nights of reading, that’s the goal of the new little book on the left: Learn Functional Programming Without Fear. It takes you from Java/OOP to functional programming in the simplest possible step-by-step learning process.

FP for Java/Kotlin/OOP developers

I taught Java and OOP for many years, and have used other OOP languages like Kotlin, Python, and Flutter/Dart, so I understand both OOP and FP, and I think that helped to make this a simple book for learning FP.

I also wrote the FP book in the middle — Functional Programming, Simplified — and it’s about three times larger than the little book on the left. I based it on most of the FP books on the right, and it goes into many details that the book on the left doesn’t go into. It’s big, but it’s still easier than reading all the books on the right.

Learn Scala 3 for just $10

I’ve been slowly working on a series of new Scala programming books, and today I’m proud to announce the first of these:

Learn Scala 3 The Fast Way! (book cover)

Starting today you can buy the PDF version of Learn Scala 3 The Fast Way! for just ten dollars — $10 (USD) — at this Gumroad.com URL.

Scala: What do “effect” and “effectful” mean in functional programming?

When you get started with functional programming (FP) a common question you’ll have is, “What is an effect in functional programming?” You’ll hear advanced functional programmers use the words effects and effectful, but it can be hard to find a definition of what these terms mean.

Effects are related to monads (but don’t worry)

A first step in the process of understanding effects is to say that they’re related to monads, so you have to know just a wee bit about monads to understand effects.

But fear not, this is surprisingly simple. As I write in my book, Functional Programming, Simplified, a slight simplification is to say that in Scala, a monad is any class that implements the map and flatMap methods. Because of the way Scala for-expressions work, implementing those two methods lets instances of that class be chained together in for-expressions (i.e., for/yield expressions).

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.

Some Scala Exception ‘allCatch’ examples

At the time of this writing there aren’t many examples of the Scala Exception object allCatch method to be found, so I thought I’d share some examples here.

In each example I first show the "success" case, and then show the "failure" case. Other than that, I won’t explain these, but hopefully seeing them in the REPL will be enough to get you pointed in the right direction:

Scala: How to use higher-order functions (HOFs) with Option (instead of match expressions)

I originally wrote a long introduction to this tutorial about how to work with the Scala Option/Some/None classes, but I decided to keep that introduction for a future article. 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

Given that background, the purpose of this article is to show how to use HOFs rather than match expressions when working with Option values.

Scala List class examples: range, fill, tabulate, appending, foreach, more ...

Scala List FAQ: Can you share some Scala List class examples?

The Scala List class may be the most commonly used data structure in Scala applications. Therefore, it's very helpful to know how create lists, merge lists, select items from lists, operate on each element in a list, and so on.

In this tutorial, I'll share examples of the most common List operations (methods).

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: