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

Quick note: Since The Mood Cure helped me so much with my thyroid problems last year, I bought The Diet Cure, and have been reading through it. In short, the power of amino acids blows me away. While I've never felt any different after taking any other "supplements", the difference I've felt by taking amino acids has been amazing.

Today I just want to make a note about the differences between DL-Phenylalanine, L-Phenylalanine, and D-Phenylalanine. Here's a description of Phenylalanine, courtesy of this website:

If you're thinking about giving to a charity this year, please consider giving to the Zen Foundation. Our organization has one simple purpose:

"The Zen Foundation will freely distribute classic Zen books to libraries, schools, healthcare facilities, and other locations where people can discover Zen."

With this mission, we hope other people will discover Zen in the same way we did: By stumbling onto one of the classic Zen texts, flipping through the pages, and beginning to wonder.

I was just reminded of an old trick to make your Mac OS X display black and white, or grayscale. To do this, just follow these steps:

Scala string FAQ: How do I embed variables in strings in Scala?

A lot of nice new features were added in Scala in version 2.10, but a simple feature that makes me happy is string interpolation. This lets Scala developers embed variables inside strings just like you can do in other languages like Perl, PHP, and Ruby -- but perhaps in an even more powerful way. (I say "perhaps" because I haven't used those other languages recently.)

[toc hidden:1]

Just a quick note that Scala 2.10.0 was released recently, and the following page, Scala 2.10.0 now available, contains a nice summary of what's new in the official Scala 2.10 release. The short list of new features includes:

Summary: Cato is the name of a language-independent, template-based, database driven “CRUD Generator” I created, and in the video below I show how to use it to create a complete set of Play Framework 2 CRUD forms, including model, controller, view, and Anorm database code.

When it comes to generating random strings with the scala.util.Random class, I haven’t figured out yet how to properly use the nextString method. I’ve tried using it in several different ways, but I always get a string of question marks as output, like this:

If you ever need to use a Scala for loop (for comprehension) with one or more embedded if statements, I hope the following example is helpful:

Scala script FAQ: How do I set the CLASSPATH in a Scala shell script?

If you need to set the CLASSPATH when executing a Scala script, use the exec command as shown in the following example:

#!/bin/sh
exec scala -classpath "lib/htmlcleaner-2.2.jar:lib/scalaemail_2.9.1-1.0.jar:lib/stockutils_2.9.1-1.0.jar" "$0" "$@"
!#

As you can see from that code, I'm adding three jar files to my classpath at the beginning of my Scala shell script.

It's been a while since I've seen a problem related to using different versions of Scala, but I just ran into it now.

Scala string FAQ: Does Scala have a String method like chomp or chop that you find in Perl?

If you're using Scala and want a string chomp or chop method that you're used to in languages like Perl to remove end of line characters, the stripLineEnd method will do what you want, as shown in the REPL:

With the end of every year I like to take a little time to look back, and for the last few years I've been noting which songs I listened to most during the previous year. This is my list of the songs I played most during 2012.

The list is a little skewed, because in my world there's Guns 'n Roses, Godsmack, The Dixie Chicks, The Cranberries, and then everyone else. So, in the "Everyone Else" list, here are my most listened to songs of 2012. The Top 5 are shown at the bottom.

Scala regex FAQ: How can I match a regex pattern to an entire string in Scala?

This morning I needed to write a little Scala code to make sure a String completely matches a regex pattern. I started off by creating a Scala Regex instance, and then realized the Regex class doesn't have a simple method to determine whether a String completely matches a pattern.

For reasons shown in the following image, I decided it was best to leave Santa Fe last night, and drive back to Colorado:

Snow storm in Santa Fe, New Mexico

That weather doesn't look too bad, but people were scattered all over the sides of the road when I drove down to Santa Fe on Monday, and there was only one lane open on I-25, and there was also at least one fatality.

There was a little funky activity on a client's Drupal 6 website that was hosted at GoDaddy, and without having access to an Apache access log file, I wanted to be able to see what was going on. So I wrote the following PHP code snippet to do some manual logging, and placed it in the Drupal theme's page.tpl.php file:

If you ever need an example of how to do something when a Play Framework application starts up, check out the Zentasks application in the Play distribution samples directory. You'll find the following code in the Global.scala file in the app subdirectory of the project. It shows how to use the onStart method of the Global object to populate some sample data when the Play Framework app starts up.

Here's the source code for that class:

I just saw the following Scala source code in a Play Framework 2 sample application, and it struck me as a nice example of how to call the foreach method on a Seq to populate some sample data:

I had a problem using the CKEditor with Drupal 6 where the CKEditor would not display <code> tags properly in the editor, and would then delete trailing spaces after the <code> tag. After some digging around, I finally found that I needed to comment out the following line in the ckeditor.config.js of my CKEditor module installation:

While looking for code on how to create a random string in Scala, I came across this article, which shows one approach for creating a random string. For my brain today, it also shows a nice way to create a list of alpha or alphanumeric characters.

For instance, to create a list of alphanumeric characters, use this approach:

val chars = ('a' to 'z') ++ ('A' to 'Z') ++ ('0' to '9')

To create a list of only alpha characters, just trim that code down to this:

val chars = ('a' to 'z') ++ ('A' to 'Z')

In the Scala REPL, you can see that this creates a Vector, which is a read-only, indexed sequence:

scala> val chars = ('a' to 'z') ++ ('A' to 'Z')
chars: scala.collection.immutable.IndexedSeq[Char] = Vector(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z)

Using the ++ operator (a method, actually) on Scala collections is a common way to merge/join/concatenate collections, and when I saw this code, I thought of what a great example it is of the ++ method.

It also shows that you can create a range of characters in Scala, which is something I didn't know you could do until recently. That's one of the things I love about Scala, it's a deep language, and I keep learning something new about it every day.

Yesterday I created a video tutorial on the Scala REPL and var & val variable types, and today I took a little time to create a new "Hello, world" Scala video tutorial. Without any further ado, here's the video:

If you're in the market for a Scala "Hello, world" tutorial, I hope you like it.