convert

A look at how the Scala `lazy val` syntax gets converted into Java code (bytecode)

Table of Contents1 - A little `lazy val` conversion example2 - A second `lazy val` conversion example3 - One more `lazy val` conversion example4 - The end

I don’t have any major conclusions to share in this blog post, but ... what I was curious about is how Scala implements lazy val fields. That is, when the Scala code I write is translated into a .class file and bytecode that a JVM can understand, what does that resulting code look like?

Two more Textmate commands (capitalize, CSV to list)

As a “note to self,” I wrote two more Textmate commands yesterday, one to capitalize each word in a selection of words, and another to convert a CSV list of words to a simple list. Here’s the source code for the Capitalize command:

#!/bin/sh

perl -ne 'print ucfirst $_'

The $_ portion of that Perl command isn’t required, but I include it as a reminder to myself about how Textmate commands and snippets work.

Here’s the source code for my Textmate command that uses the Unix tr command to convert a CSV list of words (such as a paragraph of comma-separated words) into a simple list of words:

#!/bin/sh

tr , "\n"

As you can see, those commands are fairly simple. If you know Unix/Linux and then know a little about how to write Textmate commands, you can usually get it to do what you want. I like that you can use any Mac/Unix programming language or tool to solve the problem at hand.

Getting started converting documents with Pandoc

I’m looking into producing my Scala/FP book as a PDF, and as part of that I have been looking into Pandoc. With the exception of converting HTML tables into other formats such as Markdown or LaTeX, Pandoc has been working well so far.

Here are a couple of Pandoc commands to show you how easy this is:

# create a pdf from a markdown doc
pandoc test1.md -s -o test1.pdf

# create an html doc from a markdown doc, long form
pandoc test1.md -f markdown -t html -s -o test1.html

# convert markdown to latex
pandoc test1.md -s -o test1.tex
pandoc test1.md -f markdown -t latex -s -o test1.tex

# read a markdown doc and print html to stdout
pandoc -s table.md --to html

As a “note to self,” this command helps with the Pandoc HTML to Markdown table conversion problem:

pandoc table.html --to=markdown_github -o table.md

I think the problem is that I’m used to a specific type of table markdown, and Pandoc emits something else by default.

For more information on Pandoc, see their getting started doc and user’s manual.

Safe String to Int conversion in Haskell (a string from the command line)

I was working on a Haskell factorial function (which turned out to be easy) and decided I wanted to write a program to prompt a user at the command line for an Int, and then do the factorial of that Int.

I quickly learned that this required a String to Int conversion, which I wanted to do safely (I didn’t want my program to blow up). Technically what I do is actually a String to Maybe Int conversion, as you can see in this code:

How to go to and from Java collections in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 17.1, “How to go to and from Java collections in Scala.”

Problem

You’re using Java classes in a Scala application, and those classes either return Java collections, or require Java collections in their method calls.

Solution

Use the methods of Scala’s JavaConversions object to make the conversions work.

How to create a simple Scala object from a JSON String

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is a short recipe, Recipe 15.3, “How to create a simple Scala object from a JSON String.”

Problem

You need to convert a JSON string into a simple Scala object, such as a Scala case class that has no collections.

Solution

Use the Lift-JSON library to convert a JSON string to an instance of a case class. This is referred to as deserializing the string into an object.

How to create JSON strings from Scala classes that have collections fields

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.2, “How to create a JSON String from Scala classes that have collections.”

Problem

You want to generate a JSON representation of a Scala object that contains one or more collections, such as a Person class that has a list of friends or addresses.

How to create a JSON string from a Scala object

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 15.1, “How to create a JSON string from a Scala object.”

Problem

You’re working outside of a specific framework, and want to create a JSON string from a Scala object.

Solution

If you’re using the Play Framework, you can use its library to work with JSON, as shown in Recipes 15.14 and 15.15, but if you’re using JSON outside of Play, you can use the best libraries that are available for Scala and Java: