scala

Scala: How to list files and directories under a directory

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:

I’m surprised when functional programmers say bad things about Scala

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.

Scala is very consistent

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 is to say that effects are 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)

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.

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:

Is Scala DICEE? alvin July 25, 2018 - 12:07pm

If you’ve never heard of the term DICEE, it was coined by Guy Kawasaki. Mr. Kawasaki was a developer evangelist for the original Macintosh team in the 1980s, and used the term in at least one subsequent book to refer to great products.

“DICEE” is an acronym that stands for Deep, Indulgent, Complete, Elegant, and Emotive: