"100 [1,b] [ even? ] count ."\r
"50"\r
} ;\r
+\r
+HELP: if-seq\r
+{ $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }\r
+{ $description "Makes an implicit check if the sequence is empty. If the sequence has any elements, " { $snippet "quot1" } " is called on it. Otherwise, the empty sequence is dropped and " { $snippet "quot2" } " is called." }\r
+{ $example\r
+ "USING: kernel prettyprint sequences sequences.lib ;"\r
+ "{ 1 2 3 } [ sum ] [ \"empty sequence\" throw ] if-seq ."\r
+ "6"\r
+} ;\r
+\r
+HELP: if-empty\r
+{ $values { "seq" sequence } { "quot1" quotation } { "quot2" quotation } }\r
+{ $description "Makes an implicit check if the sequence is empty. An empty sequence is dropped and " { $snippet "quot1" } " is called. Otherwise, if the sequence has any elements, " { $snippet "quot2" } " is called on it." }\r
+{ $example\r
+ "USING: kernel prettyprint sequences sequences.lib ;"\r
+ "{ 1 2 3 } [ \"empty sequence\" ] [ sum ] if-empty ."\r
+ "6"\r
+} ;\r
+\r
+{ if-seq if-empty } related-words\r
[ ] [ { } 0 firstn ] unit-test
[ "a" ] [ { "a" } 1 firstn ] unit-test
+
+[ "empty" ] [ { } [ "not empty" ] [ "empty" ] if-seq ] unit-test
+[ { 1 } "not empty" ] [ { 1 } [ "not empty" ] [ "empty" ] if-seq ] unit-test
+
+[ "empty" ] [ { } [ "empty" ] [ "not empty" ] if-empty ] unit-test
+[ { 1 } "not empty" ] [ { 1 } [ "empty" ] [ "not empty" ] if-empty ] unit-test
: short ( seq n -- seq n' )
over length min ; inline
+
+: if-seq ( seq quot1 quot2 -- )
+ [ f like ] 2dip if* ; inline
+
+: if-empty ( seq quot1 quot2 -- )
+ swap if-seq ; inline