Author: Julian Schrittwieser

Units of Measure - A Scala Macro System

I finally finished my Units of Measure system for Scala. The source is - of course - on [cached]GitHub, and I'm putting the finishing touches on my thesis about it, but for a quick view please refer to this presentation:


Fleeting Thoughts

Often I wish for a way to instantly capture my thoughts at any given moment so I can pursue them later on, when I have more time on my hands. Just now, I realized that wouldn't actually work as intended: My whole state of mind would be different then, so the thoughts would play out differently, I'd come to different come conclusions.

This presents me with a conundrum: Sometimes, there are too many interesting thoughts to pursue all of them. How do I pick? Do I try to recapture those that I didn't pick at a later time?


Units of Measurement - An Overview

Last time, I talked a bit about how you might implement parts of a Units of Measurement system in Scala (stay tuned for the rest of the implementation). Since this is going to be the topic of my Bachelor's thesis, I've made a presentation about Units of Measurement systems in other languages, specifically F#, C++ and Haskell. You can view it right here:

or download a pdf (without the snazzy transitions).


A System of Measurements in Scala

The newest version of Scala, 2.10, will allow to user to execute code at compile time with Macros, which are just normal Scala functions. You can get an overview of the possibilities at the official [cached]Scala Macro guide. Right now, Scala 2.10 is still under development, so you will have to use a Release Candidate to follow along.

First of all, a good system of measurements should make it easy to define values with units in code - no complicated trickery should be required. That's easy to achieve with a small utility macro:

1
val a = u(42 …

Handdrawn Graphs with D3

From [cached]Dan Foreman-Mackey, with slight modifications.


Beautiful JSON parsing in Scala

You probably all know JSON - it's becoming the universal data exchange format, slowly but steadily replacing XML. In JavaScript, JSON is a proper first class citizen:

1
2
3
4
5
6
7
8
person = {
  "name": "Joe Doe",
  "age": 45,
  "kids": ["Frank", "Marta", "Joan"]
};

person.age;        // 45
person.kids[1];    // "Marta"

Sadly, it's not as easy in other languages. Scala does have a JSON parser in the standard library ([cached]scala.util.parsing.json.JSON), but it is terrible slow and the output is still not very nice.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12 …

Decrypting scrambled words

I'm sure you've all seen posts like

Olny srmat poelpe can raed tihs. I cdnuolt blveiee taht I cluod aulaclty uesdnatnrd waht I was rdanieg. The phaonmneal pweor of the hmuan mnid, ...

Now this is mildly amusing, but not all that interesting on itself. However, I began to wonder - is it possible to automatically unscramble texts like the above? Surprisingly, it (almost) is!

Of the 72945 unique words in my /usr/share/dict/words (not counting possesive forms, eg "storekeeper's"), only 630 have a non-unique scrambled form, if you ignore plural forms. What do I mean by this? Well, if …


Scalisp - now with tail-call optimization

The last few days, I worked a bit more on Scalisp. Most importantly, I added tail-call optimization. If you don't know what this is all about, think what a normal function call does: It grows the stack, one frame for each call. Normally, that's what you want, but not when you try to implement loops with recursive functions. (as you usually do in functional languages)

Take this simple example, summing all numbers from 1 to n (nevermind that there's a simple formula to do that):

1
2
3
4
(defun sum_to (n)
  (if (<= n 1)
    1
    (+ n (sum_to (- n 1 …

Scalisp - now with Compilation

It's been a while since my last update - I've been quite busy with course work. Part of that was a complete write of Scalisp - now it's truely List-based and it also includes a Compiler. Since it grew quite a bit, I split it up in several modules, which should be self-explaining:

1
2
3
4
5
6
7
8
+--------+   +--------------+    +----------+
| Parser +-->| Preprocessor +--->| Compiler |
+--------+   +------+-------+    +----------+
                   |
                   v
            +--------------+
            | Interpreter  |
            +--------------+

The Parser is very simple and quite short now, since it doesn't care about special forms anymore, it just makes sure that each expression has correct syntax:

 1
 2
 3
 4
 5
 6
 7
 8
 9 …

Scalisp - a Lisp interpreter in Scala

Inspired by [cached]Peter Norvig's lispy, I wrote a little Lisp interpreter in Scala. So far, it only provides a small subset of Lisp, but I plan to extend it eventually.

It makes heavy use of Scala's parser-combinators to build the AST (abstract syntax tree), execution is little more than calling "eval" on this tree.

Here's what the main parser looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
class LispParser extends JavaTokenParsers {
    def expression: Parser[Expression] = (
        "("~>expressionBody<~")" | number | string | variable )
    def expressionBody: Parser[Expression] = ( "quote"~>expression
        | "if"~expression~expression~expression …

© Julian Schrittwieser. Built using Pelican. Theme by Giulio Fidente on github. .