Short source code examples

If you want to see an example of a Play Framework 2.6 data entry form that that sets help text (tips or tooltips) on text input fields (Play inputText fields), here’s an example of the required syntax:

As a brief note to self before I delete this code, this is how you create a drop-down list in Play Framework 2.6:

@* adding 'size to helper.select creates a select/option area (single or multi-select) *@
@helper.select(
    form("category"),
    categories,
    'id -> "category",
    '_help -> "Select one, any one"
)

In that code, categories is passed into the Play template like this:

Just a quick note today that if you split a CSV string in Scala, you should also (immediately) do a trim on each resulting element in the array. The Scala REPL shows why this is necessary:

As a brief note today, this Scala example code shows how I make a SQL query using ScalikeJdbc to return results as a List, in this case making a SQL SELECT query to create a Scala list of PhotoNode instances:

Play Framework FAQ: How do I set the Play Framework 2.6 port in production mode?

You set the port that your Play Framework application listens on by passing the http.port parameter to the “run” script for your application:

-Dhttp.port=5150

Here’s an example of a command I use to execute the run script for a Play Framework application named myapp:

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: