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

As I've mentioned before, I recently wrote a series of Twitter scripts (such as this Scala Twitter client example), and the most recent version of these scripts sends me an email with the current Twitter trends. If you didn't know it, there can be a significant difference between various "Twitter trends", including these:

In 1993, I took a leap of faith, and began a career as a software consultant. I initially worked at a "full-service" computer company, left that company after three years, lived out a one-year noncompete agreement, then started my own business named "Mission Data" in 1996. I sold that company in 2007, started Alexander Consulting in 2008, and later created Valley Programming when I moved to Alaska. During these years my salary went from $30,000 to over $300,000 per year.

I'm in the process of publishing my second eBook for the Amazon Kindle, and one thing that's currently hard to find on the Amazon website is what the eBook cover height and width size specifications are. You can currently only find this information when you get ready to publish your book.

So, as a brief public service, as of May, 2012, here are the Amazon Kindle eBook cover height and width specifications, courtesy of the Amazon KDP book publication website:

Scala REPL FAQ: How do I add a Jar file to the Scala REPL classpath? (The Scala REPL is the interactive command line you get if you just type scala at your command line.)

To add a new jar file to the Scala REPL classpath (interactive command line classpath), use the :require command at the command line, like this:

scala> :require myjar.jar

I'm actually on vacation this week, but last night I showed a friend how to write software in Scala. He's familiar with recursion, so we jumped right into a simple factorial recursion example:

object FactorialRecursion {

    def main(args: Array[String]) {
        println(factorial(5))
    }

    def factorial(n: Int): Int = {
      if (n == 0) 
          return 1
      else
          return n * factorial(n-1)
    }
}

Another way to write that method is this:

The Scala List class filter method implicitly loops over the List you supply, tests each element of the List with the function you supply. Your function must return true or false, and filter returns the list elements where your function returns true.

(Note: Even though I use a List in these examples, the filter method can be used on any Scala sequence, including Array, ArrayBuffer, List, Vector, Seq, etc.)

[toc hidden:1]

Scala List FAQ: How do I add elements to a Scala List?

Solution

"How do I add elements to a Scala List” is actually a bit of a trick question, because you can't add elements to a Scala List; it's an immutable data structure. If you’ve ever used the Java String type, it’s just like that, you can’t mutate its elements.

That being said, in the following sections I’ll show what you can do.

Prepending elements to Scala Lists

The most common way to “add” elements to a Scala List is to create a new List from an existing List by prepending elements to the existing list. We do this all the time in functional programming in Scala, and the general approach looks like this in the Scala REPL:

As a Scala newbie, I'm curious about how the process of converting a Scala class back to Java source code works. What I really want to see is how my Scala source code is converted to Java source code. Besides plain old curiosity, I think that understanding more about how Scala works can also be very important to my understanding of Scala (such as the apply() method, and so on).

UPDATE: November 14, 2012. This article describes problems I had when attempting to use the Scala Casbah driver for the MongoDB database. In the end, it turned out I was doing something wrong. However, because this article shows how to troubleshoot MongoDB and Casbah database connection problems, it seems like a good idea to leave it here. If you want to see the correct way to use Casbah with MongoDB, I created this Github project, which shows the correct approach.

Here's the source code for a simple JOptionPane showMessageDialog example, where I use a JTextArea inside of a JScrollPane to show a long text message in the showMessageDialog:

[toc]

I’ve been working with the Java JOptionPane showMessageDialog a lot lately, so I thought I’d create a page here with a number of showMessageDialog examples, sort of a JOptionPane reference page.

I’ll walk you through some Java JOptionPane examples here, starting with a simple example and then increasing the level of difficulty as I go on.

To be clear, these examples of using Scala parallel collections aren't my own examples, they come from this page on the scala-lang.org website. But, for the completeness of my Scala cookbook recipes, I wanted to make sure I included a reference to parallel collections here.

Wow, I began by writing a few Scala programming tutorials just because I like the language, and as I look here a couple of months later I now have more than sixty tutorials. As a result, I thought I'd start organizing them here in the form a Scala Programming Cookbook.

Here's my current collection of Scala FAQs and recipes, in a "cookbook" format.

While working on a Scala project recently I created the following example Scala code to test a variety of things, including:

  • Scala case classes
  • The Apache HttpClient classes, including HttpPost
  • Creating JSON with Gson
  • Sending the JSON object/string to my POST RESTful server

Given that brief introduction, here's the source code for my Scala HTTP client, which uses POST (Apache HttpPost) to send data to a RESTful web service:

I don't get to parse too much JSON code with Java because the biggest JSON source I work with is Twitter, and I always use the Twitter4J project to interact with their web services. But a few days ago while working on an Android project, I just wanted to access their "Twitter Trends" REST service, and I used Java and the json.org Java library that comes with Android to parse the Twitter Trends JSON feed like this:

Android JSON tip: How to use the JSONObject toString(n) method to debug a JSON string in an Android app, by printing the string in a human-readable format.

While working on an Android project recently, I noticed there is a toString method on a JSONObject object that looks like this:

Scala constructors FAQ: How do I create a Scala class with multiple constructors (secondary constructors)?

The Scala approach to defining multiple class constructors is a little different than Java, but somewhat similar. Rather than try to explain this in words, I just created some example source code to demonstrate how this works.

Here's some source code to demonstrate the Scala "multiple constructors" approach:

Android FAQ: “Help, I'm getting an internet access error with my Android application that says, "java.net.UnknownHostException: Unable to resolve host (hostname): No address associated with hostname". I think my HTTP/REST code is correct, what's wrong?”

If you get an error message like this:

Scala FAQ: How do I call a method on a superclass in Scala?

I was just looking at this page of how to get Scala, Android, and Eclipse working together, and at the bottom of that page they share an example of how to call a method on a superclass in Scala:

Scala String formatting FAQ: How do I write code that is equivalent to the Java String.format class? That is, how do I format strings like that in Scala?

NOTE: As of Scala 2.10 this approach is a little out of date. You can still use this approach, but there's a better way to handle this situation in Scala 2.10 and newer Scala version.

In Java I always write code like this to format a String: