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 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]

Sunday, March 18, 2007

Blogging Impulse

This is a story about how I discovered Factor, a young programming language that brought the fun back in my developer's life. It's also an analysis of what pushed me overnight to start chatting and blogging, just to be able to talk about it.

This Wikipedia article better summarizes its features than I could. You can take my word for it and just go grab it at Factor's site or keep reading on for the long story.

I'm a programming language lover. I just can't resist the urge of testing every newcomer. Through the years I sticked with some out of need, others left my desktop the very next day. This path has taken me slowly to functional programming.

I'm usually just an observer and I seldom participate to the core development activity. I often define myself as a "client-programmer" to those languages and their libraries.

Among the languages I keep using are C, Java, Python, Ruby, and Erlang. I'm a good salesman and I could easily convince my clients that Java or, for the sake of the argument, even Visual Basic is more than enough for their needs. But as most of the readers, I came to programming just for fun in the first place. This fun factor never leaves you and you keep looking for a "better" language.

The definition of a better programming language differs a lot but instead of entering the details of the features I'll just say that I don't have a simple answer. This leaves us with the fun factor.

A good language should be like a Christmas tree with beautiful gifts. Under the hood it should hold many "pleasant" surprises and keep surprising you. Like: Ah! it has this, and that feature. Later when you think you have discovered everything about it, pleasant surprises keep coming at a constant rate.

When I started learning Ruby, the fun was there and is still there but deployment of the applications started to become a nightmare. Scalability is even more difficult to attain than with Java. So let's just say that this spoils your pleasure especially when you're running an IT company and keep receiving mad calls from your clients and you know pertinently that the language just can't cope with your scalability needs.

Ruby was also a turning point and a gateway to functional programming. You quickly discover how nice blocks are and slowly shift away from the object oriented paradigm with continuations. So after years of OOP brainwashing you start eyeing Lisp.

I started with Scheme (PLT MzScheme) but was quickly put off by the excessive use of parenthesis. I do understand the principles behind them and I did my best to just ignore the noise, in vain. Meanwhile I had drifted to Gambit-C only to discover Termite.

According to Termite's own site: "Termite is an Erlang-like distributed programming system written in Scheme." What follows is simple to imagine. I went all the way with Erlang. It solved every scalability, reliability, and concurrency problem I had. My whole product line is currently based on it. It answers my needs and my clients are satisfied.

Still, I am not yet appeased. Eternal quest you may think, but I believe I will find peace with Factor. It combines the fun of Ruby with the Erlang-style lightweight threads for the much needed concurrency. What's more, since Lisp is one of its main influences it was able to replace the nasty parentheses with a stack system inspired by Forth and Joy while still retaining Lisp's power.

What I almost forgot to tell you in my enthusiasm for Factor is that its (still) small community is one of the friendliest I've ever met. Brilliant minds but humble people that love to help and share.

I'll be back with more details about this beautiful language as my knowledge grows.