100 Helloslanguages
Home / Languages / Factor

Factor

2003fraglet
functionalstack-basedconcatenative.factor
docker run --rm --platform="linux/amd64" 100hellos/factor:latest

MCP + fragletc

MCPstdinargs
This language supports code execution via MCP and the fragletc CLI. Stdin piping and argument passing are both supported.
Install fragletc →

Factor is a concatenative, stack-based programming language inspired by Forth and Lisp. It was designed by Slava Pestov and features powerful metaprogramming capabilities, a rich standard library, and an elegant object system.

About Factor

Factor is a dynamic, stack-based language with powerful metaprogramming features. It combines the simplicity of stack-based programming with modern language features like:

  • Dynamic typing with optional static type inference
  • Powerful metaprogramming through quotations and combinators
  • Rich standard library including GUI frameworks, web development tools, and more
  • Interactive development with a sophisticated listener (REPL)
  • Object-oriented programming with generic functions and method dispatch

The Stack-Based Paradigm

In Factor, computation is performed by manipulating a data stack. Functions (called "words" in Factor) consume values from the stack and push results back onto it:

! Push numbers onto the stack
2 3 +    ! Results in 5 on the stack

! String manipulation
"Hello" " " "World!" 3append    ! Results in "Hello World!"

Hello World Explained

Our Factor program is remarkably simple:

USE: io

"Hello World!" print
  • USE: io - Imports the input/output vocabulary
  • "Hello World!" - Pushes the string literal onto the stack
  • print - Pops the string from the stack and prints it to stdout

Key Features

Quotations

Factor's most distinctive feature is quotations - blocks of code that can be treated as data:

[ 2 * ] map    ! Apply doubling function to each element
[ dup * ] keep ! Square a number while keeping the original

Combinators

Higher-order functions that operate on quotations:

5 [ 1 + ] [ 2 * ] bi    ! Apply both functions: results in 6 and 10

Vocabularies

Factor organizes code into vocabularies (similar to modules):

USING: math sequences io ;

Language History

Factor was created by Slava Pestov in 2003, originally targeting the JVM. It has since evolved into a self-hosted language with its own virtual machine. The language is inspired by:

  • Forth - Stack-based computation model
  • Lisp - Metaprogramming and code-as-data philosophy
  • Smalltalk - Object system and interactive development

Further Exploration

  • Try the interactive listener: factor
  • Explore the rich vocabulary system
  • Learn about Factor's unique approach to object-oriented programming
  • Investigate the powerful metaprogramming capabilities
  • Check out the GUI framework for desktop applications

Factor represents a unique approach to programming that challenges conventional thinking about language design while remaining highly practical and expressive.

Hello World

USING: io ;

! BEGIN_FRAGLET
"Hello World!" print
! END_FRAGLET

Coding Guide

Language Version

Factor 0.100

Execution Model

  • Interpreted, runs directly from source files
  • Code executes at the top level
  • Stack-based computation model

Key Characteristics

  • Stack-based: Values are pushed onto and popped from a data stack
  • Postfix notation: Operations come after operands (e.g., 2 3 + instead of 2 + 3)
  • Words: Functions are called "words" in Factor
  • Quotations: Code blocks [ ... ] that can be treated as data
  • Vocabularies: Module system for organizing code
  • Case-sensitive: Identifiers are case-sensitive
  • Comments: Start with ! and continue to end of line

Fragment Authoring

Write valid Factor code. Your fragment becomes the script body. Code executes at the top level, so expressions run immediately in order.

Available Libraries

Factor includes a rich standard library organized into vocabularies:

  • io - Input/output operations
  • math - Mathematical operations
  • sequences - Sequence manipulation
  • combinators - Higher-order functions
  • kernel - Core language features

Use USING: to import vocabularies:

USING: io math sequences ;

Common Patterns

Stack Operations

! Push values onto stack
2 3

! Arithmetic operations
+    ! Adds top two values: 2 + 3 = 5
*    ! Multiplies top two values
-    ! Subtracts: 5 - 2 = 3
/    ! Divides: 6 / 2 = 3

String Operations

"Hello" "World!" append    ! Concatenates: "HelloWorld!"
"Hello" " " "World!" 3append    ! Appends all: "Hello World!"

Stack Manipulation

dup     ! Duplicates top of stack
drop    ! Removes top of stack
swap    ! Swaps top two stack elements
over    ! Copies second element to top

Quotations

[ 2 * ]    ! Quotation that doubles a number
[ dup * ]  ! Quotation that squares a number

Combinators

[ 1 + ] [ 2 * ] bi    ! Apply both functions to same value
[ 2 * ] map           ! Apply function to each element

Examples

! Simple output
USING: io ;
"Hello, World!" print
! Stack arithmetic
USING: io math ;
5 3 + number>string print    ! Outputs: 8
! String concatenation
USING: io sequences ;
"Hello" " " "World!" 3append print
! Using quotations
USING: io math ;
5 [ 2 * ] call number>string print    ! Outputs: 10
! Stack manipulation
USING: io math ;
10 dup * number>string print    ! Squares 10: outputs 100
! Multiple operations
USING: io math ;
2 3 + 4 * number>string print    ! (2 + 3) * 4 = 20
! Using vocabularies
USING: io math.functions ;
4 sqrt number>string print    ! Outputs: 2.0
! Combinators
USING: sequences prettyprint ;
{ 1 2 3 4 5 } [ 2 * ] map .    ! Doubles each: { 2 4 6 8 10 }

I/O

! Read a line from stdin and uppercase it
USING: io kernel ascii ;
readln >upper print

! Command-line args
USING: io command-line sequences ;
command-line " " join print

Caveats

  • Postfix notation: Remember that operations come after operands
  • Stack order matters: Values are consumed from the stack in reverse order
  • Quotations need execution: Use call or combinators to execute quotations
  • Vocabulary imports: Some operations require importing vocabularies with USING:
  • Stack effects: Factor tracks stack effects, but fragments are simple and don't need explicit declarations

Fraglet Scripts

Echo Args

#!/usr/bin/env -S fragletc --vein=factor
USING: command-line io kernel namespaces sequences ;
command-line get " " join "Args: " prepend print

Stdin Upper

#!/usr/bin/env -S fragletc --vein=factor
USING: io kernel ascii ;
readln >upper print

Test

#!/usr/bin/env -S fragletc --vein=factor
"Hello World!" print

Connections

influenced by

Container Info

image100hellos/factor:latest
build scheduleWednesday
fragletenabled