Short source code examples

As a note to self, this JavaScript code gets multiple, random, unique elements from a JavaScript array and displays three of those elements in the second div:

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.

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.

As a brief note today, if you want to know if your Drupal 8 web pages are being cached, take a look at the headers that are returned by your Drupal 8 URLs. Here’s an example using the curl command:

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:

If you happen to need Scala method to perform an MD5 hash on a string, here you go:

If you ever need to include multiple Play Framework 2.6 validators for a template form field, the uri field below shows the syntax that worked for me:

If you want to see an example of a Play Framework 2.6 data entry form that that sets help text (tips or tooltips) on text input fields (Play inputText fields), here’s an example of the required syntax:

As a brief note to self before I delete this code, this is how you create a drop-down list in Play Framework 2.6:

@* adding 'size to helper.select creates a select/option area (single or multi-select) *@
@helper.select(
    form("category"),
    categories,
    'id -> "category",
    '_help -> "Select one, any one"
)

In that code, categories is passed into the Play template like this:

Just a quick note today that if you split a CSV string in Scala, you should also (immediately) do a trim on each resulting element in the array. The Scala REPL shows why this is necessary:

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:

Play Framework FAQ: How do I set the Play Framework 2.6 port in production mode?

You set the port that your Play Framework application listens on by passing the http.port parameter to the “run” script for your application:

-Dhttp.port=5150

Here’s an example of a command I use to execute the run script for a Play Framework application named myapp:

If you haven’t been through it yet, this what the process of creating a new project with the Play Framework version 2.6 looks like:

I don’t remember exactly why I wrote this Scala shell script, but if I remember right I was having a problem getting sed to work properly, so I wrote this little script to insert an Amazon Kindle “break” tag before each <h1> tag in an HTML file:

I don’t know if there’s a better way to do this, but I can confirm that this code works as a way to handle/capture keystroke combinations in JavaFX without having to use a menu/menubar:

This code for creating a JavaFX menubar and handling a keystroke/key-combination didn’t work as desired, but I think it’s on the right track:

Here’s a little example of how to use a break in a Scala for loop:

Here’s a little Scala method I wrote to replace some “bad” characters that won’t print properly on my Radio Pi display:

def replaceBadCharacters(s: String): String = {
    s.replaceAll("“", "\"")
     .replaceAll("”", "\"")
     .replaceAll("‘", "\"")
     .replaceAll("’", "\"")
}

There are other ways to solve this problem, but I threw this together as a quick patch until I can figure out why the Phosphor screen saver on the Raspberry Pi won’t print those characters that I’m replacing.

If you ever need to convert HTML to plain text using Scala or Java, I hope these Jsoup examples are helpful:

If you ever need an example of a Unix/Linux shell script where you need to determine whether today is a weekend day, I can confirm that this code works: