Posts in the “scala” category

A Scala Spring Framework dependency injection example

Curious about how well Scala would play with the Spring Framework, I created a small Scala/Spring dependency injection example project, which I'm sharing here.

The short answer is that Scala worked just fine with Spring, but it also showed me that I still have plenty to learn about inheritance in Scala.

My Spring applicationContext.xml file

I copied a Spring applicationContext.xml file from another project, then whittled it down to these bare essentials:

[toc hidden:1]

Interval halving (Bisection) method in Scala (OOP and FP styles)

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]

Scala List/Array/Vector/Seq class filter method examples

The Scala List class filter method implicitly loops over the List you supply, tests each element of the List with the function you supply. Your function must return true or false, and filter returns the list elements where your function returns true.

(Note: Even though I use a List in these examples, the filter method can be used on any Scala sequence, including Array, ArrayBuffer, List, Vector, Seq, etc.)

[toc hidden:1]

Making Twitter web service calls concurrently with Akka Futures

While I was working on improving how Sarah gets information from Twitter and other sources, I decided to take some of that code and hack together this example. It shows how to make three Twitter web service requests concurrently -- and then wait for ther results before moving on -- with Akka Futures.

Before sharing the entire class, the cool Akka Futures stuff is in these lines of code:

String interpolation in Scala 2 (String variable substitution)

Scala string FAQ: How do I embed variables in strings in Scala?

A lot of nice new features were added in Scala in version 2.10, but a simple feature that makes me happy is string interpolation. This lets Scala developers embed variables inside strings just like you can do in other languages like Perl, PHP, and Ruby -- but perhaps in an even more powerful way. (I say "perhaps" because I haven't used those other languages recently.)

[toc hidden:1]

The Scala for-loop translation scheme

If you're interested in the details of the translation scheme of a Scala for loop (for comprehension), here's a quick look at how a for loop is translated into, well, other code.

A simple Scala for loop

In a first example, we'll start with the following Scala class:

class Main {
  def foo { for(i <- 0 to 10) println(i) }
}

Next, I compile this class from the command line like this:

[toc hidden:1]

A Scala 2.10 (and newer) implicit class example (how to add new functionality to closed classes)

Scala FAQ: Can you share an example of how to create an implicit class in Scala 2.10?

Sure. As the question implies, the implicit class functionality changed in Scala 2.10, so let's take a look at the new syntax.

Background

Rather than create a separate library of String utility methods, like a StringUtilities class, you want to add your own behavior(s) to the String class, so you can write code like this:

[toc hidden:1]

Scala ‘break’ and ‘continue’ examples

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 for loop syntax examples (including yield and guards)

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]

Scala 'unreachable code due to variable pattern' warning message

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]

Using Scala as a “better Java”

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]

Play Framework Recipes booklet (from the Scala Cookbook)

Update, August 12, 2013: The second version of Play Framework Recipes is now available as a free PDF. It contains new appendices on Play commands, a JSON reference, improved formatting, and over 80 pages of Play Framework content. Get it from the Download section below.

An Akka actors ‘remote’ example

While doing some crazy things with SARAH, I realized that the best way to solve a particular problem was to use remote Akka actors. I haven’t had the opportunity to work with Akka much since finishing the Scala Cookbook, so I dug around trying to find a simple Akka remote “Hello, world” example. Unable to find a good one, I read some stuff, and created it myself.

[toc hidden:1]

FP experts: It would help Java/OOP devs to see a complete app

Summary: Most Java/OOP developers I know are interested in FP. They want to make their code more reliable, testing easier, and want to gain performance advantages of multiple cores. The problem they have is that all the FP literature is about “programming in the small”, and what they need right now is a good example of a complete FP system (programming in the large), one that includes a UI, database, web services, etc. In this article I explore that problem from the perspective of Java/OOP developers, and offer the 80/20 rule I follow today for building systems with FP in mind.

Composition

I ran across some great thoughts from Erkki Lindpere on zeroturnaround.com yesterday. First this:

“I think the real composability and reusability in object-oriented code doesn’t come from object-oriented design at all: it comes from abstraction and encapsulation.”

That’s good, but the highlight below shows my favorite thought:

[toc hidden:1]

A collection of ScalaTest BDD examples using FunSpec

This page is a work in progress, but it currently shows a small collection of ScalaTest BDD examples. I’ll keep adding more BDD examples as time goes on. Also, in this article I assume that you are familiar/comfortable with Scala and SBT, and are at least slightly familiar with using ScalaTest.

Analyzing Apache access logs with Spark and Scala (a tutorial)

I want to analyze some Apache access log files for this website, and since those log files contain hundreds of millions (billions?) of lines, I thought I’d roll up my sleeves and dig into Apache Spark to see how it works, and how well it works. I used Hadoop several years ago, and as a quick summary, I found the transition to be easy. Here are my notes.

[toc hidden:1]