Urgent.News

What's breaking now, across thousands of outlets.

Tech

Factor Overview

The Factor programming language lends itself well to newcomers familiar with programming but unfamiliar with stack-based languages. Its core concepts, such as stack operations, word definitions, quotations, control flow, and libraries, are demonstrated through practical examples. This overview assumes prior programming experience and introduces the essentials of Factor in an accessible manner.

To begin coding in Factor, start the terminal listener using the command ./factor -run=listener. The examples provided in this overview can be directly executed by pasting the code into the listener. Comments marked with ! are part of valid Factor source code. The examples cover various aspects of the language, starting from the basics of the stack, word definitions, quotations, control flow, and sequences.

As you progress, later sections delve into more advanced topics like objects, metaprogramming, and libraries that you can refer back to as needed.

Factor’s syntax differs from conventional programming languages in several ways. For instance, there are no parentheses around function arguments—instead, arguments are placed on the stack, and the word is invoked. This design choice emphasizes the language's stack-based nature. Additionally, space matters in Factor; words like 2 3 + consist of three tokens, whereas 2+3 is a single token and would need to be the name of a word.

A trailing ? often indicates a predicate, while a trailing ! typically denotes a mutating variant of a word. For example, append! is a mutating version of the append word. Quotations, which are collections of code values, can be passed to words, returned from words, and stored in collections. They operate similarly to functions in other languages but have some unique characteristics.

Factor supports a wide range of stack operations, allowing for efficient data manipulation. The usual stack shuffling words, such as swap, manipulate the stack without altering the values below the inputs. Words can return multiple values by simply leaving them on the stack, eliminating the need for explicit return statements. ALIAS: new-name existing-word is a way to define an alternative name for an existing word, promoting code reusability and flexibility.

Numerical computations in Factor are handled with ease, with integers automatically growing beyond machine size, and integer division potentially yielding exact ratios. For floating-point arithmetic, floating-point inputs are required. Bitwise operations have their own dedicated names and are separate from boolean logic. Quotations, which are blocks of code values, play a central role in Factor and are used extensively as combinators.

Boolean tests in Factor follow a specific set of rules. Only the false value is represented as f; zero, an empty string, and an empty array are all considered true. Conditional constructs such as if, when, unless, and cond allow for branching logic based on conditions. The case construct compares an input against keys, executing the corresponding branch when a match is found. Logical operators like and and or combine computed values, while short-circuit evaluation can be achieved by passing predicate quotations.

Factor’s approach to programming emphasizes stack operations and combinators, minimizing explicit shuffling and promoting concise and expressive code. By following the examples and guidelines provided in this overview, newcomers to Factor can quickly grasp the language's core concepts and start writing functional programs in this unique and powerful stack-based language.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at re.factorcode.org →

More in Tech

Apple Quietly Triples iCloud Mail Alias Limit to Nine

Apple appears to have quietly tripled the number of iCloud Mail aliases users can create, increasing the limit from three to nine. The increase was first noticed last week by a Reddit user, and picked up shortly after by a member of the MacRumors forums . MacRumors can confirm the change is live at iCloud.com.

More from Monday 5 October →