Friday, October 16, 2009

System-wide Factor

I like the Factor listener and its powerful help system. You also get a debugger, a code walker, and much more with it. It's also a two-way tool if you use it with a supported editor. I personally use TextMate with the latest Factor TextMate bundle.

I however often use the terminal and like to have Factor handy system-wide. If I want to quickly test an idea, I like to start the terminal with Ctrl-Tab "T" (Quicksilver) and be able to just fire up Factor. Instead of entering the full path to factor or cd to it and run ./factor, I prefer calling it just like any other system command, by issuing a factor command in the terminal.

Before - './YourPathToFactor/factor/factor'

This can easily be done by creating a file (not a soft link because factor is not a self-contained executable) named factor pointing to the factor executable in your factor main folder. My Factor main folder is on my Desktop. Change the path to Factor's main folder accordingly in the following file. Don't forget to chmod 755 to make it executable.

cat ~/bin/factor

exec rlwrap "/Users/elie/Desktop/factor/factor" "$@"

The location of the above file should be somewhere under you PATH. I have a personal bin folder under my home directory (~/bin) for that use. I also added rlwrap in the exec command for readline history and reverse-i-search (ctrl-r). I previously installed rlwrap with Macports. Here's my .bashrc file. You can see that I added ~/bin to my $PATH.

cat ~/.bashrc

export PATH="/opt/local/bin:/opt/local/sbin:~/bin:$PATH"

After - it's as easy as 'factor'

You can also set 'Auto Use' on (available with the 'Auto Use' button in the listener) with the com-auto-use word. Beyond running Factor scripts, you can also have in-place vocab scaffolding by adding your current "." folder to the vocab roots with:

vocab-roots [ "." suffix ] change

The above line could be permanently added to your .factor-rc file but it currently disrupts the help system in the listener with a Permission denied (13) (probably due to searching on the "." path without the appropriate privileges).

Now supposing you change directory to your Desktop (cd Desktop/) and want to start experimenting with a new vocab there before moving it later to the work or extra directory. You only need to enter the following to get started:

"." "my-vocab" scaffold-vocab

Sample session:
Elie:~ elie$ cd Desktop/
Elie:Desktop elie$ factor
Loading /Users/elie/.factor-rc
( scratchpad ) com-auto-use
1: com-auto-use
No word named ``com-auto-use'' found in current vocabulary search path

The following restarts are available:

:1 Use the vocabulary
:2 Defer word in current vocabulary

Type :help for debugging help.
( scratchpad ) :1
1: Note:
Added "" vocabulary to search path
( scratchpad - auto ) vocab-roots [ "." suffix ] change
( scratchpad - auto ) "." "my-vocab" scaffold-vocab
1: Note:
Added "tools.scaffold" vocabulary to search path
Creating scaffolding for P" my-vocab/my-vocab.factor"
Creating scaffolding for P" my-vocab/authors.txt"
Loading my-vocab/my-vocab.factor
( scratchpad - auto )

Friday, May 16, 2008

Factor as a middle-tier language

Medioset came out of stealth mode a couple of weeks ago with an ambitious middle-tier experts certification program called Midflow™. As the FAQ goes, Midflow™ is a network of executive middle-tier experts, backed by a world-wide certification program (more details at

Copyright (C) 2008 Elie Chaftari

Beyond the expertise program, Medioset owns a suite of identity management enabling applications. These applications currently rely on a mix of Java, Erlang, and Ruby (RoR). I am seriously considering Factor as a unique middle-tier language to replace the above ones and unify the middle-tier architecture.

Since my first interest in Factor (over a year now), the language kept getting better and better at a very fast pace. No other language lets me bind to the numerous proprietary SDKs I have to deal with as easily. At the same time Factor's process launcher greatly eases system integration. I also expect its concurrency performance to be good enough by release 1.0 to replace Erlang. Moreover, it has its own Web framework and server to replace Rails.

When Factor reaches its 1.0 milestone release, I will start to gradually phase out the older Java, Erlang, and Rails applications with new Factor-based ones.

Update: Medioset is no more. I sold the crown jewels (i.e. the virtual meta-directory technology) and the main justification for the experts network was to support the identity management suite. I'm back into consulting full time.

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!