Monday, November 19, 2007

Factor Christmas Card

I am working on a Christmas card for the Factor community. The drawing is pretty much done, so don't ask me for a major redraw. If you like it and want the caption changed, we can have a caption contest :). The final version will come in 72 dpi for screen display and 300 dpi for printout. Please submit your suggestion in the comments (the suggested caption must fit in the balloon).

Click on the images below for larger versions.

ScienceDaily (Sep. 20, 2007) — A new look at some old bones have shown that velociraptor, the dinosaur made famous in the movie Jurassic Park, had feathers. The discovery was made by paleontologists at the American Museum of Natural History and the Field Museum of Natural History.

Friday, October 12, 2007

Getting started with Factor - Easy FFI

When I found Factor, a few months back, I was using Ruby for three years and Erlang for one year. I was looking for a language with a combination of the features of both languages but with a simpler FFI. Don't get me wrong, Ruby and Erlang are great languages with many features I then liked but I was looking for the "perfect" programming language (as we all do) and FFI wasn't up to my expectations with either of these.

What's FFI, by the way, and why is it so important for a real-world programmer. FFI (short for Foreign Function Interface) is a synonym for C extensions or C bindings and gives you access to a wealth of standard, universal, and platform independent libraries such as OpenSSL or OpenLDAP. If you were to write them from scratch in the programming language of your choice it would otherwise cost you a lengthy and uncertain development. Ruby is reputed for its simple C extensions but when I tackled these for the first time, they were far from being as simple as advertised. The same goes for Erlang where writing a C driver is a dreaded experience (I already know about Erlang Port and Erlang Interface but they are slow and limited to bridging your code with a huge switch case statement in C).

Let's get started now. If you've already installed Factor, skip this paragraph. Otherwise download the latest stable release from (0.90 at the time of this writing).

This tutorial focuses on Mac OS X and I'm using a PPC, so I downloaded Factor-0.90-ppc.dmg (the same web page also has binaries for Intel Mac and Windows as well as source downloads for other platforms) and dragged the Factor folder to the default Applications folder. 'Allow user to administer this computer' should be checked. As an alternative you can drag the Factor folder to your Desktop or to the user's own ~/Applications folder.

Drag Factor to your Applications folder

The Applications folder is C:\Program Files on Windows but you can just as well unpack the Factor distribution to your Desktop. Most of the commands will run unchanged on Linux, but you'll have to adapt them on Windows. Mainly for building the .dll since there are many compilers that can do the job (please paste your solution in the comments).

As many Ruby veterans, I'll be using TextMate for the tutorial examples but Factor comes with editor hooks for editpadpro, emacs, gvim, jedit, scite, textmate, and vim. You can't say you don't have the choice. The TextMate Factor Bundle (Factor.tmbundle) is under the misc/ folder.

You can at any point edit a specific word inside Factor with \ word edit. This will pop-up the file containing that word in TextMate with the edit cursor positioned at the beginning of the line where the word is defined. You need to modify line 7 in extra/editors/textmate/textmate.factor replacing "mate -a -l " with whatever the which mate command gives you as a location in your terminal (e.g. "/usr/bin/mate -a -l "), for this to work in Factor version 0.9 (it has been corrected in the upcoming version).

As an example of the above functionality, open the installed Factor folder and start Factor by double-clicking on the Factor icon (the raptor) then try the following in the Input area of the listener:

USE: editors.textmate

\ + edit

The Edit Hook in action

For this tutorial's purposes we're going to build a small C library. Paste the following code into a file called SampleDylib.c:



