Skip to content

Instantly share code, notes, and snippets.

@nick-chandoke
Last active September 7, 2025 04:15
Show Gist options
  • Select an option

  • Save nick-chandoke/c22aea245f4deb0863698c40382ac777 to your computer and use it in GitHub Desktop.

Select an option

Save nick-chandoke/c22aea245f4deb0863698c40382ac777 to your computer and use it in GitHub Desktop.
Intro to Factoring

factoring and symbolic algebra

factor gets its name from the fact that factor code is easy to factor. factoring is, like in elementary algebra, a compression scheme whereby common information is separated. consider the following:

lyrics from the hit dmx song, party up
y'all gonna make me lose my mind
up in here, up in here
y'all gonna make me act a fool
up in here, up in here
y'all gonna make me lose my cool
up in here, up in here

these lyrics can be compressed greatly. also, since factor’s syntax is simply that words (functions or data) are whitespace separated, these lyrics literally can be a valid factor program, so long as all of the words are defined (and i’ll assume that they are). therefore to factor the lyrics is to factor factor code. the two ways that we factor code in factor are combinators and definitions. for example, let’s define some words uih & ygmm to express the code more tersely:

: uih ( x -- ) up in here, up in here ;
: ygmm ( -- x ) y'all gonna make me ;

ygmm lose my mind uih
ygmm act a fool   uih
ygmm lose my cool uih

we notice that each line both begins with ygmm and ends with uih; we can modify either word’s definition to include the other. i’ll include ygmm in uih:

: uih ( x -- ) [ y'all gonna make me ] dip up in here, up in here ;

lose my mind uih
act a fool   uih
lose my cool uih

now we notice that the 3 lines all end with uih, but we can’t add a definition to make this shorter. instead, we can use a combinator:

: uih ( x -- )
  [ y'all gonna make me ] dip
  up in here, up in here ;

lose my mind
act a fool
lose my cool
[ uih ] tri@

much terser, more modular code. this symbolic algebra is an interesting property of factor code. we can even treat function words just the same as data words; a datum literal can be thought of as a function that pushes the datum to the stack; and each function leaves some data (or not) on the stack, and so the word can be thought of as one that sets the stack to some data literal.

one note about the english lyrics vs factor code: english has a complicated grammar, where relations, such as transitive verbs or prepositions, are infix. in factor, all relations are postfix, so factoring is much easier in factor than in english. for example, we see that we can factor the above code even further:

: ui ( -- ) up in ;
: lm ( -- ) lose my ;
: uih ( x -- )
  [ y'all gonna make me ] dip
  ui here, ui here ;

lm mind
act a fool
lm cool
[ uih ] tri@

but now each of lm and ui, each appearing twice nearby each other, precedes the different words; almost always in factor it’s the opposite; we usually have patterns like here, ui here ui, which obviously factors into here, here [ ui ] bi@. still, depending on the words' stack effects, we might be able to use [ ui swap ] bi@. this demonstrates the beauty of concatenative / purely pointfree code: each piece is expressed as a relation with <other pieces> rather than as a relation with any particular pieces, thus maintaining complete modularity! purely functional code is similar, but not as strong, in that it allows lexical scoping, which inextricably makes inner scopes context-sensitive and therefore no longer modular.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment