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

NOTE: If you read my previous article (a Scala functional programming style To-Do List application), the new content in this article starts down at The Scala/FP Code section.

Back when I was writing Functional Programming, Simplified I started to write a little Scala/FP “To-Do List” application that you can run from the command line, based on a similar application in the Learn You A Haskell For Great Good book. For reasons I don’t remember, I decided not to include it in the book, and forgot about it until I started using GraalVM (“Graal”) recently.

In functional programming, side effects are kind of a big deal

I originally wrote a long introduction to this tutorial 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.

Maybe because of my Back To Now app, I really like this quote about remembering from Ram Dass:

“I think that remembering is the strategy that most religions are designed to do. It’s remembering there are other planes of consciousness, it’s remembering the illusory nature. It’s remembering Maya, it’s remembering Dukkha. It’s remembering the karma, the sangha, the Buddha, it’s remembering that you’re not caught on one plane of consciousness. It’s reminding you to wake up. The device is to wake you up.”

That quote comes from this ramdass.org page.

I drove through Chicago recently and with my crappy old phone’s camera I got this photo of the pink and orange Moon rising over Greater Chicagoland. It was a clear night and the Moon was visible for the entire drive.

Pink/orange Moon rising over Greater Chicagoland

I moved to Colorado after Josh McDaniels was fired by the Broncos, and to say the least, from what I’ve heard on the local radio, he sounds like a completely different coach with the Raiders (per this SI.com article):

“One of the easy things that we’ve tried to keep in mind is we’ll get the best out of everyone here if they love coming to work every day because they love who they’re working for,” said McDaniels, sitting behind his desk. “That sounds so ridiculously simple. Seriously. But it’s the truth. If the players enjoy being coached by us the way we’re coaching them, if the coaches enjoy being treated the way they’re being treated, if the scouts and the personnel department enjoy the way that [GM] Dave [Ziegler] runs the meetings and gives everybody a voice, then when they drive in here in the morning? You should see this.”

“I’d say for me that’s from being a guidance counselor, being a teacher, those experiences,” Ziegler said. “The relationship was really what you had to solidify first to make any progress, whether you’re a classroom teacher or a guidance counselor, and you’re trying to help someone through a problem.”

“They won’t trust you if you don’t,” McDaniels said.

“As a guidance counselor, you have to create it on the front end so if there is a problem down the road, you’ve already created the relationship, so you can help someone that’s having a mental health issue, or going through something else,” Ziegler continued. “The relationship has to come first.”

Summary: This page is a printf formatting cheat sheet or reference page. I originally created this printf cheat sheet for my own programming purposes, and then thought it might be helpful to share it here.

printf: Many languages, same syntax

A great thing about the printf formatting syntax is that the format specifiers you can use are very similar — if not identical — between different languages, including C, C++, Java, Python, Perl, PHP, Ruby, Scala, Kotlin, and others. This means that your printf knowledge is reusable, which is a good thing.

I saw the following image on this Twitter page:

and immediately became curious, “How can I create something like that Ruby %Q function in Scala, but where each line becomes a string in a list, i.e., a Seq[String]?”

“It is not the critic who counts; not the man who points out how the strong woman stumbles, or where the doer of deeds could have done them better.”

“The credit belongs to the woman who is actually in the arena, whose face is marred by dust and sweat and blood; who strives valiantly; who errs, who comes short again and again, because there is no effort without error and shortcoming; but who does actually strive to do the deeds; who knows great enthusiasms, the great devotions; who spends herself in a worthy cause; who at the best knows in the end the triumph of high achievement, and who at the worst, if she fails, at least fails while daring greatly, so that her place shall never be with those cold and timid souls who neither know victory nor defeat.”

~ “The Man in the Arena” speech by Teddy Roosevelt, with a few minor changes, which I was reminded of by this article

As a brief note to self, here’s some Laminar source code that shows how to create a div in Laminar, as well as some settings/elements in that div, as well as one way to handle a reactive button click:

In my first Laminar tutorial I showed how to set up a Scala/sbt project to use Laminar to render “static” HTML code. Then in Laminar 102: A reactive “Hello, world” example, I showed how to create a small, reactive Laminar example, that sends signals from one widget to another using observables and observers.

In my experience, the next thing you’ll need to know about Laminar is routing, i.e., how to transition from one page to another in a single-page app (SPA). Therefore, in this tutorial I’ll show how to implement that for very small applications.

