Tag: programming

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 …

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 …

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 …

Visualizing sorting Algorithms with D3

In my Algorithm class, we had to implement our own sorting algorithm as an exercise in pseudo-code. It was very simple: Alternatingly iterate over an array from beginning to end and from end to beginning, always swapping pairs over numbers which are not correctly sorted. Repeat until finished.

In Python (you can also [cached]download a runnable versionspan)

 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
def sort(a):
  lower_limit = 0
  upper_limit = len(a) - 1

  while(lower_limit != upper_limit ):
    i …

LifeSaver - Game of Life as Screensaver

Recently, I promised you that I'd write my own screensaver. Well, here it is - an implementation of [cached]Conway's Game of Life using [cached]xscreensaver and OpenGL. It's based on the Python implementation I wrote, but quite a bit faster thanks to some trickery. Here's how it looks like:

Screenshot of the Screensaver

Since I need to blit a lot of pixels for each frame (all changed cells + all those that died recently and are fading away), I decided to use an OpenGL texture for drawing. That way, my dense array gets painted directly to the screen. To avoid unecessary copying, I've interleaved the …


A fairly minimal xscreensaver

Today, I wanted to write my own screensaver, so I started looking for frameworks. Quickly, I found [cached]xscreensaver, the de facto standard (?) for screensavers on X11.

The readme does mention two example screensavers developers should use to start out, but they still contain some superfluous stuff (GOTOs !). To get a better understanding of the framework, I set out to reduce them as much as possible. Without further ado, here's the result of my work. Stay tuned for my finished screensaver.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19 …

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