Short source code examples

As a short note, here’s some Scala source code that shows how to write a foldLeft function using recursion:

After yesterday’s Scala nested Option + flatMap/for example, here’s another example of plowing through nested Options with flatMap. First, start with some nested options:

val o1 = Option(1)
val oo1 = Option(o1)
val ooo1 = Option(oo1)

Here are those same three lines, with the data type for each instance shown in the comments:

Here’s a little fun with Scala functions, including the use of andThen and compose:

scala> val add1 = (i: Int) => i + 1
add1: Int => Int = <function1>

scala> val double = (i: Int) => i * 2
double: Int => Int = <function1>

scala> val addThenDouble = add1 andThen double
addThenDouble: Int => Int = <function1>

scala> addThenDouble(1)
res0: Int = 4

scala> val doubleThenAdd = add1 compose double
doubleThenAdd: Int => Int = <function1>

scala> doubleThenAdd(1)
res1: Int = 3

(Inspired by the book, Functional and Reactive Domain Modeling, and my own book, Learning Functional Programming in Scala.)

Here’s some source code for a little Scala “date utilities” class:

Here are a couple of “string to date” and “date to string” methods. They’re written in Scala, but are easily converted to Java. They only use the Java Date and SimpleDateFormat classes:

If you ever need to convert a Java Date to a Long, just call the getTime method on the Date instance. This Scala example shows the process:

As a quick note to self, this is what I had to do to get a “Delete” menu item working on a JavaFX TableView row:

As a note to self, I just used this code to delete rows from a JavaFX TableView in a Scala application:

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

As a quick note to self, this is how you add an ActionEvent EventHandler to a MenuItem in JavaFX, when writing JavaFX code in Scala:

As a quick note today, if you’re working on a Scala project and get a compiler error message like this:

As a brief note to self, I just created a “spacer” in JavaFX using a Region, as shown in this code:

// a spacer to push the visible elements up a little
val spacer = new Region
spacer.setPrefHeight(40)
VBox.setVgrow(spacer, Priority.ALWAYS)

I originally created the spacer as a Separator, which is the wrong thing (it’s more like an HTML <hr> tag). A JavaFX Region just gives you a blank space, which you can control as needed.

If you ever need to create a Dialog in LibGDX, I can confirm that this example code works:

I just started working with LibGDX, so I don’t know if there’s a better way to create a LibGDX Scene2d ImageButton, but I can confirm that this approach works:

Texture hikeTexture = new Texture(Gdx.files.internal("hike_btn.jpg"));
Texture hikeTexturePressed = new Texture(Gdx.files.internal("hike_btn_pressed.jpg"));
hikeButton = new ImageButton(
    new TextureRegionDrawable(new TextureRegion(hikeTexture)),
    new TextureRegionDrawable(new TextureRegion(hikeTexturePressed))
);
hikeButton.setPosition(60, 300);  //hikeButton is an ImageButton
stage.addActor(hikeButton);

I currently use this code in the show() method of a class that implements Screen, and it works as desired.

If you ever wanted to use Scala with Java Swing classes (like JFrame, JTextArea, JScrollPane, etc.), the process is pretty seamless. Here’s an example of a simple Scala/Swing application where I show a text area in a JFrame:

As a quick note, here’s a little Java graphics utilities class I started putting together today. Mostly I’m just concerned with monitor/display sizes at the moment, especially when a computer system has multiple displays.

In production code I recommend that you use a good “Files” library like Apache Commons IO, but if you want to create your own Scala FileUtils class, here’s some source code that can help you get started.

First, here’s some code for the FileUtils class (an object, technically):

Here’s some source code for a Scala method that reads a text file that may have comments into a List[String]:

Here’s a quick Scala example that shows how to convert multiple spaces in a string to a single space:

I’m putting this Scala shell script out here as a “source code snippet” so I can find it again if I need it. This file reads an input file that contains a series of HTML <h1> tags. I use this as part of a process of publishing an Amazon Kindle ebook from an HTML file, and in one of the steps of the creation process, I use this script to help create the Table of Contents (TOC) for the book.

Here’s the source code: