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

If you ever need to generate a list of subdirectories in a directory in Scala, here's one way to do it:

def getListOfSubDirectories(directoryName: String): Array[String] = {
  return (new File(directoryName)).listFiles.filter(_.isDirectory).map(_.getName)
}

I intentionally wrote that function in a short, "Scala like" style, but you can expand it to multiple lines, if you prefer.

Git “remove” FAQ: How do I tell Git not to track a file (or files) any more? (i.e., I want to remove the file from the Git repo.)

While working on an application named Sarah yesterday, I accidentally checked some files into Git that I didn’t mean to. These were primarily binary files in my project’s bin and target directories.

Because I didn’t want these files in my Git repository, I needed a way to remove them from the repository but still keep them on disk. Fortunately there’s a Git command for just this situation:

If you're looking for a simple Scala IMAP client tutorial/example, please follow that link, but if you're interested in some Scala source code that demonstrates IMAP searching with Scala, JavaMail, SearchTerm, AndTerm, FromStringTerm, SentDateTerm, FlagTerm, and FetchProfile, this source code may be for you.

In short, here's what the following Scala IMAP client does:

An functional programming idiom, and therefore a Scala idiom, is that functions and methods should not have side effects. As written in the book Programming in Scala:

A method's only act should be to compute and return a variable.

If you ever need to write an IMAP client in Scala, I hope the following example can be helpful. For my project named SARAH (a Mac OS X Siri-like project), I wanted an IMAP client to periodically check my mail and then say something a little better than, "You've got mail", and the first part of that was getting a Scala IMAP client going.

Java math FAQ: How do I square a number in Java?

Solution

You can square a number in Java in at least two different ways:

  1. Multiply the number by itself
  2. Call the Math.pow function

I just picked up an old college textbook named Applied Numerical Analysis, and curious to see what the Interval Halving method (also known as the Bisection Method) would look like in Scala, I decided to code it up. Considering that Scala is similar to the Java programming language, if anyone else needs the Interval-Halving method in Java, this code can easily be adapted to Java as well.

[toc hidden:1]

Note: The following examples of passing a function as an argument to another function have all been taken from the PDFs on the Scala website. The only thing I've done here is to add comments to the source code, and add detailed discussions of them in this article.

Scala FAQ: What are the Scala numeric data types? How many bits do they use to store their data, and what is the range of those data types?

Courtesy of the excellent book, Programming in Scala, here is a list and description of the Scala data types, including bit sizes and data ranges:

Data Type  Definition

Boolean    true or false

Byte       8-bit signed two's complement integer (-2^7 to 2^7-1, inclusive)
           -128 to 127

Short      16-bit signed two's complement integer (-2^15 to 2^15-1, inclusive)
           -32,768 to 32,767

Int        32-bit two's complement integer (-2^31 to 2^31-1, inclusive)
           -2,147,483,648 to 2,147,483,647

Long       64-bit two's complement integer (-2^63 to 2^63-1, inclusive)
           -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807

Float      32-bit IEEE 754 single-precision float
           1.40129846432481707e-45 to 3.40282346638528860e+38 (positive or negative)

Double     64-bit IEEE 754 double-precision float
           4.94065645841246544e-324 to 1.79769313486231570e+308 (positive or negative)

Char       16-bit unsigned Unicode character (0 to 2^16-1, inclusive)
           0 to 65,535

String     a sequence of Chars

Just a quick note today that if you want to create a mutable Scala array -- particularly an array that can grow in size after you first declare it -- you need to use the Scala ArrayBuffer class instead of the Array class, which can't grow.

Here's a short example of how to instantiate an ArrayBuffer object, then add elements to the array:

Just a quick note today on how to access the Twitter API using PHP, specifically using the Abraham Williams PHP TwitterOAuth library. While the library itself seems very good, I couldn't find much in the way of documentation, particularly a simple "getting started" tutorial, so I thought I'd share this code.

In short, I dug through the PHP files in the root directory of the TwitterOAuth library, eventually creating this simple PHP Twitter client example:

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:

In earlier articles I've described how to execute system commands from Java applications. A long time ago I wrote my first article on this topic (How to execute system commands from Java), and more recently I wrote an updated version of that article titled "Executing system commands from Java using the ProcessBuilder and Process classes".

I was just reading the book, Hadoop in Action, and came across a nice, simple way to use the Java StringTokenizer class to break a sentence (String) into words, taking into account many standard punctuation marks. Before looking at their solution, first take a look at the code they used to break a String into words using whitespace (a blank):

Java/Mac FAQ: Where is JAVA_HOME located on Mac OS X systems?

JDK location

This has changed over time, but if you're using Mac OS X 10.9 or newer, your JDK/SDK  JAVA_HOME location for Java 8 will be something like this:

/Library/Java/JavaVirtualMachines/jdk1.8.0_25.jdk/Contents/Home

For Java 7 it was also in the same area:

/Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home/

Of course that will vary by the JDK version you have installed.

Scala command line FAQ: How do I read command line arguments (args) in a Scala shell script?

If your Scala shell script is very short, and you're not using an object or class declaration -- i.e., you have no main method -- you can access the script's command line arguments through the default args array, which is made available to you by Scala.

For instance, you can create a one-line Scala script named hello.scala like this:

I've been working with Scala quite a bit lately, and in an effort to get it all to stick in my brain, I've created a Scala cheat sheet in PDF format, which you can download below.

This PDF is very different from my earlier Scala cheat sheet in HTML format, as I tried to create something that works much better in a print format. (I first tried to get it all in one page, but short of using a one-point font, that wasn't going to happen.)

Scala file FAQ: How do I open and read text files in Scala?

When you’re writing Scala scripts, you often want to read text files. Fortunately it’s pretty easy to open and read from text files in Scala. You can just use an approach like this:

import scala.io.Source

val filename = "fileopen.scala"
for (line <- Source.fromFile(filename).getLines()) {
    println(line)
}

I just got the following error message when trying to use the current Yahoo OAuth authentication system to connect to Yahoo Mail:

MySQL database FAQ: How do I backup (back up) a MySQL database?

[toc hidden:1]