if-empty ( ..a seq quot1: ( ..a -- ..b ) quot2: ( ..a seq -- ..b ) -- ..b )
Factor handbook » The language » Collections » Sequence operations » Control flow with sequences

Next:when-empty ( ... seq quot: ( ... -- ... obj ) -- ... seq/obj )


Vocabulary
sequences

Inputs
seqa sequence
quot1a quotation
quot2a quotation


Outputs
None

Word description
Makes an implicit check if the sequence is empty. An empty sequence is dropped and quot1 is called. Otherwise, if the sequence has any elements, quot2 is called on it.

Examples
USING: kernel prettyprint sequences ; { 1 2 3 } [ "empty sequence" ] [ sum ] if-empty .
6


Definition


: if-empty
( ..a seq quot1: ( ..a -- ..b ) quot2: ( ..a seq -- ..b ) -- ..b )
[ dup empty? ] [ [ drop ] prepose ] [ ] tri* if ; inline