A couple of weeks ago, I came across this talk by Slava Pestov (creator of Factor), and I was completely blown away with what I saw. The language is more minimal than Lisp, more compositional than Haskell, and yet more extensible than almost any other language on the planet. It has the property of homoiconicity, but without the paretheses soup that is usually associated with the homoiconic languages (thanks to Lisps). The metaprogramming capabilities are top notch. The feature set is quite wide, but most of it is implemented as libraries in the language itself. Despite being such a high level language, Factor exposes all internals to the programmer. You can even manage memory manually, or do pointer arithmetic, if those things fancy you :-). As Slava said, Factor is truly the best implementation of Lisp and Smalltalk the world has.

After two busy weeks, I finally got some time to play around with Factor. Factor ships with a Listener (A Listener is to an REPL what a Homo Sapien is to a Neanderthal), which is a great tool to explore and learn the language and its libraries, and to exercise your concatenative grey cells. I was reminded of the “10 one-liners” meme that was going around a few months ago, and I thought I should give it a whirl in Factor. I managed to solve the first nine problems, but the tenth was beyond my current Factor fu. (I’d be glad if someone contributes a solution. :-)

USING: math.ranges io.encodings.utf8 xml concurrency.combinators ;

! 1. Multiply Each Item in a List by 2
10 [1,b] [ 2 * ] map

! 2. Sum a List of Numbers
1000 [1,b] sum

! 3. Verify if Exists in a String
: word-list ( -- seq ) { "Factor" "Concatenative" "Point-free" } ;
: tweet ( -- str ) "This is an example tweet that talks about Factor." ;
word-list [ tweet subseq? ] any?

! 4. Read in a File
"F:/data.txt" utf8 file-contents
"F:/data.txt" utf8 file-lines

! 5. Happy birthday to you!
4 [1,b] [ "Happy birthday " swap 3 = "dear Pradnya!" "to you!" ? append ] map [ . ] each
! Or: (suggested by @abeaumont on #concatenative)
"Happy birthday " 4 [ 2 = "dear Pradnya!" "to you!" ? append . ] with each-integer

! 6. Partition List of Numbers as per the Predicate
{ 49 58 76 82 88 90 } [ 60 < ] partition

! 7. Fetch and Parse an XML web service
"http://search.twitter.com/search.atom?&q=factor" <pull-xml>

! 8. Find minimum (or maximum) in a List
{ 14 35 -7 46 98 } infimum
{ 14 35 -7 46 98 } supremum

! 9. Parallel Processing
data-list [ process-item ] parallel-map

There could be better ways to write some of these examples. Also it’s possible that some examples may contain errors. All corrections, improvements, criticism is welcome.

More resources on Factor and concatenative programming:

  1. Factor programming language home
  2. Factor/FAQ
  3. Factor/FAQ thread at reddit
  4. Slava Pestov’s blog
  5. mrjbq’s re-factor repo at github