Tutorials about the Scala programming language.

A database query for implementing paging in a Play Framework application alvin April 25, 2018 - 1:28pm

As a quick note to self, one way to implement paging in a Play Framework application is to use the limit and offset arguments in a MySQL query. This way you can show X number of objects on one web page, then give the user Next and Previous links so they can scroll backwards and forwards through the dataset.

To implement paging with a MySQL query, use the limit and offset query parameters, as shown in this example:

Examples of Play Framework Twirl template functions alvin April 25, 2018 - 11:56am

If you ever need to create a Play Framework Twirl template function, here’s an example of how to create and use one. First, create the Twirl function like this:

@fullUrl(uri: String) = @{

That function creates a complete URL from the URI it’s given.

Later in your template you can call the Twirl template function like this:

Scala/Java: How to write a pattern that matches a minimum to maximum number of specified characters alvin April 24, 2018 - 8:12pm

If you’re using Java or Scala and need to write a pattern that matches a range of characters, where those characters occur between a minimum and maximum number of times in the pattern, the following example shows a solution I’m currently using.

The idea is that the pattern "[a-zA-Z0-9]{1,4}" means, “Match a string that has only the characters a-z, A-Z, and 0-9, where those characters occur a minimum of one time and a maximum of four times.” The following tests in the Scala REPL shows how this works:

A note on how to change Play Framework logging levels alvin April 24, 2018 - 5:01pm

As a brief note to self, when you need to change Play Framework logging, such as to change it to more of a debug mode, edit the logback.xml file and add/change entries like these:

A Play Framework login/authentication example project alvin April 24, 2018 - 2:44pm

Without an example it can be hard to understand how user authentication works in a Play Framework application, so I just created a project on Github named PlayFrameworkLoginAuthenticationExample that demonstrates one way to implement login authentication in a Play Framework 2.6 application.

Dotty will become Scala 3.0 alvin April 20, 2018 - 1:06pm

As shown in the image, the project known as Dotty will be Scala 3.0.

Play Framework: Anorm SQL query syntax and examples alvin April 18, 2018 - 2:35pm
Table of Contents1 - Anorm2 - More background ...3 - A note about my coding style4 - SQL SELECT queries5 - A brief intermission6 - SQL INSERT with primary key (auto increment)7 - SQL INSERT with no primary key8 - SQL UPDATE9 - SQL DELETE10 - Complete example methods11 - A complete class12 - More information

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.

How to get multiple, unique, random elements from a list of elements alvin April 17, 2018 - 8:04pm

One thing I never thought about before is that if you need to get multiple, unique, random elements from a list of elements, one solution to the problem is to shuffle the list and then take as many elements as you want/need. For instance, if you want three unique, random elements from a list of integers in Scala, you can do this:

scala> val list = List(1,2,3,4,5,1,2,3,4,5)
list: List[Int] = List(1, 2, 3, 4, 5, 1, 2, 3, 4, 5)

scala> val uniq = list.distinct
uniq: List[Int] = List(1, 2, 3, 4, 5)

scala> val shuffled = scala.util.Random.shuffle(uniq)
shuffled: List[Int] = List(1, 4, 5, 2, 3)

scala> val firstThree = shuffled.take(3)
firstThree: List[Int] = List(1, 4, 5)

As that solution shows, you start with a simple list; get the unique/distinct elements from the list; shuffle those elements to create a new list; then take the first three elements from the shuffled list. That’s probably not a great solution for huge lists, but for many simple lists it’s a way to get multiple random elements from the list.

In Scala, how to get the day of the year alvin April 15, 2018 - 2:44pm

Scala date FAQ: How do I determine the day of the year in Scala?

Solution: Use the Java Calendar class, as shown here:

scala> import java.util.Calendar
import java.util.Calendar

scala> Calendar.getInstance.get(Calendar.DAY_OF_YEAR)
res0: Int = 104

I’m writing this on April 14, 2018, which is the 104th day of the year.

Play Framework: An Anorm ‘singleOpt’ query example alvin April 13, 2018 - 6:35pm

It’s embarrassing how long it took to get this to work, but if you ever need to select a single row from a database using Play Framework (version 2.6), Anorm, and singleOpt, I hope this example is helpful, and will save you time and frustration: