Posts in the “scala” category

Scala-Style Logging with Grizzled-SLF4J

Problem: You want to add logging to an application in a more Scala-specific way than simply using SLF4J.

Solution

Grizzled-SLF4J is a thin wrapper around SLF4J that gives you logging in a more Scala-like way.

To get started with Grizzled-SLF4J, create a simple SBT project, then edit your build.sbt file so it has the dependencies you’ll need:

Thinking in Scala: The 'map' method

Thinking in Scala: The map method on the Scala collections classes is used to transform an input collection to an output collection using a transformation algorithm you supply. This is similar to the way an electricity transformer converts voltage from one purpose to another. The following examples come from the Scala Cookbook:

Scala: Saving/writing XML to a file

Problem: You want to write XML data to a file in a Scala application, such as saving application data or configuration information to a file.

Solution

Use the scala.xml.XML.save method to write a Scala literal to a file. Given this XML literal:

Scala: How to create XML literals

Problem: You want to create XML variables, and embed XML into your Scala code.

Solution

You can assign XML expressions directly to variables, as shown in these examples:

val hello = <p>Hello, world</p>
val p = <person><name>Edward</name><age>42</age></person>

In the REPL you can see that these variables are of type scala.xml.Elem:

How to mix Scala and XML to dynamically generate XML

Problem: You want to dynamically generate XML from your Scala source code, such as creating output for a SOAP web service.

Solution

A great feature of Scala’s XML support is that you can interweave XML and regular Scala source code together. This lets you dynamically generate XML from your Scala code.

To create XML with dynamic, embedded data, just put your Scala code in curly braces inside the XML tags, as shown in the following example:

Basic Scala XPath searching with \ and \\

Problem: When writing a Scala application, you want to search an XML tree for the data you need using XPath expressions.

Solution

Use the \ and \\ methods, which are analogous to the XPath / and // expressions. The \ method returns all matching elements directly under the current node, and \\ returns all matching elements from all nodes under the current node (all descendant nodes).

To demonstrate this difference, create this XML literal:

The Play Framework, MySQL, currency, decimal fields, and BigDecimal types

I’m currently working on a Play Framework server-side application that handles money/currency. (The application UI uses Sencha ExtJS, but that doesn’t matter for this example.)

From past experience I know that this means I need to use a decimal field in my MySQL database table. (MySQL also lets you declare this field type as numeric.) I further know that the MySQL JDBC driver uses a java.math.BigDecimal field to insert and select from this field type.

How to run Play Framework model methods from the Play console

Here’s a quick look at how to run Play Framework “model” methods from the Play console. In my case, my model methods are Anorm database access methods, but you may be accessing MongoDB, Cassandra, CouchDB, whatever.

First, move to your Play application directory and start the Play interpreter:

$ play

Then start the Play console:

[MyApp] $ console

Your prompt will now look like this:

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:

Wanted: Scala Cookbook reviewers

UPDATE: I originally posted this article in January, 2013, and it's now mid-February, 2013, and we're no longer looking for reviewers. I've only kept this page here so people won't get 404 errors.

OLD CONTENT:

Interested in being a reviewer for the Scala Cookbook?