In my first Laminar tutorial I showed how to set up a Scala sbt project to use Laminar, and then showed a “static” example — i.e., there were no moving parts. Please see that tutorial first if you’ve never used Laminar.

BUT, because Laminar is meant for writing reactive applications with observables and observers, this tutorial begins to show its reactive concepts.

I recently started a new Scala project that uses the Scala/Scala.js Laminar library for frontend development (i.e., as a JavaScript replacement).

Laminar is a library that’s built on top of Scala.js to let you build “reactive” web applications with observables, and in this tutorial I’ll show how to create a static “Hello, world” application from scratch. Once we get past these basics, I’ll show in Part 2 of this series how to create a reactive single-page application (SPA) with Laminar.

When I first saw Scala generic type and multiple-parameter group code like this ~12 years ago, my initial thought was, “Wow, maybe I need to think about a different career”:

def race[A, B](lh: IO[A], rh: IO[B])(implicit cs: ContextShift[IO]): IO[Either[A, B]]

But in the end, as Rocky once said, it ain’t so bad. :)

Rocky: You ain’t so bad

For many years I struggled with how to combine two of my main interests, Zen and work. I had read that the Zen mind is the mind before thinking, so it seems like Zen and work must be totally unrelated, because you need your mind to work. And then over time I came to understand phrases like, when working, just work; in computer parlance, become single-threaded.

This article contains a collection of quotes that have been helpful to me in understanding the relationship between Zen and work. Please note that I don’t wrap each quote in double quotes, and I also try to attribute each quote to the correct author/speaker. If you’re interested in how to combine Zen and work, I hope you’ll find them helpful.

At some point somebody was like, “Let’s get a mast cell — a type of white blood cell — from a bone marrow biopsy, magnify it 1,000 times, piss it off, and see what happens.”

The result? Ka-boom! It looks like a little firework went off when it released its histamine, tryptase, serotonin, superoxide, heparin, thromboxane, PGD2, PAF, and other granules.

That’s pretty much what it feels like, lol. I used to tell doctors that it felt like I had been drugged, and indeed, I was.

(Image from this nih.org research paper.)

MCAD: What an activated mast cell looks like

Over on my Valley Programming website I wrote a little User Story Mapping Example Using Facebook. Whether people currently have a Facebook account or not, at one time almost everyone did, so it makes for a good example when running a story mapping workshop. See that link for more details.

A User Story Mapping Example Using Facebook

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 3.10, Using Java Collections in Scala.

Problem

You’re using Java classes in a Scala application, and those classes either return Java collections, or require Java collections in their method calls, and you need to integrate those with your use of Scala collections.

Solution

Use the extension methods of the scala.jdk.CollectionConverters object in your Scala code to make the conversions work. For example, if you have a method named getNumbers in a public Java class named JavaCollections:

Flutter 3 was released in mid-May, 2022. You can read more about it here on 9to5google.com, and on other websites.

Flutter 3 release, May, 2022

Reporting live from Longmont, Colorado in May, 2022, I’m glad to report that I am now a Certified ScrumMaster (CSM) and Certified Scrum Product Owner (CSPO).

Sometimes this doesn’t feel like much, but in retrospect it’s amazing how much I know now compared to what I used to know. Last October I couldn’t have gotten many answers right on the practice tests and real test, but now the answers are obvious. So kudos to the Scrum Alliance for their rigorous certification training courses.

Certified ScrumMaster and Product Owner, Longmont, Colorado

I don’t watch many movies or tv shows, at least not as many as most people. The reason I don’t is because I experience lucid dreams most night that Hollywood can’t compare to. I don’t know why I have these dreams, but I have had them since I was a young child, and that’s just the way things are. This story from May 2, 2022, is just one example.

~~~

There was a bomb in the building.

Somehow I knew that.

I remember walking into a government-style building that had a parking lot directly in front of it. If you walked into the front doors, went down the hallway to the right, and then took your first left, there was a device on the floor that looked like a boxy, 1970s-style tape recorder, but it was really a bomb.

So I began working with a friend to evacuate the building. He went one way, I went the other.

As I ran around the building, Lori — my friend who died last summer — ran up to me and said in a panic, “He knows everything about you. Everything! You have to run.” I looked at her and tried to make sense of how she was here and what she was saying, but she was terrified and screamed, “RUN!” Then she ran away in the direction I had just come from.