scala

Scala - How to declare multiple variables on one line

I thought I’d have a little fun 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:

Scala file reading performance - Line counting algorithms

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:

// took 101 secs (10M lines)
// work on one character at a time
def countLines1(source: Source): Long = {
  var newlineCount = 0L
  for {
    c <- source
    if c.toByte == NEWLINE
  } newlineCount += 1
  newlineCount
}

As the comment shows, this took 101 seconds to read a file that has 10M lines. (An Apache access log file for this website.)

Scala zip and zipWithIndex examples (with Stream)

I’ve known about using the Scala zipWithIndex method for quite some time. I’ve 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:

Scala - Thinking of functions as transformers

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:

A Scala XML EntityRef example (unicode character)

This page shows a good Scala XML EntityRef example:

import scala.xml._
val xml = <body>Hello{EntityRef("#8198")}World</body>

That page states that 8198 is the unicode value for a tiny space character.

That code isn't too helpful unless you can see it in the REPL, so here it is:

Scala REPL - java.lang.OutOfMemoryError: Java heap space error

I just got a “java.lang.OutOfMemoryError: Java heap space error” when trying to use the Scala REPL to analyze a large XML dataset:

A Scala Null Object example

A Null Object is an object that extends a base type with a null or neutral behavior. Here’s the Scala version of the Java example Wikipedia uses to demonstrate this:

trait Animal {
  def makeSound()
}

class Dog extends Animal {
  def makeSound() { println("woof") }
}

class NullAnimal extends Animal {
  def makeSound() {}
}

As you can imagine, later in your application you might have some code like this:

Is Scala DICEE?

If you’ve never heard of the term DICEE, it was coined by Guy Kawasaki. Mr. Kawasaki was a developer evangelist for the original Macintosh team in the 1980s, and used the term in at least one subsequent book to refer to great products.

“DICEE” is an acronym that stands for Deep, Indulgent, Complete, Elegant, and Emotive:

Scala version of Collective Intelligence Euclidean distance algorithm

While reading the excellent book, Programming Collective Intelligence recently, I decided to code up the first algorithm in the book using Scala instead of Python (which the book uses).

Scala Option, Some, None syntax examples

Today I’m sharing some examples of the Scala Option/Some/None syntax. These examples will show how to use an Option for the var fields in a Scala class. Then I’ll show how to set those Option fields, and then get the values from the Option fields.

To get started, we’ll need a little case class to represent an Address:

Syndicate content