In Factor, you define a new word with : name ( stack-effect ) body ;.
The body is a sequence of words; calling your new word runs them in
order.
USING: kernel ;
IN: my-vocab
: shout ( str -- str ) "!" append ;
When a routine grows large, you can — and should — factor it: pull groups of words into smaller named words, each with one job. The language is named for this.
Each .factor file declares a vocabulary with IN:. Words in one
vocabulary can call words from another by listing the other vocab in
USING:.
! file: my-vocab/helpers/helpers.factor
USING: kernel ;
IN: my-vocab.helpers
: shout-line ( str -- str ) "!" append ;
! file: my-vocab/my-vocab.factor
USING: kernel my-vocab.helpers sequences ;
IN: my-vocab
: announce ( names -- strs )
[ shout-line ] map ;
The directory layout follows the dotted vocab name: vocab my-vocab
lives in my-vocab/my-vocab.factor; vocab my-vocab.helpers lives
in my-vocab/helpers/helpers.factor. Factor's loader follows
USING: declarations transitively, loading each vocab on demand.
The bosun runs a daily watch briefing on the deck: a fixed greeting, a roll-call of the crew on duty, then a closing dismissal. You'll factor the briefing into small named lines and compose them.
You'll be editing two files for this exercise:
bosuns-briefing/helpers/helpers.factor — small reusable
pieces. Vocabulary: bosuns-briefing.helpers.bosuns-briefing/bosuns-briefing.factor — the main file that
composes the helpers. Vocabulary: bosuns-briefing.In helpers.factor, define greeting to return the fixed
opening string "All hands, attention!".
greeting .
! => "All hands, attention!"
In helpers.factor, define crew-line to take a name string and
return that name prefixed with " - " (two spaces, a hyphen, a
space).
"Lopez" crew-line .
! => " - Lopez"
In helpers.factor, define closing to return the fixed
dismissal string "Carry on.".
closing .
! => "Carry on."
In bosuns-briefing.factor, define roster to take an array of
crew names and return a single multi-line string with each name
formatted by crew-line, joined by newlines.
{ "Lopez" "Mwangi" } roster .
! => " - Lopez\n - Mwangi"
{ } roster .
! => ""
In bosuns-briefing.factor, define briefing to take an array
of crew names and return the full briefing as a single string:
the greeting on the first line, the roster on the next lines,
and the closing on the last line. Use the helpers and roster.
{ "Lopez" "Mwangi" } briefing .
! => "All hands, attention!\n - Lopez\n - Mwangi\nCarry on."
{ } briefing .
! => "All hands, attention!\n\nCarry on."
Sign up to Exercism to learn and master Factor with 47 concepts163 exercises, and real human mentoring, all for free.