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

Without much explanation today, here are different ways to create a sorted set from a Set in Scala:

val set = Set(1, 6, 2, 12, 7, 3, 11)
val ss = collection.immutable.SortedSet[Int]() ++ set
val ss = collection.immutable.TreeSet[Int]() ++ set
val ss = collection.mutable.SortedSet(set.toList: _*)

Here’s what this looks like in the REPL:

scala> val set = Set(1, 6, 2, 12, 7, 3, 11)
set: scala.collection.immutable.Set[Int] = Set(1, 6, 2, 12, 7, 3, 11)

scala> val ss = collection.immutable.SortedSet[Int]() ++ set
ss: scala.collection.immutable.SortedSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12)

scala> val ss = collection.immutable.TreeSet[Int]() ++ set
ss: scala.collection.immutable.TreeSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12)

scala> val ss = collection.mutable.SortedSet(set.toList: _*)
ss: scala.collection.mutable.SortedSet[Int] = TreeSet(1, 2, 3, 6, 7, 11, 12)

I haven’t done any performance comparisons on these approaches, I just thought I’d share them here today.

Note that if you want to convert a Scala Set to a sorted sequence, you can also use this approach:

val sortedSeq = set.toSeq.sorted

From this terrific article on parallelism vs concurrency.

From the original article by Joe Armstrong:

Concurrent = two queues and one coffee machine.
Parallel = Two queues and two coffee machines.

I saw this page in a Yoga Journal magazine several years ago. At that time there was a teacher teaching Iyengar yoga in Talkeetna, Alaska. I'm not sure if she still teaches there.

I thought I’d have a little fun with Scala today, and show different ways to declare multiple variables on one line. These aren’t the most useful or common things to do, but they’re interesting, and I know I learned at least one thing while looking into this.

You can define multiple fields on one line, separated by commas, as long as they are all the same type and have the same value:

Just a quick note here today that if you need some example MySQL database tables, you can use these. I created them for some experiments I ran last night. They show the MySQL create table, primary key, and foreign key syntax:

Out of curiosity about Scala’s file-reading performance, I decided to write a “line count” program in Scala. One obvious approach was to count the newline characters in the file:

I’ve known about using Scala’s zipWithIndex method for quite some time. I used it in for loops to replace counters, and it works like this:

scala> List("a", "b", "c").zipWithIndex
res0: List[(String, Int)] = List((a,0), (b,1), (c,2))

I learned about using zip with Stream last night while reading Joshua Suereth’s book, Scala In Depth. It works like this:

When you write Scala code like this:

val x = List.range(1, 10)
val evens = x.filter((i: Int) => i % 2 == 0)

the following portion of the code is a function literal (also known as an anonymous function):

(i: Int) => i % 2 == 0

When reading this code, it helps to think of this symbol:

If you ever want to create a Unix shell script that you can give to someone else so they can double-click it and run it through the Mac OS X Finder, all you have to do is (a) name the file with the ".command" extension and (b) make it executable. So, just name your Mac/Unix script like this:

ShowProcesses.command

Then make it executable, like this:

chmod +x ShowProcesses.command

You can also leave out the usual #!/bin/sh part on the first line.