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

July 28, 2010, Wasilla, Alaska: Bundled up in a semi-rainproof jacket and hat, I just returned from a walk in the cold November rain. Most neighbor's fireplaces were in full use, while one man in a jacket and shorts and smoking a cigarette was mowing his weeds with his lawnmower. Hard to believe, Denali National Park closes for the winter in just over six weeks.

A lot of people seem to create/self-inflict a lot of stress in their own lives, and that’s based on how they perceive situations. In that regard I like this quote by Broncos backup quarterback Kevin Hogan:

“Control what I can control and move on from there.”

Dateline: July 27, 2010, Wasilla, Alaska.

“It looks like she passed away around 4:30,” Al said, holding his neighbor’s just-deceased cat, and looking at the clock on the wall.

“No,” Neighbor #1 replies, wiping her eyes with her kleenex. “That clock doesn’t work. It’s almost 8:30.”

“Oh,” says Al, looking out the window of the second-floor apartment and seeing what appears to be afternoon light in the treetops. Funny how the Alaskan summer sun still throws the perception of time out of balance.

“Can you give us a ride to the hospital? My wife just cut her finger open,” yells unemployed car-less Neighbor #2, suddenly appearing at the open front door. His wife screams from somewhere down below. Neighbor #3, a former police deputy, instinctively gets up to help, but Neighbor #4 says, “I’ve got this one,” and hops off the couch and out the door behind #2. He looks comfortable in his shorts and t-shirt in the mid-40s temperature, as Al shivers.

As they run out the door, Neighbor #5, just home from her job at Carr’s, stands in the doorway, looks around, sees three neighbors and a dead cat in the apartment next to hers, and doesn’t seem to know what she should say or do ...

“My current advice to everyone is this: fall in love with your life. Pursue the things that bring you joy. Let go of things that don’t. Know the difference.”

~ Edie Freedman, Creative Director at O’Reilly Media

“I realize that most inventions fail not because the R&D department can’t get them to work, but because the timing is wrong‍ — not all of the enabling factors are at play where they are needed. Inventing is a lot like surfing: you have to anticipate and catch the wave at just the right moment.”

~ Ray Kurzweil

I just saw this “Prayer of Saint Francis” in a collection of photos from a church in Santa Fe, New Mexico, and it reminds me of the creed of a superhero. :)

Prayer of Saint Francis, Santa Fe, New Mexico

Ferguson Jenkins was one of my favorite pitchers to watch when I was very young. Unlike other “throwers,” he was a true “pitcher,” getting by more by his control and changing speeds than having a blazing fastball. I created this “artistic” image of him pitching using Gimp.

Ferguson Jenkins, Chicago Cubs (painting)

It’s worth mentioning that my last post about a glass teapot was inspired by a book titled Emotional Design: Why We Love (or Hate) Everyday Things by Don Norman. In that book he shows this image of three teapots, and the glass one in the middle is known as a “Nanna teapot.” I just saw that one sold on eBay for $275; that’s a little more than I had in mind. :) Mr. Norman earlier published a best-selling book titled The Design of Everyday Things.

Nanna teapot (from Don Norman's Emotional Design)

When I saw this tweet this morning:

[DOG MAGICIAN] think of a color, any color ... is it ... gray?

[OTHER DOG] oh my GOD

I knew that I loved the joke, but I didn’t like the presentation. I wanted to put the joke on Facebook, but I know that people like images more than they like text, so I made a second cup of coffee and began putting the text on an image.

After getting an infection in my eyes this week, someone suggested I try echinacea (which is not recommended for people with weakened immune systems). That reminded me of this “euthanasia, echinacea” tweet.

Euthanasia, echinacea, whatever

I just received notice that the Apple Store in Anchorage, Alaska has reopened. Might be time to move back, although I have no idea what “all the new Today” means. :)

The Apple Store in Anchorage, Alaska has reopened

This is an excerpt from the 1st Edition of the Scala Cookbook (partially modified for the internet). This is Recipe 14.6, “How to disassemble and decompile Scala code.”

Problem

In the process of learning Scala, or trying to understand a particular problem, you want to examine the source code and bytecode the Scala compiler generates from your original source code.

Solution

You can use several different approaches to see how your Scala source code is translated:

As a quick note, I just got a little bit better about logging stack traces when writing Java or Scala code. In Scala I used to get the text from a stack trace and then log it like this:

// this works, but it's not too useful/readable
logger.error(exception.getStackTrace.mkString("\n"))

In that code, getStackTrace returns a sequence, which I convert to a String before printing it.

I didn’t think about snakes at all when I lived in Alaska, but here in Colorado they cross my mind from time to time. Following up on my previous article, Can snakes see?, here’s some information on what snakes do in the winter, from snakeprotection.com:

