Tips for golfing in Factor on codegolf.stackexchange.com
See the official handbook for docs and to search for words. Note that the docs represent the latest development version, and so might differ occasionally from the stable version used by code.golf.
Imports
Code.golf uses Factor stable release 0.100 with the extras
vocab root removed. You can see if a vocab is available by looking at its docs page, under the Files
section. If the file paths begin with resource:extra/
, then that vocab is not available on code.golf. For example.
Code.golf runs in "auto-use" mode, so many vocabularies will be automatically imported when a word in that vocab is used. Some vocabs will not automatically import, in which case they must be explicitly imported using: USE: some-vocab
, or for multiple vocabs: USING: vocab1 vocab2 ;
.
Name clashes
Sometimes words will fail to load even other the vocabulary itself does automatically load. This happens when multiple different, auto-use vocabs all contain words with the same name. This can be resolved by either importing the desired vocab beforehand (explicitly or implicitly), or using the qualified form of the word (eg ascii:>lower
).
Strings
Strings in factors are just sequences of integers. This means you can (usually) treat a string as an array, or an array as a string.
To use character point 0
in a string, use the escape code \0
. Strings in Factor are multi-line, so \n
can be replaced with literal newlines.
Useful words / other tips
on
, +@
are shorter than drop
and 2drop
respectively.
stack.
for printing a list of numbers, one number per line.
simple-table.
for printing numbers on a single line, or grid.
[I
for printing fixed strings, optionally inserting computed values.
>dec
is a short version of number>string
.
printf
prints formatted strings. It can print numbers to arbitrary decimal places (eg. "%.1000f" printf
).
f
is a sequence.
(command-line)
is sometimes the shortest way to access args.
show
can sometimes be used instead of print
.
- New in 0.100,
..=
is shorter than [a..b]
,[1..b]
,etc. for constant arguments
Useful vocabs