formatting

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.

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:

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:

Java String formatting with the String format method (like sprintf)

Java String formatting FAQ: How can I format Java String output?

For a long time I appended Java strings together using the + operator to combine them into the output format I wanted, but I always knew that approach was pretty ugly. Fortunately at some point I learned how to use the Java String format method to format my text, and as you’ll see, the resulting code is much cleaner this way.

A Log4J format example

Log4J formatting FAQ: Can you share a Log4J output/logging format example?

Sure. I'll share a Java Log4J format example that I'm pretty happy with.

I've used the following Log4J logging format quite a bit lately, as I've been working on a headless Java app that can be deployed on thousands of computers, and I was looking for a good Log4J format that was easily readable by humans, and also easy to parse by computers. Here's what my Log4J output format look like these days: