Posts in the “scala” category

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:

val file = new File("/Users/al")
val files = file.listFiles()
val dirs = files.filter(_.isDirectory)

As noted in the comment, this code only lists the directories under the given directory; it does not recurse into those directories to find more subdirectories. Also as noted, use isFile to check to see if the “files” are files and isDirectory to see if the files are really directories.

How to write a Scala shell script that reads input from STDIN

As a quick note, if you need an example of how to write a Scala shell script that reads from STDIN (standard input) and writes to STDOUT (standard output), this code shows a solution:

#!/bin/sh
exec scala -savecompiled "$0" "$@"
!#

import scala.io.StdIn

var line = ""
while ({line = StdIn.readLine(); line != null}) {
    println(line)
}

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

(Advanced) Scala with Cats

If you’re interested in understanding the Cats library, I’m a big fan of the book, Scala with Cats (formerly known as Advanced Scala with Cats). Noel Welsh and Dave Gurnell have a simple writing style, with good examples. Being an older person, I only wish a print version was available.

A Scala factorial recursion example

I'm actually on vacation this week, but last night I showed a friend how to write software in Scala. He's familiar with recursion, so we jumped right into a simple factorial recursion example:

object FactorialRecursion {

    def main(args: Array[String]) {
        println(factorial(5))
    }

    def factorial(n: Int): Int = {
      if (n == 0) 
          return 1
      else
          return n * factorial(n-1)
    }
}

Another way to write that method is this:

Getting the union, intersection, and difference of Scala sets

A cool thing about Scala sets -- and some other Scala collections -- is that you can easily determine the union, intersection, and difference between two sets. The following examples demonstrate how the methods work. First, we create two sets that have a slight overlap:

scala> val low = 1 to 5 toSet
low: scala.collection.immutable.Set[Int] = Set(5, 1, 2, 3, 4)

scala> val medium = (3 to 7).toSet
medium: scala.collection.immutable.Set[Int] = Set(5, 6, 7, 3, 4)

Now we exercise the methods. First, the union:

First proof copy of Hello, Scala

The bad news is that I’ll be in the hospital most of next week after surgery to remove part of my digestive system, but the good news is that I just received the first proof of Hello, Scala, and I should have time to review it.

JavaFX: How to add a ContextMenu to a TableView

As a note to self, I used code like this in a Scala + JavaFX application to add a ContextMenu to a TableView:

defaddContextMenuToTableView(
    tableView: TableView[Note],
    tableViewContextMenu: ContextMenu,
    addNoteMenuItem: MenuItem,
    deleteNoteMenuItem: MenuItem,
) = {
    tableViewContextMenu.getItems().add(addNoteMenuItem)
    tableViewContextMenu.getItems().add(deleteNoteMenuItem)
    tableView.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler[MouseEvent]() {
        override def handle(mouseEvent: MouseEvent): Unit = {
            if (mouseEvent.getButton == MouseButton.SECONDARY) {
                tableViewContextMenu.show(tableView, mouseEvent.getScreenX, mouseEvent.getScreenY)
            }
        }
    })
}

There wasn’t anything special about the buttons, they were regular JavaFX Buttons:

val addNoteButton = new Button("add note")
val deleteNoteButton = new Button("delete note")

I did this in my Notes application, but later decided not to use a popup menu for this purpose.