Scala, Java, Unix, MacOS tutorials (page 132)

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.

I’m not sure why, but on April 3, 2018, the people behind the Mollom anti-spam module for Drupal basically went out of business. This meant that I either had to disable comments on this site (which I did for a while), or look at other anti-spam modules, which I did over the weekend.

A bad part of two other popular Drupal anti-spam modules (Honeypot and reCAPTCHA) is that if you use them, the result is that they disable caching on your website. (In the case of Honeypot it only disables caching if you use its time-related feature.)

The image here shows a performance effect of using these modules. I turned on these modules at ~17:00 hours today (which is really 1pm, my time), and there was an immediate spike in CPU use. I assume that web pages are also being served slower, and both of these performance side effects are not good for the website business.

There’s one other Drupal anti-spam module I can try that doesn’t disable web page caching, so I’ll take a look at it soon. If that doesn’t work well I’ll either have to turn off the comments again, or write my own module that doesn’t affect caching.

Drupal 8 - enabling anti-spam modules is bad for performance

“Do I look like a guy who needs hookers?” is going right between “We choose to go to the moon...” and “Mr. Gorbachev, tear down this wall” in my list of great presidential quotes.

If you’re interested in learning about “pay for written content” models, Medium has an article titled, The Medium Model
How we are building a system for high-quality publishing at scale
.

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.

This past week I started working with the Play Framework (version 2.6), and this is a quick look at how to implement user authentication in a Play application. Specifically this blog post focuses on how to create a custom action so you can secure your Play controllers methods, where you’ll implement those methods using this new, custom action.

One summer morning back when I lived in Talkeetna, Alaska, I had to leave my little cabin at 5am to be at the Toyota dealer in Anchorage by 7am so they could try to figure out why my RAV4 kept filling up with water every time it rained. Since it was summer, the sunlight at 5am was pretty much the same as it was at noon, which was always cool.

About twenty seconds after driving down the main road out of town — actually there’s only one road in and out of town, so it’s hardly worth referring to it as the “main road” — I came across a woman in a white dress walking on the lane divider stripes in the middle of the road, in the same direction I was driving.

I was reminded of that morning when I heard the song Chevy Van by Sammy Johns just now.

Mark Zuckerberg is now living out every young person’s worth nightmare: Trying to explain how tech stuff works to the nation’s elderly.

~ someone on twitter yesterday

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:

//see https://www.playframework.com/documentation/2.6.x/ScalaCustomValidations
//see https://www.playframework.com/documentation/2.6.x/ScalaForms
val form: Form[BlogPost] = Form (
    mapping(
        "title" -> nonEmptyText,
        "blogContent" -> nonEmptyText,
        "tags" -> nonEmptyText,
        "description" -> nonEmptyText(maxLength = 160),
        "uri" -> nonEmptyText
                     .verifying("error: must begin with a /", u => beginsWithSlash(u))
                     .verifying("error: invalid characters in uri", u => charsAreAllowedInUri(u))
    )(BlogPost.apply)(BlogPost.unapply)
)

As shown, just call the verifying method as many times as needed on the form field you want to validate with your custom functions.

The two custom validation methods I use in that example take a String and return a Boolean, as shown here:

// verify that a string begins with a slash
private def beginsWithSlash(s: String): Boolean = s.startsWith("/")

// make sure the characters in the string are legal in a uri
private def charsAreAllowedInUri(s: String): Boolean = {
    val regex = "[0-9a-zA-Z-_]*"
    s.matches(regex)
}

In summary, if you ever need to include multiple custom Play Framework validation functions for a form field, I hope this example is helpful.

“You are neither right nor wrong because the crowd disagrees with you. You are right because the data and reasoning are right.”

~ Benjamin Graham

“Deliver to the world what you would buy if you were on the other end.”

~ Charlie Munger

If you’d like an example of how to create a data entry form using the Play Framework v2.6, here’s a complete example.

“Warren Buffett has become one hell of a lot better investor since the day I met him, and so have I. If we had been frozen at any given stage with the knowledge we had, the record would have been much worse than it is today. So the game is to keep learning, and I don’t think people are going to keep learning who don’t like the learning process. You need to like the learning process.”

This is another quote by Charlie Munger. It reminds me both of working on good programming teams, but also of learning from people that I haven’t enjoyed working with, but learned things from.

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:

val s = """a:2:{s:3:"alt";s:35:"The Fairview Inn, Talkeetna, Alaska";s:5:"title";s:0:"";}"""

Within that string I wanted to extract the “The Fairview Inn, Talkeetna, Alaska” string. The way I approached this problem was to split the input string on the “:” character, giving me this:

scala> s.split(":")
res0: Array[String] = Array(a, 2, {s, 3, "alt";s, 35, "The Fairview Inn, Talkeetna, Alaska";s, 5, "title";s, 0, "";})

Then I saw that the substring I wanted was in the seventh position of the resulting array:

scala> s.split(":")(6)
res1: String = "The Fairview Inn, Talkeetna, Alaska";s

There are some extra junk characters on the end of that string, so I used dropRight:

scala> s.split(":")(6).dropRight(3)
res2: String = "The Fairview Inn, Talkeetna, Alaska

Then I noticed there was a double-quote at the beginning of the string, so I used drop on it:

scala> s.split(":")(6).drop(1).dropRight(3)
res3: String = The Fairview Inn, Talkeetna, Alaska

Of course there are other ways to solve this problem, but if you want to get a substring somewhere around the Nth occurrence of a string or character in another string, I hope this example is helpful.

“Terribly smart people make totally bonkers mistakes.”

~ from the book, Charlie Munger, The Complete Investor

“In engineering, people have a big margin of safety. But in the financial (investing) world, people don’t give a damn about safety.”

~ from the book, Charlie Munger, The Complete Investor

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.