Posts in the “scala” category

Scala: How to use Play Framework “Twirl templates” in a standalone application (tips, examples)

Last week I finished writing some Scala code to convert this website from using Drupal 8 to using static web pages. Technically what happens is that I use Drupal at a different location, and then my Scala code reads the Drupal database and uses to the Twirl template system — that comes with the Play Framework — to convert that data into the static HTML pages you see here.

Along the way I learned a lot about the 2020 version of Twirl templates, so I thought I’d share some tips and examples about how to do things with Twirl. If you ever want to use Twirl in standalone mode like I did, or inside the Play Framework, I hope this is helpful.

Scala: How to use higher-order functions (HOFs) with Option (instead of match expressions)

I originally wrote a long introduction to this article about how to work with the Scala Option, but I decided to keep that introduction for a future 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 for you 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

A Scala function to get the Unix epoch time for X days ago

As a brief note, here’s a Scala function to get the Unix epoch time for X days ago:

/**
 * Returns a 10-digit Long (like 1585275929) representing the date/time.
 * Use it to get the time for 1 day ago, 2 days ago, etc. `0` will give
 * you the current time.
 */
def unixEpochTimeForNumberOfDaysAgo(numDaysAgo: Int): Long = {
    import java.time._
    val numDaysAgoDateTime: LocalDateTime = LocalDateTime.now().minusDays(numDaysAgo)
    val zdt: ZonedDateTime = numDaysAgoDateTime.atZone(ZoneId.of("America/Denver"))
    val numDaysAgoDateTimeInMillis = zdt.toInstant.toEpochMilli
    val unixEpochTime = numDaysAgoDateTimeInMillis / 1000L
    unixEpochTime
}

As shown in the comments, if you give it a 0 it will return the current epoch time.

Of course you can make the code shorter and better; I just wanted to show the steps in the approach using the Time classes in Java 8.

Using Scala with Java Swing classes is pretty seamless

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:

import java.awt.BorderLayout
import java.awt.Dimension
import javax.swing.JFrame
import javax.swing.JScrollPane
import javax.swing.JTextArea

object SwingExample extends App {

    val textArea = new JTextArea
    textArea.setText("Hello, Swing world")
    val scrollPane = new JScrollPane(textArea)

    val frame = new JFrame("Hello, Swing")
    frame.getContentPane.add(scrollPane, BorderLayout.CENTER)
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
    frame.setSize(new Dimension(600, 400))
    frame.setLocationRelativeTo(null)
    frame.setVisible(true)

}

Technically you’ll want to display the JFrame the way I show in How to create, center, and display a Java JFrame, but this code gives you an idea of how the process works.

I’ve written much larger applications with Scala and Swing, but for today I just wanted to share some code to show that it’s a very straightforward process.

How to emit raw HTML with the Play Framework and Twirl template library

I’ll write more about this when I have some free time, but for the moment I’ll just say that if you ever need to emit raw HTML when using the Play Framework and Twirl template library, code like this will do the trick:

@play.twirl.api.HtmlFormat.raw(currentBookNode.bodyValue)

In this case the bodyValue field already contains HTML as a Scala String, and I don’t want Twirl to mess with that, so I use its HtmlFormat.raw method as shown.

One example of how to avoid using `var` fields in Scala

As one note and example of how to avoid using var fields in Scala, I initially wrote this code:

var numDaysToProcess = 0

if (args.length != 0) {
    numDaysToProcess = args(0).toInt
}

After I looked at that code for a moment I realized that I could write it without a var like this instead:

val numDaysToProcess =
    if (args.length == 0) {
        0
    } else {
        args(0).toInt
    }

That’s a simple example of one situation where you can easily avoid using a var field in Scala. I’ll share more examples as they come up.

How to read Scala command line arguments

Scala command line FAQ: How do I read command line arguments (args) in a Scala shell script?

If your Scala shell script is very short, and you're not using an object or class declaration -- i.e., you have no main method -- you can access the script's command line arguments through the default args array, which is made available to you by Scala.

For instance, you can create a one-line Scala script named hello.scala like this:

How to determine if a Scala String contains a regular expression pattern

