scala

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

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.

How to extract a substring near the Nth occurrence of a string or character in a string

A Scala substring example: I ran into a situation today where I wanted to get a string after the Nth occurrence of another string, in this case after the 6th occurrence of a “:” character. There are probably many ways to determine the Nth occurrence of a string in another string, but as a quick example, this is what I did.

First, I started with this string:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

ScalikeJdbc: An example of SQL SELECT query to create a Scala List

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:

This website is a little one-man operation. If you found this information helpful, I’d appreciate it if you would share it.

From Drupal 6 to the Play Framework

As I wrote last week, I got tired of dealing with Drupal 6 (D6) security update issues — especially since D6 is no longer officially supported and the last unofficial D6 security update made my websites unusable — so I wrote a Play Framework (Scala) application to display my D6 database tables data.

It’s still a work in progress, but as you can see from this page on my One Man’s Alaska website, it’s coming along. As far as visitors of the website are concerned, mostly only thing the website needs is some CSS styling and maybe a search field. (I could also add support for comments and a contact page, but my D6 websites are old, and I don’t need/want those things. I probably also won’t put any effort into supporting 10-20 custom “category” URIs I used back in the day.)

As for the specific page I linked to on the One Man’s Alaska website, that’s a favorite memory of getting ready to winterize the car in October, 2010, when I lived in the Wasilla/Palmer area.