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

While working on a Java application yesterday, I ran into a situation where I needed to search my POP3 mailbox for unseen messages, i.e., email messages I haven't read yet. (I'm adding some "You've got mail" functionality to one of my automated robots.)

Jumping right to the solution, here's the complete source code for a JavaMail example class that demonstrates how to search for unseen (unread) email messages in a POP/POP3 mailbox:

Scala date/time FAQ: How do I get the current date and time in Scala?

The following code demonstrates how to get the current time in Scala, and then further shows how to get other information, such as the current minute, using the Java SimpleDateFormat class:

Scala FAQ: Can you share some examples of the Scala try/catch/finally syntax? Also, can you show how to catch multiple exceptions, and just one exception with the Scala wildcard operator (_)?

The general Scala try/catch/finally syntax looks like this:

Scala FAQ: Can you share some examples of the Scala if/then/else syntax? Also, can you show a function that returns a value from an if/then/else statement?

In its most basic use, the Scala if/then/else syntax is very similar to Java:

I just ran into a situation where I wanted to use AppleScript to read some file contents into a list/array, and came up with the following code:

set theFile to "/Users/al/Projects/Scala/Sarah/scripts/thank_you.data"
set fileHandle to open for access theFile
set thankYous to paragraphs of (read fileHandle)
close access fileHandle

I can confirm that this code works on my Mac OS X 10.6.8 system. The variable thankYous is a list/array that contains the lines from my file.

Scala JDBC FAQ: How can I use the Java JDBC API in my Scala application?

If you want to use a SQL database with your Scala applications, it's good to know you can still use the traditional Java JDBC programming library to access databases. I just ran a simple JDBC connection and SQL SELECT test, and everything seems to work just as it does in Java.

A nice feature of Scala is that in addition to letting you call and define functions just like you do in Java, you can also use both named arguments and default arguments. In this brief tutorial we'll look at both of these language features.

With Scala's named arguments (parameters), you can specify the names of function or method arguments when calling the method. Here's an example:

Scala FAQ: How do I use the Scala varargs syntax (or, What is the Scala varargs syntax)?

You can define a Scala function that accepts a varargs parameter like this:

def printAll(strings: String*) {
    strings.map(println)
}

The only magic in using the varargs syntax is adding the * symbol after the String declaration, as highlighted in this line of code in the function declaration:

Summary: This tutorial demonstrates how to use the Java SimpleDateFormat class to convert a Java Date to a formatted String.

[toc hidden:1]

Just a quick note here today that if you want to create a Twitter client in Scala, the Java Twitter4J library looks like a good path to take.

I've shown an example below, where you can see that besides the eight lines of code it takes to create a Scala twitter object, the actual code you need to get information from the Twitter developer API is pretty short.

I was reading a book named Calculus Made Easy, and they note that PI is the limit of the following series:

4/1 - 4/3 + 4/5 - 4/7 + 4/9 ...

Ruby PI program

It's been a while since I've done much math work, so to test this I wrote the following Ruby PI program.

If you've started using Scala, you've seen that there are some cool things in the Scala syntax compared to Java. For instance, in Java you'd execute a method on an object like this:

object.method();

But in Scala you definitely don't need the semi-colon:

object.method()

and then you can omit the parentheses:

While adding some AJAX autocomplete functionality to a new web application, I created a simple jQuery autocomplete example, using a PHP server-side script, and JSON to communicate the data from the server to the client. After creating that simple example, I thought I'd share all that code in a "jQuery AJAX autocomplete" tutorial here.

Before we get started, I'm going to assume you're familiar with basic HTML, CSS, and JavaScript programming fundamentals. It will also help if you've had a little exposure to jQuery, though that isn't entirely necessary.

Just a quick note here that I've just moved to Broomfield, Colorado, and I've brought my computer programming consulting services (Valley Programming) with me.

So, if you're looking for a Java computer programmer in the Boulder, Louisville, Lafayette, and Broomfield, Colorado areas, give me a call. All initial consultations are free, and if I know how to do something I'll let you know, and if I don't, I'll let you know that also.

Scala FAQ: What is the Scala ternary operator syntax?

In other programming languages there is a definite, unique ternary operator syntax, but in Scala, the ternary operator is just the normal Scala if/else syntax:

if (i == 1) x else y

The beauty of this is (a) it is just the normal if/else syntax, so you don't have to remember something else, and (b) it's easy to read.

In an earlier post I wrote about Mac freeware applications, and now, as I'm still recovering from having the solid state drive (SSD) fail in my MacBook Air (MBA), I thought I'd make a list of the Mac applications I just had to reinstall on my MacBook Air.

Here then, in no particular order, is the list of Mac software applications I just reinstalled:

Mac freeware FAQ: Can you provide a list of the best free Mac software?

While I'm loading up some freeware on a friend's new MacBook, it hit me how much really wonderful free Mac software is available these days. Of course there are free web browsers, which everyone wants, but there are also free Mac HTML editors, mail clients, and other free Mac apps for image editing, FTP, RSS, IRC, and CD/DVD burning and ripping, and much more. You can get a lot of things done these days using only free software.

I just found this great little “Code Bloat” script on Ward Cunningham's Smallest Federated Wiki website:

wc -l `find . | perl -ne 'next if /jquery/; print if /\.(rb|haml|coffee)$/'`

If you’re familiar with those Linux commands (wc, find) and Perl, you can tell that the intent of the command is to find the number of lines of source code per file, for all files beneath the current subdirectory.

While fooling around recently with various computer programming algorithms, I ended up writing an implementation of the Adler-32 checksum algorithm in Scala. There isn’t too much to say about it, other than I hope I got it right. My results for the simple test below matched the results shown on the Adler-32 Wikipedia page, so that’s encouraging. :)

Here's the Scala source code for my Adler-32 checksum implementation:

Linux shell script FAQ: How can I deal with spaces (blank spaces) in my input data when I'm writing a shell script for loop or while loop?

I was just working on a Linux shell script, and ran into the ages-old problem of handling data that has spaces (space characters) in it. I run into this any time I try to read a data file with blank spaces in it, or when I run into files and directories with spaces in their names. Whenever I try to work this data like this in a shell script for loop, the spaces always ruin what I'm trying to accomplish.