Short source code examples

If you haven’t been through it yet, this what the process of creating a new project with the Play Framework version 2.6 looks like:

I don’t remember exactly why I wrote this Scala shell script, but if I remember right I was having a problem getting sed to work properly, so I wrote this little script to insert an Amazon Kindle “break” tag before each <h1> tag in an HTML file:

I don’t know if there’s a better way to do this, but I can confirm that this code works as a way to handle/capture keystroke combinations in JavaFX without having to use a menu/menubar:

This code for creating a JavaFX menubar and handling a keystroke/key-combination didn’t work as desired, but I think it’s on the right track:

Here’s a little example of how to use a break in a Scala for loop:

Here’s a little Scala method I wrote to replace some “bad” characters that won’t print properly on my Radio Pi display:

def replaceBadCharacters(s: String): String = {
    s.replaceAll("“", "\"")
     .replaceAll("”", "\"")
     .replaceAll("‘", "\"")
     .replaceAll("’", "\"")
}

There are other ways to solve this problem, but I threw this together as a quick patch until I can figure out why the Phosphor screen saver on the Raspberry Pi won’t print those characters that I’m replacing.

If you ever need to convert HTML to plain text using Scala or Java, I hope these Jsoup examples are helpful:

If you ever need an example of a Unix/Linux shell script where you need to determine whether today is a weekend day, I can confirm that this code works:

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

Nothing major here today, but here’s some source code to start 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.