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

I recently bought the second edition of Mastering the Core Teachings of the Buddha: An Unusually Hardcore Dharma Book (I already read the first edition), and it occurred to me as I was reading the introductory pages why I like this book so much: As the author (Daniel Ingram) writes, “... it is a very strange thing to have such a completely different language, set of experiences, and perspectives from most of the people around me. I can often feel like an alien wearing a trench coat of normalcy ... If you go way into this stuff, you will discover this same loneliness.” I know that feeling well.

If you’re debating about whether this book might be right for you, my recommendation is that if that description applies to you, this book is probably right for you.

Also, you don’t have to be a Buddhist to read the book. Mr. Ingram often explains mindfulness and meditation techniques in great detail, and without archaic language.

In fact, I think one of the prime reasons to buy the book might be the thought, “I had some sort of unusual experience that I find hard to talk to other people about, but I’m courageous enough to want to learn more about it, and understand it.” As Mr. Ingram explains, sometimes experiences that happen to some people while meditating happen to other people who have never meditated a moment before in their life.

I suspect that some of my early experiences happened because I was a pitcher on baseball teams in my high school years, and I took that very seriously and concentrated hard while pitching. While other players had more talent, I had great concentration (and control). These days when I meditate I sometimes think, “Yep, this feels just like the concentration required while pitching.”

If you want a truly hardcore book with no-nonsense language from someone who is willing to talk about these things, this book is for you.

This is an excerpt from my much longer blog post, How to Use By-Name Parameters in Scala.

“By-name” parameters — also known as call-by-name parameters — are quite different than by-value parameters. Rob Norris, (aka, “tpolecat”) makes the observation that you can think about the two types of parameters like this:

Today I spent some time going down the road of “What is a thunk in Scala?” Here’s some relevant text from this Wikipedia page. Note that I have deleted some lines, and modified others:

In computer programming, a thunk is a subroutine used to inject an additional calculation into another subroutine. Thunks are primarily used to delay a calculation until its result is needed, or to insert operations at the beginning or end of the other subroutine.

The term originated as a humorous, incorrect, past participle of “think.” That is, a “thunk value” becomes available after its calculation routine is thought through, or executed.

Sometimes I get away from writing Scala for a while, and when I come back to it I see a piece of code that looks like this following example and I wonder, “What is Foo, and how does this code work?”:

val f1 = Foo {
    println("hello from the `f1` instance")
    "this is the result of the block of code"

Well, one way it can work is that Foo can be a class that accepts a function parameter, and so everything inside those curly braces is the argument to Foo. For example, this code shows how a Foo class can be defined to accept a function as a constructor parameter:

case class Foo[A, B](f: A => B) {
    // more code here ...

That code says that Foo is a Scala case class that takes one constructor parameter, which is a function that transforms a generic type A into a resulting generic type B.

Going back to the first code I showed, everything inside the curly braces is that first argument to Foo:

Scala anonymous class example - passing a block of code

Informally speaking, everything inside a set of curly braces in Scala can be thought of as “a block of code,” and typically that block of code has some return value, such as "hello" in this example.

Facebook tells me that I found this quote by Janis Ian back in 2014. Still seems like a good idea. :)

Janis Ian - Buy a book instead of a drink

“My father always used to say, ‘Don’t raise your voice; improve your argument.’ Good sense does not always lie with the loudest shouters, nor can we say that a large, unruly crowd is always the best arbiter of what is right.”

~ Archbishop Desmond Tutu

“If we think we want to get joy for ourselves, we realize that it’s very shortsighted, short-lived. Joy is the reward, really, of seeking to give joy to others. When you show compassion, when you show caring, when you show love to others, do things for others, in a wonderful way you have a deep joy that you can get in no other way.”

“You can’t buy it with money. You can be the richest person on Earth, but if you care only about yourself, I can bet my bottom dollar you will not be happy and joyful. But when you are caring, compassionate, more concerned about the welfare of others than about your own, wonderfully, wonderfully, you suddenly feel a warm glow in your heart, because you have, in fact, wiped the tears from the eyes of another.”

~ Archbishop Desmond Tutu, in The Book of Joy

“Awareness admits no obstructions. It can be boundless even when we are confined.”

~ A Tibetan monk in Bloomington, Indiana (Arjia Rinpoche)

“Make today the most important day of the year, because tomorrow is not guaranteed.”

~ a quote from John Altobelli, who lost three-fifth’s of his immediate family in one day

Hold me in your thoughts
Take me to your dreams
Touch me as I fall into view

When the winter comes
Keep the fires lit
And I’ll be right next to you

Shadows are fallin’ and I’m runnin’ out of breath
If I leave you it doesn’t mean I love you any less
Keep me in your heart for a while

~ Keep Me in Your Heart for a While, Warren Zevon (lyrics slightly rearranged)

I was surprised to see this Scala 3 compiler error just now:

Class differs only in case ... Such classes will
overwrite one another on case-insensitive filesystems

What happened was that I created this code:

trait Hello:
    def greet = "hello"
trait Hi:
    def greet = "hi"

class Greeter extends Hello, Hi:
    override def greet = "I greet thee!"
@main def greeter =
    val g = Greeter()
    println(g.greet == "I greet thee!")   // true

And apparently the Greeter class and greeter method collide with one another to create this compiler error message:

[error] -- Error: src/main/scala/traits/mixin_order/MixinOrder.scala:18:10 
[error] 18 |    class Greeter extends Hello, Hi:
[error]    |          ^
[error]    |     |class Greeter differs only in case from
                 |class greeter in package mixins.test1.
Such classes will overwrite one another on case-insensitive filesystems.
[error] one error found

I don’t know if this error will continue to stay like this in Scala 3, but that’s the way it works today.

I originally wrote a long introduction to this article about how to work with the Scala Option/Some/None classes, but I decided to keep that introduction for a future article. For this article I’ll just say:

  • idiomatic Scala code involves never using null values
  • because you never use nulls, it’s important for you to become an expert at using Option, Some, and None
  • initially you may want to use match expressions to handle Option values
  • as you become more proficient with Scala and Options, you’ll find that match expressions tend to be verbose
  • becoming proficient with higher-order functions (HOFs) like map, filter, fold, and many others are the cure for that verbosity

Given that background, the purpose of this article is to show how to use HOFs rather than match expressions when working with Option values.

This is a view of the mountains from my apartment in Palmer, Alaska, way back in 2011. Palmer is surrounded my mountains on three sides, and these are the mountains you see to the south.

A view of the mountains from my apartment in Palmer, Alaska

I don’t remember where I saw this photo — it may have been the Fireside Books account on Twitter or Facebook — but it shows that they don’t worry about cleaning the snow off the streets too much in the winter in Alaska. This photo was taken about three blocks from my old apartment in Palmer, Alaska. (My apartment was one block down this road in the direction shown, and one or two blocks to the right.)

They don’t clean the snow off the streets in Alaska (much)

When I went to Texas A&M University, we discovered a restaurant just down the road in Bryan, Texas, called the “Chicken Oil Company.” Once there, we discovered that they made something called a “Deathburger,” known in this photo as the, “Hamburguesa de Muerte.”

The Chicken Oil Company “Deathburger”

I was just updating my Scala fold/reduce tutorial, and noticed that I start with this definition of a fold in computer programming from the excellent book, Learn You a Haskell for Great Good:

Folds can be used to implement any function where you traverse a list once, element by element, and then return something based on that. Whenever you want to traverse a list to return something, chances are you want a fold. That’s why folds are, along with maps and filters, one of the most useful types of functions in functional programming.”

That’s a great definition because it includes (almost) everything, so I’ll add this:

  • You traverse an entire list, once
  • Element by element
  • You apply your own custom algorithm to the elements as you traverse the list
  • In the end, you return a single value based off of that traversal and the application of your algorithm

If you’re interested in the definition of a fold in computer programming, I hope that’s helpful. See my fold/reduce tutorial for many more details.

Back in 2016 I posted this photo on Facebook with the caption, “The current minefield that may one day grow into a book.” Those notes eventually grew into Functional Programming, Simplified.

The notes that became Functional Programming, Simplified

When you get started with functional programming (FP) a common question you’ll have is, “What is an effect in functional programming?” You’ll hear advanced FPers use the words effects and effectful, but it can be hard to find a definition of what these terms mean.

I haven’t watch all of this video yet, but from what I have seen, it appears that Steve Jobs secret passion was Japanese art.

The secret passion of Steve Jobs (Japanese art)

As a brief note to self, I started looking into writing my own Scala testing framework, mostly because the main current frameworks are not currently available for Scala 3.0-M3. That led me to this Minitest blog post, the Minitest repository, and finally this sbt test-interface repository. It looks like if you want to write your own testing framework to work with sbt, that interface is what you need to implement.

I just tested Minitest with Scala 3, and it seems to work, so for now I’m not going any further. But I was happy to find that interface project, because I have often wondered what it would take to write a testing framework to work with sbt.