Cleave combinators
Up:
Data flow combinators
,
Combinators
,
The language
,
Factor handbook
Prev:
Retain stack combinators
Next:
Spread combinators
The cleave combinators apply multiple quotations to a single value.
Two quotations:
•
bi ( x p q -- )
•
2bi ( x y p q -- )
•
3bi ( x y z p q -- )
Three quotations:
•
tri ( x p q r -- )
•
2tri ( x y p q r -- )
•
3tri ( x y z p q r -- )
An array of quotations:
•
cleave ( x seq -- )
•
2cleave ( x y seq -- )
•
3cleave ( x y z seq -- )
Technically, the cleave combinators are redundant because they can be simulated using shuffle words and other combinators, and in addition, they do not reduce token counts by much, if at all. However, they can make code more readable by expressing intention and exploiting any inherent symmetry. For example, a piece of code which performs three operations on the top of the stack can be written in one of two ways:
! First alternative; uses keep [ 1 + ] keep [ 1 - ] keep 2 * ! Second alternative: uses tri [ 1 + ] [ 1 - ] [ 2 * ] tri
The latter is more aesthetically pleasing than the former.
•
Expressing shuffle words with cleave combinators