# Control-flow combinators for Seq
#
# Built on top of `if`, the conditional combinator.
#
# ## Available Words
#
# - when: ( ..a Bool [ ..a -- ..a ] -- ..a ) Run quotation only when condition is true
# - unless: ( ..a Bool [ ..a -- ..a ] -- ..a ) Run quotation only when condition is false
#
# ## Examples
#
# x 0 i.> [ "positive" io.write-line ] when
# x 0 i.= [ "zero!" io.write-line ] unless
#
# ## Notes
#
# - The quotation must leave the stack shape unchanged (same in/out row).
# This is what makes `when` and `unless` one-armed: there's no else
# branch to unify against, so the no-op `[ ]` empty quotation must be
# compatible with the active branch.
# --------------------------------------------------------------------------
# when — run quotation only when condition is true
# --------------------------------------------------------------------------
# Push an empty quotation as the else-branch and dispatch through `if`.
: when ( ..a Bool [ ..a -- ..a ] -- ..a )
[ ] if
;
# --------------------------------------------------------------------------
# unless — run quotation only when condition is false
# --------------------------------------------------------------------------
# Stack at entry: ( ..a Bool quot ). We push `[ ]` (empty quotation),
# then `swap` so the layout becomes ( ..a Bool [ ] quot ) — i.e. the
# user's quotation is now the else-branch. `if` then runs the user's
# quotation when the Bool is false, and the empty quotation (no-op)
# when true.
: unless ( ..a Bool [ ..a -- ..a ] -- ..a )
[ ] swap if
;