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

After looking at my website (alvinalexander.com) yesterday on my iPad, I thought maybe I should create a custom version of the website for mobile devices. However, after looking at the stats, it appears that there's a 10:1 ratio of stationary devices to mobile devices, which you can see in this graphic, courtesy of Google Analytics:

Akka actors FAQ: Can you share an "Akka Actors 101" example (a simple "Introduction to Akka Actors" example)?

Sure. If you're looking for a really simple tutorial, check out my Akka Actors "Hello, world" tutorial. If that one is overly simplified and you want something more, continue on here.

I just ran across this scala-lang.org post about using tuples in an anonymous function, and thought it was good enough to reproduce here, with only the solution part.

In essence, the question is, if I have a Map like this, which is really composed of tuples:

A lot of people want to sell Scala as a functional programming (FP) language. That's fine, it certainly is, and FP has many benefits. But when I first started working with Scala I was just looking for a "better Java".

Personally, I grew tired of Java's verbosity. Ruby showed us a different way, and I loved it, but ... after a while I felt like many of my unit tests in Ruby wouldn't be necessary if the language had static types. I wished for a concise Ruby-like language with static types.

[toc hidden:1]

I like how the Anchorage Daily News puts a photo of the front cover of their newspaper on Twitter most days, so I thought I'd try something like that, posting a photo of my Facebook timeline from time to time. I don't know if I'll keep doing this, but I'll do it at least once. Here's what my timeline looks like on March 3, 2013:

My Facebook timeline, March 3, 2013

I just fired up my old 2006 MacBook Pro that runs Mac OS X 10.5.8, and it helped me realize why I don't like the Spaces feature in the newest versions of the Mac operating system: It used to be fun.

I just went through my notes on the book, Clean Code, and thought I'd share what I thought were some of the best quotes and “lessons learned” from reading that book.

Normally I just write about solutions, but I thought I'd take a moment today to write about something else. In this case I just wanted to note that it's possible to create an immutable List of mutable data in Scala. This scenario made me wonder, "What does 'immutable' mean?" Let's take a look.

As a first example, we'll create a Person class that has two fields, and the first field (firstName) can change:

Linux find/copy FAQ: How can I use the find command to find many files and copy them all to a directory?

Scala Set FAQ: How do I create a mutable Set in Scala?

To create a mutable set in Scala, first determine the type of set you want. You do this because the mutable Set is actually a trait, and you need to determine which concrete implementation you want.

For instance, if you want a SortedSet of String, define it like this:

val names = scala.collection.mutable.SortedSet[String]()

Then you can add elements to the set later like this:

Scala FAQ: How do I find the unique items in a List, Array, Vector, or other Scala sequence?

Solution: Use the distinct method.

Here's a simple example using a List of integers:

scala> val x = List(1,1,1,2,2,3,3)
x: List[Int] = List(1, 1, 1, 2, 2, 3, 3)

scala> x.distinct
res0: List[Int] = List(1, 2, 3)

As you can see, res0 now contains only the unique elements in the list.

Very cool, Jonathan Ive of Apple discusses a television show named Blue Peter, and the process of design, while looking at designs from young children:

 

Scala FAQ: When you create a case class in Scala, a copy method is generated for your case class. What does this copy method do?

In short, it lets you make a copy of an object, where a “copy” is different than a clone, because with a copy you can change fields as desired during the copying process. The copy method is important in functional programming, where values (val) are immutable.

In David Pollak's book, Beginning Scala, he introduces a using control structure that is helpful in several ways. First, it can be used to automatically close any resource that has a close method. Second, it's a great example of how to create a custom control structure in Scala.

Introduction

Here's the source code for the using control structure he created:

[toc hidden:1]

Scala match/case FAQ: Why am I getting an "unreachable code due to variable pattern" warning on my match/case statement when compiling my Scala code?

The usual reason for getting this error message is that you try to use an existing variable name inside a Scala match expression. For instance, if you try to create a match expression like this:

[toc hidden:1]

I've been keeping notes about the process of writing the Scala Cookbook for O'Reilly, and I thought I'd share a few of those thoughts here today (early February, 2013).

Scala FAQ: Can you share some examples of how to implement break and continue functionality in Scala?

Sure. The example Scala code below shows both a break and a continue example. As you can see from the import statement, it uses the code in the Scala util.control.Breaks package.

To understand how this works, let's first look at the code, and then the output. First, here's the code:

[toc hidden:1]

Scala FAQ: Can you share some examples of the Scala for loop syntax?

Sure. I'm going to start with a comparison to Java for loops, because that's what I was just thinking about.

In Java you might write a for loop with a counter like this:

for (int i = 0; i < 10; i++) {
  System.out.println(i);
}

The equivalent for loop in Scala looks like this:

for (i <- 1 to 10) {
  println(i)
}

(The use of parentheses isn’t necessary in either example, but most for loops will be longer.)

[toc hidden:1]