Saturday, March 31, 2007

Factor Magic

The last few days were a wonderful immersion in the Factor world and if you're looking for a boost in productivity you should take a look at its C library interface for a start. Even Ruby which is reputed for its simple C integration pales in front of how simple it is in Factor. I was able to put together a basic Oracle OCI binding in a few hours where it would have taken days in Ruby. It turns out that a lengthy "Hello World!" C extension in Ruby is a one-liner in Factor (more about this in an upcoming article).

Copyright (C) 2007 Elie Chaftari

My purpose today is to tell you about the "magical" aspects of programming in Factor. This is how I see it. Say you want to add 2 to 3. You put 2 and 3 on the stack, then you "invoke" the "+" word putting the result (5) on the stack.

Let's try something a little more involved. If we put 1 2 3 4 on the stack, "invoking" the "rot" word "manipulates" the stack leaving 1 3 4 2 on it. Thus "rot" causes the 3rd stack item to be moved to the top while the 1st and 2nd stack item are pushed down. Programming in Factor is merely about invoking words that manipulate the stack for your needs.

We're going to test drive this and I'll visually illustrate some basic words to show you how simple it is to get quick results. If you want to follow on with your own "magic wand" download the appropriate Factor binary for your OS from Factor's Website.

Extract the archive file then start the UI by double-clicking f.exe on Windows or Factor.app on Mac OS X.

Copyright (C) 2007 Elie Chaftari

I have redrawn the UI in the image above keeping the relevant buttons for this walk through. Now let's practice what we just learned. What follows is a question from Factor's IRC channel.

Q: Hi, What's a stack-based language?
A: Hi, you get a stack as an implicit data structure that can store your data and then you define words that operate on that stack, like +, so 3 4 + that leaves 7 on the stack. You can output it or square it or whatever.

Now let's practice with these numbers. Write 3 4 + in the input area (space is significant). Hit Enter and 7 is displayed in the stack area. I promised I would illustrate some "magic" words and their effect on the stack. If you want to follow on push the 'Clear stack' button in the top menu bar.

Copyright (C) 2007 Elie Chaftari

Let's take our rot example from above and practice it with the help of the illustration. Write 1 2 3 4 in your input area and hit Enter (the Return key) on your keyboard. You should see the start stack displayed in your stack area. Notice the position of the top and bottom of the stack in your stack display area. Now write "rot" (without the quotation marks) in your input area and hit Enter. You should see the rot stack displayed as above because the "rot" word caused the 3rd stack item to be moved to the top while the 1st and 2nd stack item were pushed down. Now the "-rot" word will cause the top stack item to be moved to the 3rd item while the 2nd and 3rd items are pushed up. A -rot, following a rot, will return the stack to its original order. In the last example swap exchanges the stack positions of the top two stack items.

[Factor Magic, Part 2]

No comments: