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 $_'
$_ 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.
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.
As shown by the image, this short article shows how to crop images with ImageMagick by using the
convert commands. Notice that
mogrify modifies an existing image, while
convert creates a new image from the existing image. Please see that article for a little more information.
I don’t have any major conclusions to share in this blog post, so I’m more or less burying it at the back of my website. 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?
A little `lazy val` conversion example
To look at this I created a file named Foo.scala and put the following code in it:
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
I quickly learned that this required a
Int conversion, which I wanted to do safely (I didn’t want my program to blow up). Technically what I do is actually a
Maybe Int conversion, as you can see in this code:
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.”
You’re using Java classes in a Scala application, and those classes either return Java collections, or require Java collections in their method calls.
Use the methods of Scala’s
JavaConversions object to make the conversions work.
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.”
You need to convert a JSON string into a simple Scala object, such as a Scala
case class that has no collections.
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.
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.”
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.
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.”
You’re working outside of a specific framework, and want to create a JSON string from a Scala object.
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: