scala

Tutorials about the Scala programming language.

How to use Lift-JSON to parse JSON array data alvin January 22, 2018 - 6:52pm

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:

What Digital (eBook) and Print sales look like for a computer programming book alvin January 22, 2018 - 10:27am

If you ever wondered what Digital and Print book sales look like for a technical book (a computer programming book, in this case), here you go. This is a slightly cleaned up chart that O’Reilly provides to me for sales of the Scala Cookbook over time, showing eBook sales vs the printed book sales.

A Scala ‘foldLeft’ function written using recursion alvin January 20, 2018 - 4:25pm

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

Fun with Scala functions (andThen and compose)

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

A little Scala “date” utilities class alvin January 20, 2018 - 2:36pm

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

Bug reports for Functional Programming, Simplified alvin January 19, 2018 - 11:15am

If you own a copy of Functional Programming, Simplified, and would like to report any bugs, I started this Github repo so you can do that. Just report your bug under the Issues tab.

Scaladoc syntax examples (Scaladoc tags, wiki markup) alvin January 18, 2018 - 8:08pm
Table of Contents1 - An example of Scaladoc tags and wiki formatting2 - Sample Scaladoc output3 - Common tags4 - Scaladoc “wiki” character formatting tags5 - Scaladoc “wiki” paragraph formatting tags6 - Generating Scaladoc documentation with SBT7 - See Also8 - The Scala Cookbook

In this article I share some examples of Scala’s Scaladoc syntax, including common Scaladoc tags, and the wiki-style of markup that Scaladoc supports.

 

(this space left open to make room for the table of contents over there -->)

 

Back to top

An example of Scaladoc tags and wiki formatting

You can mark up your source code using Scaladoc tags as well as a wiki-like syntax. The following code shows many of the Scaladoc tags and a few of the wiki-style markup tags:

Scala “string to date” and “date to string” methods alvin January 17, 2018 - 5:38pm

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: