Posts in the “scala” category

How to replace regular expression patterns in strings in Scala

Scala String FAQ: How do I replace a regular expression (regex) pattern in a String in Scala?

Solution

Because a String is immutable, you can’t perform find-and-replace operations directly on it, but you can create a new String that contains the replaced contents. There are several ways to do this.

You can call replaceAll on a String, remembering to assign the result to a new variable:

A Scala function to get the first paragraph from a string

As a brief note today, here’s the source code for a little Scala function that gets the first paragraph of text from a string. Paragraphs are assumed to be separated by \n\n characters. Also, if that character sequence isn’t found, the input text is assumed to be one paragraph. Here’s the source code:

Scala Book: Free PDF, Mobi, and ePub versions

Update, May 3, 2020: I added the single-page HTML version of the book to the Downloads section below.

Back in the summer of 2019, a book publisher approached me and asked if they could buy the rights to an “introduction to Scala” book I had independently published and titled, “Hello, Scala.” The book is about 225 pages long and provides a brief-but-thorough introduction to the Scala language.

I came extremely close to signing with that publisher, but as fate would have it, right at that time I saw some people on contributors.scala-lang.org say something to the effect, “If only we had an online book ...”

A new Scala Book cover

I spent a little time working on the Scala Book recently, and came up with this new cover design. I’d like it be a little lighter, but at the moment I don’t like it when it’s white on the bottom with black text.

The “Hello, Scala” paperback is just $10

November 29, 2019: A few days ago I made the PDF version of “Hello, Scala” free, and today I made the paperback version of “Hello, Scala” available again, and reduced it’s price from $20 to just $10. Click the image below to buy the book on Amazon.

As I’ve noted before, the contents of this book are being updated and improved, and in the future it will be available as Scala Book. The HTML version of those contents are currently available on the docs.scala-lang.org site.

How to turn off (disable) Akka logging

Using Akka logging is a great thing, until you need to turn it off. In short, to disable Akka logging, you need to create a file named application.conf in your SBT src/main/resources folder, and set the loglevel to “OFF” in that file, like this:

#
# see http://doc.akka.io/docs/akka/snapshot/general/configuration.html
#
akka {

    # options: OFF, ERROR, WARNING, INFO, DEBUG
    loglevel = "OFF"

}

I show the other log levels in the comments in that file, but setting it to OFF disables Akka logging.

When I refer to Akka logging, I’m referring to logging when you have an Akka actor that extends ActorLogging.

Simple Scala Akka Actor examples (Hello, world examples)

Scala Actors FAQ: Can you share a Scala Akka Actors example/tutorial?

Sure. Most of the Scala Akka Actor tutorials I see jump right into the deep end, throwing you into some tough concepts right away. Personally I'm more into the "crawl before you walk approach," and to that end, here are some simple Akka Actor examples, of the "Hello, world" variety.

A simple Akka "Hello, world" example

My first Akka Actor example is about as simple as I can make it:

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.

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 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"