“Snakes do not actually hibernate, rather they become less active during cold weather. It is called ‘brumation.’ Brumation is an extreme slowing down of their metabolism. Snakes are awake, but just very lethargic so you don’t see them moving around. In the fall, snakes move back to the previous year’s den. If a sudden cold snap catches them before they get there, they may die if not fortunate enough to find a suitable secondary den. A number of species may share the same den. For example, black rat snakes, timber rattlesnakes, and copperheads commonly den together. Sometimes there will be as many as 100 snakes in one cave. A group site is called a hibernaculum.”

Unix/Linux grep FAQ: How can I perform a recursive search with the grep command in Linux?

Two solutions are shown next, followed by some additional details which may be useful.

Solution 1: Combine 'find' and 'grep'

For years I always used variations of the following Linux find and grep commands to recursively search sub-directories for files that match a grep pattern:

find . -type f -exec grep -l 'alvin' {} \;

This command can be read as, “Search all files in all sub-directories of the current directory for the string ‘alvin’, and print the filenames that contain this pattern.” It’s an extremely powerful approach for recursively searching files in all sub-directories that match the pattern I specify.

Here’s an explanation of those command-line arguments:

  • The name of the command is find
  • The “.” means “current directory”
  • The -type f part means “only search for files (not directories)”
  • -exec means “execute the following command”
  • grep -l 'alvin' is the command that searches the files for the string “alvin
  • The {} \; part of the command is a little unusual ... I think it’s an old artifact of the find command, but you can think of that area as holding each file that the find command finds, and then grep operates on each one of those files, one file at a time

Solution 2: 'grep -r'

However, I was just reminded that a much easier way to perform the same recursive search is with the -r flag of the grep command:

grep -rl alvin .

As you can see, this is a much shorter command, and it performs the same recursive search as the longer command, specifically:

  • The -r option says “do a recursive search”
  • The -l option (lowercase letter L) says “list only filenames”
  • As you’ll see below, you can also add -i for case-insensitive searches

If you haven’t used commands like these before, to demonstrate the results of this search, in a PHP project directory I’m working in right now, this command returns a list of files like this:

./index.tpl
./js/jquery-1.6.2.min.js
./webservice/ws_get_table_names.php

More: How to search multiple sub-directories

Your recursive grep searches don’t have to be limited to just the current directory. This next example shows how to recursively search two unrelated directories for the case-insensitive string "alvin":

grep -ril alvin /home/cato /htdocs/zenf

In this example, the search is made case-insensitive by adding the -i argument to the grep command.

Using egrep recursively

You can also perform recursive searches with the egrep command, which lets you search for multiple patterns at one time. Since I tend to mark comments in my code with my initials ("aja") or my name ("alvin"), this recursive egrep command shows how to search for those two patterns, again in a case-insensitive manner:

egrep -ril 'aja|alvin' .

Note that in this case, quotes are required around my search pattern.

Summary: `grep -r` notes

A few notes about the grep -r command:

  • This particular use of the grep command doesn’t make much sense unless you use it with the -l (lowercase "L") argument as well. This flag tells grep to print the matching filenames.
  • Don’t forget to list one or more directories at the end of your grep command. If you forget to add any directories, grep will attempt to read from standard input (as usual).
  • As shown, you can use other normal grep flags as well, including -i to ignore case, -v to reverse the meaning of the search, etc.

Here’s the section of the Linux grep man page that discusses the -r flag:

-R, -r, --recursive
Read all files under each directory, recursively; this is
equivalent to the -d recurse option.

  --include=PATTERN
  Recurse in directories only searching file matching PATTERN.

  --exclude=PATTERN
  Recurse in directories skip file matching PATTERN.

As you’ve seen, the grep -r command makes it easy to recursively search directories for all files that match the search pattern you specify, and the syntax is much shorter than the equivalent find/grep command.

For more information on the find command, see my Linux find command examples, and for more information on the grep command, see my Linux grep command examples.

“People inspire you, or drain you. Pick them wisely.”

~ Hans F. Hansen (image from positivelifetips.com)

People inspire you, or drain you. Pick them wisely.

This is an excerpt from my book on Functional Programming in Scala. It’s an appendix that “explains and explores” Scala’s function syntax.

Background

I wrote in the “Functions are Values” lesson that most developers prefer to use the def syntax to define methods — as opposed to writing functions using val — because they find the method syntax easier to read than the function syntax. When you write methods, you let the compiler convert them into functions with its built-in “Eta Expansion” capability. There’s nothing wrong with this. Speaking as someone who used Java for 15+ years, the def syntax was easier for me to read at first, and I still use it a lot.

A favorite quote from an all-time favorite movie, Peaceful Warrior.

You need to start asking better questions

I don’t remember the origin of this photo, but here are two moose fighting in Palmer, Alaska.

I lived in Palmer when I first started going through the thyroid issues, and because of those issues I’d go for walks at any time during the day, 24 hours a day. One late night or early morning I went for a walk and came across a moose, but fortunately I saw it early and it was a non issue. He or she went on their way, and I went the other way.

Two moose fighting in Palmer, Alaska