Smart combinators
Factor handbook » The language » Combinators

Prev:Short-circuit combinators
Next:Quotation construction utilities


A smart combinator is a macro which reflects on the stack effect of an input quotation. The combinators.smart vocabulary implements a few simple smart combinators which look at the static stack effects of input quotations and generate code which produces or consumes the relevant number of stack values.

Take all input values from a sequence:
input<sequence ( seq quot -- )

input<sequence-unsafe ( seq quot -- )


Store all output values to a sequence:
output>sequence ( quot exemplar -- seq )

output>array ( quot -- array )


Reducing the set of output values:
reduce-outputs ( quot operation -- )

map-reduce-outputs ( quot mapper reducer -- quot )


Applying a quotation to groups of elements on the stack:
smart-apply ( quot n -- )


Summing output values:
sum-outputs ( quot -- n )


Concatenating output values:
append-outputs ( quot -- seq )

append-outputs-as ( quot exemplar -- seq )


Drop the outputs after calling a quotation:
drop-outputs ( quot -- )


Cause a quotation to act as a no-op and drop the inputs:
nullary ( quot -- )

Preserve the inputs below or above the outputs of the quotation:
preserving ( quot -- )

keep-inputs ( quot -- )


Versions of if that infer how many inputs to keep from the predicate quotation:
smart-if ( pred true false -- )

smart-when ( pred true -- )

smart-unless ( pred false -- )


Versions of if* that infer how many inputs to keep from the predicate quotation:
smart-if* ( pred true false -- )

smart-when* ( pred true -- )

smart-unless* ( pred false -- )


New smart combinators can be created by defining Macros which call infer.