Scala, Java, Unix, MacOS tutorials (page 292)

Problem: You want to convert the data in a Scala class to its equivalent XML, or convert an XML representation of a class to an instance of the class.

Solution

There are two primary approaches to solving this problem:

Problem: You want to use match expressions as another way to access the information contained in XML data when writing your Scala applications.

Solution

Given this XML literal:

Problem: Your XML data has an array of elements, and you need to extract the first element, second element, or more generally, the Nth element, using Scala.

Solution

The following simplified version of the XML from the Yahoo Weather API has three <forecast> elements:

Problem: You need to perform deep XML searches, combining the \ and \\ methods, and possibly searching directly for tag attributes.

Solution

Combine the \\ and \ methods as needed to search the XML. When you need to extract tag attributes, place an @ character before the attribute name.

Given this simplified version of the Yahoo Weather RSS Feed:

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:

Problem: In a Scala application, you want to extract information from XML you receive, so you can use the data in your application.

Solution

Use the methods of the Scala Elem and NodeSeq classes to extract the data. The most commonly used methods of the Elem class are shown here:

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:

This comic seems appropriate today.

Some interesting SBT build.sbt options, from this link.

I love this comment from Martin Odersky, from the image shown, which comes from this link:

“Initially, things were too fluid to invest in tests. I simply did not know whether the design would hold up, had to fit a lot of pieces together first. But now is a good time to put these tests in place.”

So often people talk about “Test First Development” that I think they’re insane, or just regurgitating marketing-speak to sound good. There are times when you’re coding things like SARAH where you don’t know how things are going to shake out that “Test First” just doesn’t make sense. If you know what you’re getting into when you’re coding, Test First sounds good, but when you’re exploring, “Oh, this is how SARAH should work”, or, “I thought an FTP server worked like this but it works like that”, Test First doesn’t make sense. (Once you understand the domain, giddyup, test your heart out.)

Once or twice a year I pull out my “Investing” folder and review the notes that I’ve made over the years, reviewing terms like P/E, P/S, EPS, and so on. As I was doing some research today I came across one book titled, Stocks For The Long Run, which looks like a very good book. The image shown here is from a review of the book on Amazon.com, and I found this review very helpful by itself.

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:

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:

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:

As I’ve written before, when I finished writing the Scala Cookbook it ended up being about 140 pages longer than my editor wanted, and I had to cut some content from the book. Unfortunately the chapter on “Logging & Testing” was one of the victims of the cut, but I’m glad to say that I’ve finally taken the time to convert that material to HTML. As a result, here are links to the 12 ScalaTest tutorials in that chapter:

I probably spend about 10 hours a year looking at data related to website visitors, and today was one of those days where I gave it 15 minutes of time. Here’s a quick look at the data.

This first image shows what browsers the visitors are using:

I write mostly about Open Source and Macs, so if IE is a little lower than usual, it may be because of that.

This image shows the number of people using desktop, mobile, and tablet clients:

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: