Factor Language

last modified: December 29, 2013

A StackBasedLanguage based on JoyLanguage, ForthLanguage, LispLanguage, and SlateLanguage. It features the ConsCell as the building block of code, HigherOrderFunctions, CallWithCurrentContinuation, DynamicTyping, and DynamicScope.

See http://www.factorcode.org/.

For a sample, http://docs.factorcode.org/ leads to a slick web-based dictionary and documentation browsers for the source of the web server, which supports WebTransactionsWithContinuations. Super-cool!

Factor is a ConcatenativeLanguage.

-- SlavaPestov


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.


Loading...