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

When I first saw Scala 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

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:

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.9, Simulating Dynamic Typing with Union Types.

Problem

You have a situation where it would be helpful if a value could represent one of several different types, without requiring those types to be part of a class hierarchy. Because the types aren’t part of a class hierarchy, you’re essentially declaring them in a dynamic way, even though Scala is a statically-typed language.

Solution

In Scala 3, a union type is a value that can be one of several different types. Union types can be used in several ways.

In one use, union types let us write functions where a parameter can potentially be one of several different types. For example, this function, which implements the Perl definition of true and false, takes a parameter that can be either an Int or a String:

Summary: This tutorial shows how to use Unix/Linux curl scripts to test REST/RESTful web services.

There may be better ways to do this, but when I was writing a mobile app — with a JavaScript client written in Sencha Touch, and the server written with the Play Framework — I wrote some curl scripts to simulate GET, POST, DELETE, and PUT request (method) calls to my Play Framework REST/RESTful web services.

You can also write REST clients with Scala, Java, and other languages, but for various reasons I wanted to test these web services with curl. As an added bonus, you can include scripts like this into your testing and integration process, if you like. You can also use the same approach with Nagios to make sure your service is still running.

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

As a brief note, SQLite doesn’t have date/time (datetime) fields, so when you want to use a datetime field with Flutter and Dart, you have to do something else.

My choice was to store my date/time data as a SQLite INTEGER field. I did this so I can query and sort that field easily myself. The benefit of storing a datetime field as a TEXT field is that you can read it easier, but I think an INTEGER field is a little easier to work with, though YMMV.

“Your ego is writing checks your body can’t cash.”

~ from the movie, Top Gun

This is an excerpt from the Scala Cookbook, 2nd Edition. This is Recipe 23.8, Using Term Inference with Given and Using.

Scala 3 Problem

Using Scala 3, you have a value that’s passed into a series of function calls, such as using an ExecutionContext when you’re working with Futures or Akka actors:

My 700+ page book, Functional Programming, Simplified — 4.5-star rated on Amazon, their 6th-best selling book on functional programming, and 5-star rated on Gumroad.com — is currently on sale in three formats (prices shown in USD):

PDF Format
$15 (sale!) on Gumroad.com

PDF version of Functional Programming, Simplified

Paperback Book
Now $34.99 on Amazon

Print version of Functional Programming, Simplified

Kindle eBook
$14.99 on Amazon

Kindle version of Functional Programming, Simplified

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.

I don’t agree with everything Nathaniel Hackett has done to start his tenure with the Denver Broncos, but I do agree with this quote about trying to create a good and happy work environment:

“You want them to feel an environment that they want to come into, and they’re excited to come into. That’s all you can do, that’s what you’re trying to create.”

I’m creating this page as a collection of Flutter source code snippets. These are early days, but here you go.

Note also that my Flutter Quick Reference has many more examples as well.

Replacement home page

Here’s a link to my Flutter replacement home page code.

This may not make sense to most people, but if you ever want a simple replacement Flutter “home page” for a new app, this should work. All it does is create a home page with a ListView, and you can use those ListView items as simple menu items to get started with.

I also added a FloatingActionButton in case you want that to start with as well. Note that you’ll probably want to remove the @dart line for modern Flutter apps. (I’ll try to remember to update this code the next time I use it.)

I prefer this to the default home page you get when running flutter create my_app. My goal is to be able to highlight the default Flutter home, delete that code, and replace it with this code as a simple starter menu.

Here’s the code:

In one of the crazier bugs I’ve seen, I got to a point where I had my Flutter app in good shape, and I had been testing it on an Android device. So as one last test I wanted to make sure I deleted the app from the Android device — a Kindle Fire tablet — so I could then do a complete reinstall of my app so I could give it one more quick test.

And then it never installed properly.

The app wouldn’t install on the device

It seemed like the app started to install on my Android device, but then it wouldn’t. I tried all sorts of different flutter run commands, but the app just wouldn’t install. It seemed a little like it was hung up (stalled), but it also seemed a little like it also finished installing and was sitting there waiting for me. (At the moment I don’t remember the exact messages the Flutter build process was showing.)

As a brief note today, if you ever need to debug some Dart/Flutter code to see how much time some code is taking, here’s a manual approach that you can use to determine the “delta time” between two statements:

final t1 = DateTime.now();
// the source code you
// want to test is in here
final t2 = DateTime.now();
debugPrint('DELTA TIME: ${t2.difference(t1)}');

I just used this code to find a significant bottleneck, so while it’s not as sophisticated as other techniques, it was a nice performance-debugging tool today.

One thing I like about using Flutter to build mobile apps is that like Scala, it’s low-ceremony: every character has a purpose that isn’t redundant. It’s also a modern take on creating UIs, where your code is just widgets, event-handling, and custom logic.

For example, with a little extra file/directory logic that’s not shown here, this code is all you need to list the files in a directory on both iOS and Android devices.

How Flutter is like Scala (and why I like them both)

Half of my DNA is Assyrian, and I just learned that the Assyrian New Year is April 1st, so to celebrate that I’ve reduced the price of both the Print and Kindle versions of my book, Functional Programming, Simplified. I’ll keep the prices this low until April 15, 2022, so be sure to grab a copy before then.

SALE: Functional Programming, Simplified

As a brief note, these are two different ways to do a “sleep” call in Flutter/Dart, depending on your needs:

// inside an async method
await Future.delayed(const Duration(milliseconds: 250));

// not in an async method
sleep(const Duration(milliseconds: 250));

As shown, the first approach is for when you want an async/await approach, and the second sleep method is for when you are doing things in serial order (as opposed to a parallel/concurrent approach).