int addFunction( int a, int b ) {
return a + b;

int stringLength( char *str ) {
return strlen(str);


Now to compile it into a dynamic library on Mac OS X, run the following three commands in a terminal shell:

(Mac OS X)

cc -fno-common -c SampleDylib.c

cc -dynamiclib -install_name SampleDylib.dylib \
-o SampleDylib.dylib SampleDylib.o


cc -fPIC -c SampleDylib.c

cc -shared SampleDylib.o

(Mac OS X, Linux)

sudo mv SampleDylib.dylib /usr/local/lib

The last command puts the dynamic library in the library search path defined on Mac OS X by LD_LIBRARY_PATH, DYLD_LIBRARY_PATH, DYLD_FALLBACK_LIBRARY_PATH ($HOME/lib:/usr/local/lib:/usr/lib), and the process's working directory (this is usually done through make install with standard libraries). Linux has the LD_LIBRARY_PATH environment variable in case you move your .so to some other location than $HOME/lib, /usr/local/lib, or /usr/lib. The DLL would go typically under windows/system32 on Windows but since the DLL search path on Windows looks in the same directory as the executable, the system directory, and directories specified in the PATH environment variable, you can put your .dll anywhere under this path.

When you'll start contributing libraries to Factor these will go into the extra/ folder but for the time being create a new work/ folder under Factor's root folder where you can do your own experimentation. You can find out where Factor searches for vocabularies with vocab-roots get .. This is an example output:

V{ "resource:core" "resource:extra" "resource:work" "." }

where "." is your installed Factor's root folder.

Create a work/ folder

Let's organize the folder structure of our sample vocabulary. Under work/ create a new sample/ folder (mkdir sample) and inside sample/ create a libsample/ folder (mkdir libsample). Under sample/ create two files named sample.factor and sample-tests.factor respectively (touch sample.factor sample-tests.factor). Open the libsample/ folder and create a libsample.factor file (touch libsample.factor). This is how your file/folder structure should now look like:


Your file/folder structure in TextMate

Now let's start implementing our binding. If any of the words' usage is not clear you can get extensive help with:

\ word help

Here's the code for libsample.factor:


USING: alien alien.syntax ;

IN: sample.libsample

: add-sample-library
"libsample" "SampleDylib.dylib" "cdecl" add-library ; parsing


LIBRARY: libsample

FUNCTION: int addFunction ( int a, int b ) ;

FUNCTION: int stringLength ( char* str ) ;


You could also give the full path of SampleDylib.dylib to the add-library word above but you don't need to since you already moved the library to the Mac OS X's library search path. Later when the libraries you're binding to are located in unusual locations like /opt/local/lib (e.g. MacPorts), you can export their path with LD_LIBRARY_PATH (e.g. export LD_LIBRARY_PATH=/opt/local/lib) in order to avoid having to hard-code the path name (check \ add-library help for more details).

In a typical multi-platform implementation, you'll let Factor detect the OS:

"libsample" {
{ [ win32? ] [ "SampleDylib.dll" "stdcall" ] }
{ [ macosx? ] [ "SampleDylib.dylib" "cdecl" ] }
{ [ unix? ] [ "" "cdecl" ] }
} cond add-library

Notice how FUNCTIONs are declared. They reproduce the function prototype in C with simple differences such as char* str instead of char *str. You need to watch for the white spaces after the function name as parentheses and commas "are only syntax sugar" to make the declaration easier to read.

That's all it takes to implement a C binding in Factor!

Now we can add words that use the above FUNCTIONs in sample.factor:


USING: sample.libsample ;

IN: sample

: add-function ( x y -- z )
addFunction ;

: string-length ( str -- n )
stringLength ;


Let's test our new words in the listener. In the Input area enter:

USE: sample

Factor will find sample.factor since work/sample/ is under the vocabularies path and will compile the words. Now enter:

3 4 add-function .
"Factor: a practical stack language" string-length .

Calling string-length in the Listener

Ok, it works as expected. To make sure we have no regression with further development, we add some unit tests in sample-tests.factor:


USING: sample tools.test ;
IN: temporary

[ 7 ] [ 3 4 add-function ] unit-test

[ 34 ] [
"Factor: a practical stack language" string-length
] unit-test


You run the unit-tests with:

"sample" test

You must have called either USE: sample or "sample" require beforehand.

Well, that's it for this tutorial. If you have questions or comments, please feel free to post them below.

Tuesday, September 18, 2007

Website Design

Copyright (C) 2007 Elie Chaftari

Saturday, September 8, 2007


Copyright (C) 2007 Elie Chaftari

Copyright (C) 2007 Elie Chaftari

Copyright (C) 2007 Elie Chaftari

Copyright (C) 2007 Elie Chaftari

Thursday, April 26, 2007

Factor Magic, Part 2

In the first part of this article, we looked through some graphics at how you can manipulate the stack with words. We visually examined the positions of the "values" on the stack before and after "shuffle" words were applied.

Copyright (C) 2007 Elie Chaftari

As a reminder take another look at the start position and the other subsequent effects once the rot, -rot, and swap words have been applied.

Another way to express these effects is through "stack notation". With stack notation you describe what parameters a word expects and what that word will leave on the stack once it has been applied.

( n1 n2 n3 -- n )

Here, for example, parameters n1 n2 n3 (in that order) are expected to be on the stack prior to the word's execution. They will be "consumed", leaving a new value n on the stack. A double dash separates input parameters from output parameters.

Let's put this new knowledge to profit to write the above diagram's effects.

rot ( n1 n2 n3 -- n2 n3 n1 ) causes the 3rd stack item to be moved to the top while the 1st and 2nd stack item are pushed down.

( n1 n2 n3 -- n3 n1 n2 ) causes the top stack item to be moved to the 3rd position while the 2nd and 3rd items are pushed up. A -rot, following a rot, returns the stack to the start position.

( n1 n2 -- n2 n1 ) exchanges the stack positions of the top two stack items.

A good place to discover the remaining shuffle words is in the Factor documentation. Another getting-started spot is the Factor tutorial in the online documentation.

Your "book of magic spells" needs more words of course and this takes some practice.

According to Doug Coleman, one of Factor's top gurus, you need to learn the following words by heart: dup, keep, 2dup, 2keep, swap, pick, rot, -rot, roll, -roll, 2apply, 2array, first2, nth, set-nth, append. He also advises to learn the following words for iterating: each, each-with, map, map-with, 2each, 2map.

Twenty-two words to learn are not a high price for a lot of power. Another nifty trick they'll teach you on the IRC channel is to enter: '\ word see' or '\ word help' in the listener to get immediate insight or help about any word.

( scratchpad ) \ swap see
IN: kernel
PRIMITIVE: swap ( x y -- y x )

Here you "see" the vocabulary (kernel) of the word swap and its stack effect.

If this quick introduction got you interested in more Factor magic, drop by at the IRC channel ( #concatenative) where you can ask for help and advice. See you there!

Wednesday, April 4, 2007

And the winner is...

The raptor dinosaurs are the fiercest because of their weapons, their speed and their intelligence. They have been chosen as a symbol because they are pack hunters like the Factor team.

However, since many would have liked to keep the lonely F icon; I have combined the two symbols with a backlit velociraptor in front of a large F. Here's a preview before I turn it into an icon.

[Image removed because of possible copyright infringement]

I just learned that even redrawing a photograph as an illustration could end up in a copyright suit. I'll either have to go out find a raptor who would graciously accept to pose for me or ask the author's permission.

Update: I usually don't surrender easily. Since no raptor was willing to pose for me, I decided to create my own. Refreshing my old 3D modeler skills, I created a low-poly mesh of a dino and posed it. After a first subdivision, I refined the mesh and extracted the outline. Below you can see the screen captures I took at each step.

Copyright (C) 2007 Elie Chaftari

I did not use any reference for the 3D model on purpose to avoid copyright issues. Here is the current state of the icon on Windows (Vista ready) and Macintosh.

Copyright (C) 2007 Elie Chaftari

Copyright (C) 2007 Elie Chaftari

Tuesday, April 3, 2007

Musing on a Factor icon

Ruby has the gem. Python has the snake. What is Factor's symbol?

I came up with this stack-like "F" to prompt more ideas. Jot down those ideas and let me know your thoughts in the comments.

Copyright (C) 2007 Elie Chaftari

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.