Posts in the “scala” category

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:

Two ZIO, Scala CLI, and Scala 3 examples

If you’d like to get started working with the Scala ZIO functional programming (FP) library, here are two little ZIO 101 and ZIO 102 “Hello, world” examples that you can run with Scala CLI and Scala 3.

This post is sponsored by my new book,
Learn Functional Programming Without Fear.

FP: ZIO + Scala CLI example: “Hello, world” 101

First, here’s a complete ZIO “Hello, world” example that shows everything you need to get started using ZIO with Scala CLI:

Creating a Thread (and Runnable) in Scala

I ran into a strange problem this weekend where I noticed a huge difference between the way a Scala Future and a Thread worked for the exact same code. I think I’m pretty aware of the obvious differences between futures and threads, so this really caught me by surprise. If/when I figure out why there was such a difference in behavior for the given problem I’ll post that information here.

A Scala Thread example

While that problem will haunt me for a while, what I really want to post here today is a simple Scala Thread example:

How to convert a Scala Array/List/Seq (sequence) to string with mkString

Scala collections FAQ: How can I convert a Scala array to a String? (Or, more, accurately, how do I convert any Scala sequence to a String.)

A simple way to convert a Scala array to a String is with the mkString method of the Array class. (Although I've written "array", the same technique also works with any Scala sequence, including Array, List, Seq, ArrayBuffer, Vector, and other sequence types.)

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:

Using the Scala Option, Some, and None idiom (instead of Java null)

A powerful Scala idiom is to use the Option class when returning a value from a function that can be null. Simply stated, instead of returning one object when a function succeeds and null when it fails, your function should instead return an instance of an Option, where the instance is either:

  1. In the success case, return an instance of the Scala Some class
  2. In the failure case, return an instance of the Scala None class

Because Some and None are both children of Option, your function signature just declares that you're returning an Option that contains some type (such as the Int type shown below). At the very least, this has the tremendous benefit of letting the user of your function know what’s going on.

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: