The
bend vocabulary is an experimental Factor port of some interesting features found in the bend language (
https://github.com/HigherOrderCO/bend).
The
BEND[ word can help create recursive datastructures, while
fold consumes them in a convenient way.
Examples:
USING: bend kernel math variants ;
IN: bend-example
VARIANT: tree
leaf: { value }
branch: { { left tree } { right tree } }
;
: make-tree ( depth -- tree )
0 BEND[ swap
[ <leaf> ]
[ 1 - swap 2 * [ fork ] [ 1 + fork ] 2bi <branch> ]
if-zero
] ;
: sum-tree ( tree -- sum )
{
{ leaf [ ] }
{ branch [ + ] }
} fold ;