Short source code examples

This is a sample .gitignore file that I use for Scala SBT projects:

If you ever need to copy text (or a text file) from the MacOS Terminal to the Mac clipboard, I can confirm that the macOS pbcopy command works. It reads from STDIN and copies the text to the clipboard, so commands like these work:

$ echo "foo bar baz" | pbcopy

$ cat /etc/passwd | pbcopy

If you ever need to get the “cleaned” HTML as a String from the Java HTMLCleaner project, I hope this example will help:

Without any explanation, these are some of my working notes from my upcoming book on Scala and Functional Programming about a) for expressions, b) map, c) flatMap, d) Option, and e) nested flatMap and map calls.

These are equivalent (map and for)

this:

val y = x.map(_ * 2)

and this:

As a quick Scala note, the syntax to add multiple library dependencies with SBT is this:

libraryDependencies ++= Seq(
    "com.typesafe.akka" %% "akka-actor" % "2.1.1",
    "com.typesafe.akka" %% "akka-remote" % "2.1.1"
)

As a quick note, if you ever need to fill/populate a Scala list with the same element X number of times, one solution is to use the fill method that’s available to Scala sequences, like this:

scala> val x = List.fill(3)("foo")
x: List[String] = List(foo, foo, foo)

If you want to populate a list with different element values, another approach is to use the tabulate method:

I may explain this more in the future, but for now, here’s some source code for an example of how to use Quicklens in a Scala functional programming project.

Given some model/ADT definitions like this:

If you need to dump the contents of an array to the Android Log (Logcat) output, I can confirm that this approach works, at least with simple arrays of integers and strings that know how to print themselves:

Log.i("MyAndroidClass", Arrays.toString(arr));

If you’re trying to print more complicated arrays of custom objects you’ll probably need to implement good toString methods on those objects, and then this technique should work.

As a quick note, if you need some examples of the syntax of how to write a Java method that returns a generic type, I hope these are helpful:

As a quick note, here’s the source code for a Java “approximately equal” function that I use in an Android application:

If you want to hide the Android ActionBar on an Activity, it looks like there are at least two approaches.

First, add the android:theme="@android:style/Theme.NoTitleBar" entry to the activity’s definition in AndroidManifest.xml:

<activity
    android:name=".PlayAGameActivity"
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.NoTitleBar" >
</activity>

A second approach is to add this code in the Activity (or Fragment) onCreate method:

Here’s a little snippet of Android code that I want to remember:

Without much discussion, here’s an Android ListView/ListFragment with its Back/Up/Home button enabled:

An Android ListFragment/ListView with Back/Up button enabled

(That button used to be a Home button, but now it’s used for the Back/Up action.)

And here’s the source code for that ListView/ListFragment:

Here’s an example of how to populate an Android ListView, where I get the data for the ListView from a database Cursor:

DatabaseHelper.TeamsCursor tc = DatabaseManager.get(getActivity()).getAllTeams();
ArrayList<String> listOfTeamNames = new ArrayList<>();
for(tc.moveToFirst(); !tc.isAfterLast(); tc.moveToNext()) {
    listOfTeamNames.add(tc.getTeam().teamName);
}

// list the team names with an adapter that talks to our listview
TeamNamesAdapter adapter = new TeamNamesAdapter(listOfTeamNames);
setListAdapter(adapter);

There are more formal ways to create an adapter class to work with a Cursor, but for my needs I just needed to get a list of names from a SQLite database table and show them in a simple ListView — part of a ListFragment — and this was the simplest code to write.

FWIW, the example also shows one way to iterate over the elements in a Cursor using a Java for loop.

As a quick note, if you need to get an Android TableRow to align right, I can confirm that this layout code works:

As a quick note, this source code shows how to set the data (array) on an Android Spinner (also known as a dropdown list, or drop down list) from Java code:

Here’s an Android SQLite class I use in an Android app I wrote in 2014-2015. I’m sharing it here so I can easily find an Android SQLiteOpenHelper example:

I just learned an easy way to populate/initialize a Java int array with data, such as a range of numbers. The key is to use the rangeClosed method on the Java 8 IntStream class. Here’s an example using the Scala REPL:

scala> val n = java.util.stream.IntStream.rangeClosed(0, 10).toArray()
n: Array[Int] = Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

I show that in Scala to show the output, and here’s what it looks like with Java:

As a quick note, this is the syntax for creating a Scala ArrayBuffer:

import scala.collection.mutable.ArrayBuffer

val fruits = ArrayBuffer[String]()
val ints = ArrayBuffer[Int]()

The key thing to know is that the keyword new is not required before the ArrayBuffer. (This is because ArrayBuffer is either defined as a case class, or because it has an apply method defined. I haven’t looked at its source code to know which approach is taken.)