Posts in the “scala” category

Scala “split string” examples (field separator, delimiter)

Scala String FAQ: How do I split a String in Scala based on a field separator, such as a String I get from a comma-separated value (CSV) file or pipe-delimited file.

Solution

Use one of the split methods that are available on Scala/Java String objects. For instance, this example in the Scala REPL shows how to split a string based on a blank space:

Five ways to create a Scala List

Scala List class FAQ: How do I create a List in Scala? (Also asked as, how do I create and initially populate a List in Scala?)

You can create a Scala List in several different ways, including these approaches:

Scala for/yield examples (for-loop and yield syntax)

I just found some notes from when I first began working with Scala, and I was working with the yield keyword in for loops. If you haven't worked with something like yield before, it will help to know how it works. Also, when you need to do searches for problems, or when you want to talk to other Scala developers, it will also help to know that when you use the for/yield keywords as shown in these examples, you’re creating something known as a for expression.

How a “for expression” works

Here's a statement of how the yield keyword works in for loops, from the book, Programming in Scala:

Scala zip and zipWithIndex examples (with Stream)

I’ve known about using Scala’s zipWithIndex method for quite some time. I used it in for loops to replace counters, and it works like this:

scala> List("a", "b", "c").zipWithIndex
res0: List[(String, Int)] = List((a,0), (b,1), (c,2))

I learned about using zip with Stream last night while reading Joshua Suereth’s book, Scala In Depth. It works like this:

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

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, although I do show the output of all the examples. This is mostly just a collection of examples that can be used as a Scala String reference page or cheat sheet.

How to use multiple Futures in a Scala for-comprehension

If you want to create multiple Scala Futures and merge their results together to get a result in a for comprehension, the correct approach is to (a) first create the futures, (b) merge their results in a for comprehension, then (c) extract the result using onComplete or a similar technique.

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

The fastest way for OOP developers to learn FP

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 The Fast Way. 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.

A book to learn Functional Programming fast! (#1 new Java/OOP release)

I’m glad to report that my new Scala/FP book — Learn Functional Programming The Fast Way! — is a #1 New Release in the Java Computer Programming category on Amazon.

The book is written for Java, Kotlin, and other object-oriented programming (OOP) developers who want to learn functional programming quickly, so it’s cool to see it achieve this ranking in Amazon’s Java programming category.

2024 Update: The PDF version of the book is now free!

Functional programming books, comparison

Since I’ve written two functional programming (FP) books, I thought it might help to provide a comparison of them.

The short story is that both FP books have “limited technical jargon,” and as shown, The Little FP Book essentially has one purpose, which is to help Java/Kotlin/OOP developers learn functional programming as fast as possible, using a technique that I “discovered” over the last few years.

Conversely, The Big FP Book covers many topics in great detail.

If you’re interested in more details, here are links to the two books:

Learn Scala 3 The Fast Way (book)

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 money and currency: The BigDecimal class and libraries

(Note: I don't have any immediate solutions in this article; it's more of a discussion of where I'm at today when looking at handling money/currency in Scala.)

As a quick note, I've started to look at handling money/currency in Scala, and I'm also starting to explore a couple of money/currency libraries.

A Scala shell script example (and discussion)

Scala shell script FAQ: How do I create a Unix/Linux shell script to run a small Scala script?

If you want to run a Scala script as a Unix or Linux shell script -- such as hello.sh -- write your script like this:

Scala 3 dates: How to parse strings into dates (LocalDate, DateTimeFormatter)

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.12, How to Parse Scala Strings Into Dates.

Problem

While using Scala (Scala 2 or 3), you need to parse a Scala String into one of the date/time types introduced in Java 8, and still used in Java 11, 14, 17, etc.

Scala Solution

If your String is already in the expected format, pass it to the parse method of the desired class. If the String is not in the expected (default) format, create a formatter to define the format you want to accept. The following examples demonstrate the expected formats, and other solutions.

How to iterate over Scala lists with foreach and for

Scala list/sequence FAQ: How do I iterate over a Scala List (or more generally, a Scala sequence) using the foreach method or for loop?

There are a number of ways to iterate over a Scala List using the foreach method — which is available to Scala sequences like List, Array, ArrayBuffer, Vector, Seq, etc. — and the for comprehension, and I'll show several of these solutions here.

How to add elements to a List in Scala (List, ListBuffer)

Scala List FAQ: How do I add elements to a Scala List?

Solution

"How do I add elements to a Scala List” is actually a bit of a trick question, because you can't add elements to a Scala List; it's an immutable data structure. If you’ve ever used the Java String type, it’s just like that, you can’t mutate its elements.

That being said, in the following sections I’ll show what you can do.

Prepending elements to Scala Lists

The most common way to “add” elements to a Scala List is to create a new List from an existing List by prepending elements to the existing list. We do this all the time in functional programming in Scala, and the general approach looks like this in the Scala REPL: