Posts in the “scala” category

Simple Scala string array examples

Scala array FAQ: How do I create a String array in Scala?

There are a few different ways to create String arrays in Scala. If you know all your array elements initially, you can create a Scala string array like this:

val fruits = Array("Apple", "Banana", "Orange")

If you don't know the strings that you want in your array initially, but know the size of your array, you can create it first, then populate it later, like this:

Class casting in Scala

While this may not be the recommended approach, here's how I currently handle class casting in Scala.

In Java you can cast a class like this:

Recognizer recognizer = (Recognizer)cm.lookup("recognizer").asInstanceOf;

As you can see, this is done with the "(Recognizer)" class cast operator.

In Scala you can't use the same syntax, but you can come close, like this:

A Scala JSON array parsing example using Lift-JSON

I just worked through some Scala Lift-JSON issues, and thought I'd share some source code here.

In particular, I'm trying to parse a JSON document into Scala objects, and I'm using Lift-JSON to do so. One of the things I'm doing here is to parse some of the JSON text into an array of objects, in this case an array of String objects.

First, here's the Scala source code for my example, and then a description will follow:

A Scala Robot class example (Java Robot class)

If you need an example of how to use the Java Robot class in a Scala program, here's a quick one. As unusual as it might seem, this example lets your Scala program log back into a Mac OS X computer after you've been logged out, typically by a screensaver.

First, here's the Scala source code to demonstrate the Robot class:

Scala and XPath - get the first element of an array

Scala/XPath FAQ: How do I get the first element of an array in an XML document using Scala and XPath?

I ran into the problem of needing to get the first array element from an XML document using Scala and XPath recently, and in short, I ended up writing some Scala/XPath code that looked like this:

A Scala JSON (Gson) HTTP POST RESTful client example

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:

Managing MongoDB connections with the Scala Casbah database driver

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.

There are no ++ or -- operators in Scala (use += or -=)

In Scala there are no ++ or -- operators. You should instead use the += and -= operators, as shown below. First the += operator:

scala> var i = 1
i: Int = 1

scala> i++
<console>:9: error: value ++ is not a member of Int
              i++
               ^

scala> i += 1

scala> println(i)
2

Next the -= operator:

Scala SBT documentation in PDF format

I did some manual labor this weekend, and converted the Scala SBT tool documentation into PDF format (a PDF file). I also tried to contact the SBT author, Mark Harrah, to see if he wanted the PDF on the Github website, but I couldn't find any way to email him through Github, so ...

If you want a copy of the Scala SBT documentation in PDF format, current as of June 3, 2012, here it is:

How to put SBT into offline mode with a command line setting

Scala SBT FAQ: How do I put SBT into offline mode so I can use it while working on an airplane, or any other location where I don't have a WiFi connection?

Use an SBT command like this to compile and run a project from your operating system command line:

$ sbt "set offline := true" run

I used this command a lot recently when I didn't have internet access, and I had to keep SBT from trying to reach out to the internet all the time.

Getting help on Play Framework console commands

It took me a few minutes to figure out that when you want help on Play Framework console commands (i.e., the Play command line tool), you may get more useful help by typing help play instead of just help. Using the first command shows information on commands like clean, compile, console, etc., which is what I'm more interested in.

To take a quick look at this, here's what you get when you type help play:

Scala - Merging two Arrays or ArrayBuffers

Scala Array FAQ: How do I merge two Arrays or ArrayBuffers?

Solution: Use the ++ method to join two arrays into one new array:

scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val b = Array(4,5,6)
b: Array[Int] = Array(4, 5, 6)

scala> val c = a ++ b
c: Array[Int] = Array(1, 2, 3, 4, 5, 6)

ArrayBuffer

Use the same approach to merge two ArrayBuffers into a new ArrayBuffer:

A short video introduction to the Scala REPL and variables

I didn't feel like going out in the rain and snow yesterday here in the Boulder, Colorado area, so I decided to stay indoors and create a video introduction to the Scala REPL and the Scala variable types, var and val. Without any further ado, here's the video: