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

“When someone shows you who they are, believe them the first time.”

~ Maya Angelou

Table of Contents1 - Scala 2: Create the method in an implicit class2 - Scala 3 (Dotty): Adding methods to closed classes with extension methods

Scala lets you add new methods to existing classes that you don’t have the source code for, i.e., classes like String, Int, etc. For instance, you can add a method named hello to the String class so you can write code like this:

"joe".hello

which yields output like this:

"Hello, Joe"

Admittedly that’s not the most exciting method in the world, but it demonstrates the end result: You can add methods to a closed class like String. Properly (tastefully) used, you can create some really nice APIs.

In this article I’ll show how you can create implicit methods (also known as extension methods) in Scala 2 and Scala 3 (Dotty).

“On one occasion of my own practice, nearing deep samadhi, I happened to notice that the stage of my mind was quietly turning and a new scene was appearing. In this new scene no wandering thought popped up its head; there was absolute stillness and silence, as if one had landed on the Moon.”

~ Zen Training

Table of Contents1 - A shell script solution2 - A JavaFX GUI3 - Summary

I recently started using AsciiDoc to write a new book. A great thing about it is that unlike Markdown, you can use AsciiDoc to write a book and get all of the features you want in a book, including linking between anything, captions for tables and figures, indexes, etc. Because this got me started using AsciiDoc I thought, “Wouldn’t it be nice if I could also use AsciiDoc to write blog posts like this one?”

Sadly, I quickly ran into a problem: I couldn’t find a good way to convert AsciiDoc into HTML, or even Markdown. There are tools to convert AsciiDoc to HTML, but for some reason they take the approach of including a ton of markup in the HTML (divs, spans, and attributes), and as far as I can tell there’s no way to turn off that markup.

Back to top

A shell script solution

“One woman can make you fly like an eagle, another can give you the strength of a lion, but only one in the cycle of life can fill your heart with wonder and the wisdom that you have known a singular joy.”

~ Twin Peaks

From a translation of the Tao Te Ching:

The master, by residing in the Tao (the Way),
sets an example for all beings.

Because he doesn’t display himself,
people can see his light.

Because he has nothing to prove,
people can trust his words.

Because he doesn’t know who he is,
people recognize themselves in him.

(I recommend that third stanza in particular for people who are interested in consulting.)

I ended up not using this code, but if you wanted to see one way to use JSoup’s OutputSettings (Document.OutputSettings) class to set some parameters before calling JSoup.clean, I hope this is helpful:

// tried some things to improve the html output
val settings: OutputSettings = new OutputSettings
settings.prettyPrint(true)  //`true` is default
settings.charset("UTF-8")
settings.outline(true)  //this is close to what i want, but too extreme
settings.indentAmount(4)
val cleanHtml: String = Jsoup.clean(html, "", wl, settings)

I can attest that this code works, it’s just not what I need at the moment.

Also, the code shown is written in Scala, but as you can see, it converts easily to Java.

I haven’t been back to Alaska in a while now, but this is what the movie theater in Homer, Alaska looked like the last time I was there.

The movie theater in Homer, Alaska

“How can we prevent our thoughts from wandering? How can we learn to focus our attention on one thing?”

“The answer is that we cannot do it with our brain alone; the brain cannot control its thoughts by itself. The power to control the activity of our mind comes from the body, and it depends critically on posture and breathing.”

~ From the book, Zen Training, by Katsuki Sekida

If you ever need a Scala method/function to convert a string with newline characters in it to a sequence of strings (Seq[String]), here you go:

def convertStringWithNewlinesToSeq(s: String): Seq[String] =
    s.split("\n").toVector

You can convert the final result to a Vector, Seq, List, ArrayBuffer, Array, etc., but I prefer Vector. The Scala REPL demonstrates how it works:

scala> convertStringWithNewlinesToSeq("")
res0: Seq[String] = Vector("")

scala> convertStringWithNewlinesToSeq("foo")
res1: Seq[String] = Vector(foo)

scala> convertStringWithNewlinesToSeq("foo\nbar\nbaz")
res2: Seq[String] = Vector(foo, bar, baz)

scala> convertStringWithNewlinesToSeq("foo\nbar\nbaz\n\n")
res3: Seq[String] = Vector(foo, bar, baz)

If there’s a dark side to writing books and having a website that gets millions of views, it’s that people who seem to have a lot of angst think it’s okay to leave mean-spirited comments on the website. I moderate all the comments, so outside people never see them, but I’m amazed at some of the angry posts people write. Part of me would like to reply, “Sorry, I didn’t write Unix, Java, Scala, Gimp, etc., I just try to explain how they work,” but instead I just delete their comments (because they never leave a name or email address).

My guess is that the people who write these things are angry at something else and for whatever reason that leads them to think that they can post a mean comment here anonymously, but yeesh, every once in a while the haterade gets to me and I just turn off the ability to comment completely for days or weeks. To the people that post mean-spirited comments like that, you need to take responsibility for your own life and/or get some help.

“The way you look at things is the most powerful force in shaping your life. In a vital sense, perception is reality.”

~ John O’Donohue, Anam Cara: A Book of Celtic Wisdom

(Or, as I wrote many years ago, all you are is attitude.)

Here’s a photo of a big sky over the Rocky Mountains, just south of Boulder, Colorado on January 28, 2014. I saw this same sky in Denali, Alaska once many years ago. Breathtaking both times. (Click the image for a much larger version of it.)

Big sky over Boulder, Colorado

As a brief note, here are a couple of examples about why you shouldn’t use double or float values for currency, courtesy of Joshua Bloch’s Effective Java book:

scala> 1.03 - .42
val res0: Double = 0.6100000000000001

scala> 1.00 - 9 * 0.10
val res1: Double = 0.09999999999999998

I don’t know who created this image, but I’m all for it. Make America kinda again. Make America compassionate again. Make America smart again. Any sort of intelligent, enlightened leadership in this country would be wonderful.

Make America Kind Again

“No dreams come without a bill. The reality of making something happen is just a ton of hard work.”

~ Peter Gabriel

On the recent drive back to Colorado I listened to Midnight in the Garden of Good and Evil. In the book, The Lady Chablis talked about how much the estrogen shots affected her, mentally and physically — her thoughts, such as who she was attracted to, and her physical attributes.

I’ve often thought about how our thoughts and behavior are affected by our hormones. That’s one reason I like meditation: The farther you get away from the physical body and chemically-influenced brain, the more you can figure out who you are.

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:

Don’t judge each day by the harvest you reap, but by the seeds you plant.

~ Robert Louis Stevenson

This is a photo of some snowy trees in Louisville, Colorado, on March 27, 2018. I had to get up early to go to a physical therapy appointment and I was rewarded with this view.

Snowy trees, Louisville, Colorado