Factor Language

For a sample, docs.factorcode.org leads to a slick web-based dictionary and documentation browsers for the source of the web server, which supports Web Transactions With Continuations. Super-cool!


For a small sample of Factor code that performs an operation well-known to many Internet users, here's rot13.factor from the "extras" distributed with the Factor system.

! Copyright (C) 2006 Daniel Ehrenberg ! See http://factorcode.org/license.txt for BSD license. USING: kernel math sequences strings io combinators ascii ; IN: rot13

rotate ( ch base -- ch ) [ - 13 + 26 mod ] keep + ;

rot-letter ( ch -- ch )

{ { [ dup letter? ] [ CHAR: a rotate ] } { [ dup LETTER? ] [ CHAR: A rotate ] } { [ t ] [ ] } } cond ;

rot13 ( string -- string ) [ rot-letter ] map ;

rot13-demo ( -- )

"Please enter a string:" print flush readln [ "Your string: " write dup print "Rot13: " write rot13 print ] when* ;

MAIN:
rot13-demo

This illustrates a few properties of Factor code: it looks roughly like Forth, largely composed of colon definitions; control-flow words such as "cond" use data structures composed of code (called quotations) as their arguments; and higher-order functions such as "map" work similarly.

See original on c2.com