scala

Tutorials about the Scala programming language.

Scala ArrayBuffer class: methods, syntax, and examples alvin August 16, 2018 - 12:33pm

This page contains a large collection of examples of how to use the Scala ArrayBuffer class.

Use the ArrayBuffer class when you want to use a general-purpose, mutable indexed sequence in your Scala applications:

Scala: How to list files and directories under a directory alvin August 15, 2018 - 10:50am

When using Scala, if you ever need to list the subdirectories in a directory, or the files under a directory, I hope this example is helpful:

import java.io.File

object FileTests extends App {

    // list only the folders directly under this directory (does not recurse)
    val folders: Array[File] = (new File("/Users/al"))
        .listFiles
        .filter(_.isDirectory)  //isFile to find files
    folders.foreach(println)

}

If it helps to see it, a longer version of that solution looks like this:

Scala immutable Map class: methods, examples, and syntax alvin August 9, 2018 - 4:38pm

This page contains a large collection of examples of how to use the Scala Map class, including most of the methods that are available on a Vector. (Currently well over 100 examples.)

I’m surprised when functional programmers say bad things about Scala alvin August 6, 2018 - 12:51pm

I’m surprised when many functional programmers feel the need to say something bad about Scala. As a community, that makes them seem like a bunch of people who aren’t very nice. There are things I don’t like about Haskell, F#, Lisp, Scala, Kotlin, Go, Perl, PHP, Python, C, C++, etc., but I don’t feel the need to take pot shots at any languages or individuals.

Trying to make Functional Programming, Simplified smaller alvin August 5, 2018 - 5:20pm

I’ve been looking for a way to make Functional Programming, Simplified smaller, but haven’t yet found a way to do that while keeping all the essential information in it. But IMHO, it’s still a heck of a lot easier than reading all of these books on the right:

Mutable state is fine but ... alvin August 4, 2018 - 10:50am

“Mutable state is fine but needs to be contained and non-observable.” A quote from Jonas Bonér, which I saw in this tweet.

Scala is very consistent alvin August 2, 2018 - 10:32am

One thing I was reminded of recently is how consistent the Scala language is. Unlike other languages that have special conditions and special operators for those special conditions — leading to a big vocabulary for those languages — Scala is ... well, it’s just very consistent, and that’s a great thing.

(As a bit of background, I used to be annoyed that Scala didn’t have ++ and -- operators for integers, but after working with other languages, I now understand what Martin Odersky & Co. were trying to avoid.)

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 FPers use the words effects and effectful, but rarely do you get a definition of what they mean.

Effects are related to monads

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

How to use higher-order functions with Option (instead of match expressions) alvin July 29, 2018 - 1:59pm

I originally wrote a long introduction to this article, but I decided to keep that introduction for the second article in this series. For this article I’ll just say:

  • idiomatic Scala code involves never using null values
  • because you never use nulls, it’s important 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 with Option values rather than match expressions.

Functional programming: Math functions, not programming functions alvin July 28, 2018 - 7:53pm

“There’s only ONE rule, but it’s an important one: all of your values must be functions. Not programming functions, but math functions.”

I think I read that quote in an earlier version of this article. The quote is about functional programming, and it influenced something I wrote in my book, Functional Programming, Simplified: Functional programmers think about themselves as being mathematicians, and think of their code as being a combination of algebraic equations, where each function is a pure function that you can think of in mathematical terms.