scala

Tutorials about the Scala programming language.

Scala currency and money libraries (JVM-based libraries)

I've been trying to find some good Scala currency and money libraries lately, and I just ran across the following list of projects on the Joda Money Github project. That URL contains the following list of Java and JVM-based projects that should all be usable in Scala:

Scala type annotations (and ascription)

Summary: A discussion of Scala type annotations and type ascription.

While Scala normally determines the type you want to use automatically, such as this Int:

scala> val x = 1
x: Int = 1

there may be times you want to control the type, such as if you want a Byte, Short, Long, Double, Float, etc. In these cases you can annotate your type when you create it, like this:

Scala money and currency - The BigDecimal class and libraries

Note: I don't have any immediate solutions in this article; it's more of a discussion of where I'm at today when looking at handling money/currency in Scala.

As a quick note, I've started to look at handling money/currency in Scala, and I'm also starting to explore a couple of money/currency libraries.

A Scala 2.10 implicit class example

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

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

How to set the Scala version in the SBT build.sbt configuration file

SBT FAQ: How do I set the desired Scala compiler version in the SBT build.sbt configuration file?

Set the value of the scalaVersion variable in your SBT build.sbt file. For instance, to use Scala 2.9.2, put an entry like this in the build.sbt file:

scalaVersion := "2.9.2"

To use Scala 2.10.0, put an entry like this in the build.sbt file:

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:

A collection of Scala flatMap examples

Scala flatMap FAQ: Can you share some Scala flatMap examples?

Sure. When I was first trying to learn Scala, and cram the collections' flatMap method into my brain, I scoured books and the internet for great flatMap examples. Once I had a little grasp of it I started creating my own examples, and tried to keep them simple.

Using flatMap on a list of String

The following examples show the differences between map and flatMap on a sequence of String:

Convert a Scala array to string with mkString

Scala collections FAQ: How can I convert a Scala array to a String?

A simple way to convert a Scala array to a String is with the mkString method of the Array class. (Although I've written "array", the same technique also works with any Scala sequence, including Array, List, Seq, ArrayBuffer, Vector, and other sequence types.)

Here's a quick array to string example using the Scala REPL:

String interpolation in Scala 2.10 (embedding variables in strings)

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

There are a lot of nice new features in Scala 2.10, but a simple feature that makes me happy is string interpolation. In short, Scala developers can now 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.)

What's new in Scala 2.10

Just a quick note that Scala 2.10.0 was released recently, and the following page, Scala 2.10.0 now available, contains a nice summary of what's new in the official Scala 2.10 release. The short list of new features includes:

Syndicate content