[toc]

Scala String FAQ: How can you determine whether a String contains a regular expression pattern in Scala? (Or, “How can I find the first match (or all matches) of a regex in a String?”)

Solution: findFirstIn, findAllIn

Create a Regex object by invoking the .r method on a String, and then use that pattern with findFirstIn when you’re looking for one match, and findAllIn when looking for all matches.

The Play Framework template comments syntax

When you want to create Play Framework template comments, use the @* ... *@ syntax. Here's a one-line comment:

@* COMMENT *@

Here's a multiline comment:

@*
 * Four score
 * and seven year ago
 * our fathers ...
 *@

(You can format that however you want.)

You can use comments as the first line of Play Framework templates (before the function declaration):

How to use Lift-JSON to parse JSON array data

If you ever need to parse JSON stock data from alphavantage.co using Scala, here’s a test class I just wrote that uses Lift-JSON.

The JSON data format

First, here’s the JSON I get back from them:

{
    "Meta Data": {
        "1. Information": "Batch Stock Market Quotes",
        "2. Notes": "IEX Real-Time Price provided for free by IEX (https://iextrading.com/developer/).",
        "3. Time Zone": "US/Eastern"
    },
    "Stock Quotes": [
        {
            "1. symbol": "MSFT",
            "2. price": "91.6000",
            "3. volume": "23511825",
            "4. timestamp": "2018-01-22 16:00:00"
        },
        {
            "1. symbol": "FB",
            "2. price": "186.4000",
            "3. volume": "20946922",
            "4. timestamp": "2018-01-22 16:41:06"
        },
        {
            "1. symbol": "AAPL",
            "2. price": "176.8900",
            "3. volume": "27027474",
            "4. timestamp": "2018-01-22 16:00:00"
        }
    ]
}

I get that array back when I access a URL like this:

  • https://www.alphavantage.co/query?function=BATCH_STOCK_QUOTES&symbols=MSFT,FB,AAPL&apikey=demo

Parsing the JSON

Next, here’s how I parse that JSON using Scala and Lift-JSON:

import net.liftweb.json.DefaultFormats
import net.liftweb.json._
import scala.collection.mutable.ArrayBuffer

object Test2 extends App {

    val json = parse(SampleData.data)
    implicit val formats = DefaultFormats

    val elements = (json \\ "Stock Quotes").children

    val stocks = ArrayBuffer[Stock]()
    // keys are: "1. symbol", "2. price", "3. volume", "4. timestamp"
    // i expect 3 stocks, so ...
    for (i <- 0 until 3) {
        val e = elements(0)(i)
        // Map(1. symbol -> MSFT, 2. price -> 91.6000, 3. volume -> 23511825, 4. timestamp -> 2018-01-22...)
        val values = e.values.asInstanceOf[Map[String,String]]   //coercion
//        for ((k,v) <- values) printf("key: %s, value: %s\n", k, v)
//        println("")
        stocks += Stock(values("1. symbol"), values("2. price"))
    }

    stocks.foreach(println)

    case class Stock(symbol: String, price: String)


}

The output

The output of that code looks like this:

Stock(MSFT,91.6000)
Stock(FB,186.4000)
Stock(AAPL,176.8900)

Discussion

The source code is ugly I write now because I just got it working, but I thought I’d share it here in case you need to parse some JSON data that’s in an array format like this. I’m more used to working with JSON that represents objects — see my other Scala/JSON examples — but this data is more of an “array of arrays” format, so this approach is different than my previous examples.

My build.sbt file

As a final note, my build.sbt file looks like this:

name := "StockQuotes2018"

version := "1.0"

scalaVersion := "2.12.4"

libraryDependencies ++= Seq(
    "net.liftweb" %% "lift-json" % "3.1.1"
)

scalacOptions += "-deprecation"

Play Framework: Anorm SQL query syntax and examples

[toc]

In general the online Play Framework documentation is excellent, but one area where I needed more help was in seeing more examples of the Anorm syntax. To that end, here are some Anorm query examples, taken from a Play Framework application I worked on recently. But first, a little background about Anorm.

Is Scala DICEE?

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: