Alvin Alexander | Java, Scala, Unix, Perl, Mac OS X

Table of Contents1 - String equality2 - Multiline strings3 - String interpolation/substitution4 - Substrings5 - Treating a String as an Array[Char]6 - Using map and for7 - Regular expressions8 - Transforming arrays to a String9 - A lot of String method examples

This page contains a collection of over 100 Scala String examples, including strings functions, format specifiers, and more. I don’t provide too many details about how things work in these examples; this is mostly just a collection of examples that can be used as a reference page or cheat sheet. (I do show the output of most examples.)

First, here are some basic uses of the Scala String class to help get us warmed up:

I just started working with the Android Room database persistence library, and since you’re not supposed to run things like database queries on the main thread (the UI thread), I was looking at other ways to run them.

In general, you probably won’t want to run database queries using a Thread, but just to see how Room works, I wrote this Java Thread code, and confirmed that it works as expected:

I woke up on the left side of a king size bed this morning. It wasn’t my bed, but I was blanketed in a thick, soft comforter, which felt wonderful. I looked around briefly. Wherever I was, the room seemed very nice. It was light outside.

As my other senses came to me, I realized that what woke me up was a strange woman standing on the right side of the bed, yelling at me. She wanted to know where I got the warm Cinnabon that was on the nightstand next to me. More accurately, she rather forcefully wanted to know if someone brought that for me, and if so, who.

When I watched the movie Coffee Shop, I knew that Laura Vandervoort wasn’t singing the songs in the movie, and I assumed that the artist singing was Norah Jones, but it turns out that was wrong. The actual singer’s name is Mandi Mapes, and the two songs are called “Dance With Me” and “In Your Arms.” Videos for both of the Coffee Shop songs are shown below.

“Pure functional programming is programming with mathematical functions.”

~ Erik Meijer

On my list to listen to: A TED Radio Hour discussion, Why Does Time Exist?

“There could have been no two hearts so open, no tastes so similar, no feelings so in unison.”

I realy like this quote from baseball pitcher Jason Marquis, talking about Tony LaRussa, Manager of the St. Louis Cardinals:

“One thing Tony (La Russa) always preached over there was execution and minimizing mental mistakes. You don’t have to have the most talented team to do that, and it doesn’t take the most talented team to win.”

In baseball and in work I think this is true. It’s similar to this quote from Mike Ditka:

“Effort without talent is a depressing situation....but talent without effort is a tragedy.”

The book, Advanced Scala with Cats, has a nice little function you can use to run a block of code “slowly”:

def slowly[A](body: => A) = try body finally Thread.sleep(100)

I’d never seen a try/finally block written like that, so it was something new for the brain.

In the book they run a factorial method slowly, like this:

slowly(factorial(n - 1).map(_ * n))

FWIW, you can modify slowly to pass in the length of time to sleep, like this:

def slowly[A](body: => A, sleepTime: Long) = try body finally Thread.sleep(sleepTime)

As a quick note, I was just looking into the state of Scala “lint” tools, and found ScalaStyle, WartRemover, and Scapegoat.

This 2014 underscore.io post states, “Those interested in FP purity in a Scala world, you’ll want WartRemover.” (Of course that recommendation may have changed by now.) The current ScalaStyle website states, “Scalastyle is used as part of the grading framework for the course Functional Programming Principles in Scala by Martin Odersky on Coursera.”

While reading the excellent Scala/FP book, Advanced Scala with Cats, I was just reminded that Scala’s Either class was redesigned in Scala 2.12. Prior to 2.12, Either was not biased, and didn’t implement map and flatMap methods. As the image from the book shows, Either is redesigned in 2.12 to include those methods, so it can now be used in Scala for-expressions as shown.

(I write about biasing in my book, Learning Functional Programming in Scala.)

Scala 2.12: Either is biased, implements map and flatMap

Summary: This page is a printf formatting cheat sheet. I originally created this cheat sheet for my own purposes, and then thought I would share it here.

A cool thing about the printf formatting syntax is that the specifiers you can use are very similar, if not identical, between several different languages, including C, C++, Java, Perl, Ruby, and others, so your knowledge is reusable, which is a good thing.

I haven’t used Scala Native yet, but at v0.1 it looks very cool. The image is from scala-lang.org, you can read much more about it at scala-native.org, and follow the developers here on Twitter.

The Scala Native project

“Pleasure in the job puts perfection in the work.”

~ Aristotle

Table of Contents1 - Running Ekko2 - Running Ekko commands3 - Listing commands Ekko understands4 - Written with Akka (and Scala)5 - Next steps6 - Add actors with reflection? (plugins)7 - The source code8 - More ...

I recently started working on a project that may or may not make it into my Scala/FP book. I’m currently calling it “Akkazon Ekko” — or “Ekko” — because it’s a little like the Amazon Echo, but written with Scala and Akka.

Benedict Cumberbatch narrates a film about Vietnamese Buddhist monk Thich Nhat Hanh titled, Walk With Me. You can see the movie preview here on YouTube, and learn more at WalkWithMeFilm.com.

Table of Contents1 - The correct approach (simplified)2 - A thorough example for verification3 - The wrong approach4 - Summary

If you want to create multiple Scala Futures and merge their results together to get a result in a for comprehension, the correct approach is to (a) first create the futures, (b) merge their results in a for comprehension, then (c) extract the result using onComplete or a similar technique.

“If you speak and act with a pure mind, happiness will follow you as a shadow clings to a form.”

~ from the Kung Fu tv series

Android FAQ: When is the Android Fragment onCreateOptionsMenu method called?

I was just working through a problem with an Android Menu and MenuItem, and added some debug code to the methods in my Android Fragment, and found that the onCreateOptionsMenu method is called after onStart. I didn’t put Log/debug code in every activity lifecycle method, but for the ones I did add logging code to, the specific order of the fragment method calls looked like this:

When is the Android Fragment onCreateOptionsMenu method called?

After 84 days, the Sun finally set in Barrow, Alaska (now known as Utqiaġvik) on August 2, 2017.

(Image from this Twitter page.)

The Sun finally sets in Barrow, Alaska