formatting

An example of Scala’s `f` string interpolator

With Scala it’s common to embed variables in strings like this with the s string interpolator:

val name = "Fred"
println(s"My name is $name.")

That’s cool, but when you need to format your string, Scala gives you an even more powerful tool: the f string interpolator. Here’s an example of how I just did this in my LittleLogger logging library:

Scala number, date, and formatting examples

This short blog post contains a collection of Scala number and date examples. I created most of these in the process of writing the Scala Cookbook. Unlike the Cookbook, I don’t describe the examples here much at all, I just show the examples, mostly as a reference for myself (and anyone else that can benefit from them).

Scala numeric types

Scala has these numeric types:

Scaladoc syntax examples (Scaladoc tags, wiki markup)

Table of Contents1 - An example of Scaladoc tags and wiki formatting2 - Sample Scaladoc output3 - Common tags4 - Scaladoc “wiki” character formatting tags5 - Scaladoc “wiki” paragraph formatting tags6 - Generating Scaladoc documentation with SBT7 - See Also8 - The Scala Cookbook

In this article I share some examples of Scala’s Scaladoc syntax, including common Scaladoc tags, and the wiki-style of markup that Scaladoc supports.

 

(this space left open to make room for the table of contents over there -->)

 

Back to top

An example of Scaladoc tags and wiki formatting

You can mark up your source code using Scaladoc tags as well as a wiki-like syntax. The following code shows many of the Scaladoc tags and a few of the wiki-style markup tags:

Using sed to add a newline on Mac OS X

As a quick note today, I have been converting parts of the Scala Cookbook from a plain text format to a Markdown format, and as part of that I needed to add some newline characters to add spacing to the document. This wouldn’t be bad if it was a few pages, but it’s hundreds of pages, so I decided to use the Unix sed command to do the work.

Scala: Displaying XML in a human-readable format (pretty printing)

Problem: You have some XML in a hard-to-read format in a Scala application, and want to print it in a format that’s easier to read, at least for humans.

Solution

Use the scala.xml.PrettyPrinter class. To see how it works, imagine starting with a long, continuous string of XML:

Drupal 6 - The CKEditor is removing/deleting CODE tags

I had a problem using the CKEditor with Drupal 6 where the CKEditor would not display <code> tags properly in the editor, and would then delete trailing spaces after the <code> tag. After some digging around, I finally found that I needed to comment out the following line in the ckeditor.config.js of my CKEditor module installation:

The Scala String format approach (and Java String.format)

Scala String formatting FAQ: How do I write code that is equivalent to the Java String.format class? That is, how do I format strings like that in Scala?

NOTE: As of Scala 2.10 this approach is a little out of date. You can still use this approach, but there's a better way to handle this situation in Scala 2.10 and newer Scala version.

In Java I always write code like this to format a String: