From 34b8bcf3050d92ee4acc5cbe33283ad247ec78fa Mon Sep 17 00:00:00 2001 From: Slava Pestov Date: Fri, 21 Nov 2008 04:36:18 -0600 Subject: [PATCH] Clean up short-circuit combinators --- .../short-circuit/short-circuit-docs.factor | 14 ++++++------ .../short-circuit/short-circuit.factor | 22 ++++++++----------- .../short-circuit/smart/smart.factor | 8 +++---- 3 files changed, 19 insertions(+), 25 deletions(-) diff --git a/basis/combinators/short-circuit/short-circuit-docs.factor b/basis/combinators/short-circuit/short-circuit-docs.factor index 54fc3aac43..b1e4a6cf21 100644 --- a/basis/combinators/short-circuit/short-circuit-docs.factor +++ b/basis/combinators/short-circuit/short-circuit-docs.factor @@ -52,17 +52,17 @@ HELP: 3|| { "quot" quotation } } { $description "Returns true if any quotation in the sequence returns true. Each quotation takes the same three elements from the datastack and must return a boolean." } ; -HELP: n&&-rewrite +HELP: n&& { $values { "quots" "a sequence of quotations" } { "N" integer } { "quot" quotation } } -{ $description "A macro that reqrites the code to pass " { $snippet "N" } " parameters from the stack to each AND quotation." } ; +{ $description "A macro that reqrites the code to pass " { $snippet "n" } " parameters from the stack to each AND quotation." } ; -HELP: n||-rewrite +HELP: n|| { $values - { "quots" "a sequence of quotations" } { "N" integer } + { "quots" "a sequence of quotations" } { "n" integer } { "quot" quotation } } -{ $description "A macro that reqrites the code to pass " { $snippet "N" } " parameters from the stack to each OR quotation." } ; +{ $description "A macro that reqrites the code to pass " { $snippet "n" } " parameters from the stack to each OR quotation." } ; ARTICLE: "combinators.short-circuit" "Short-circuit combinators" "The " { $vocab-link "combinators.short-circuit" } " vocabulary stops a computation early once a condition is met." $nl @@ -77,8 +77,8 @@ ARTICLE: "combinators.short-circuit" "Short-circuit combinators" { $subsection 2|| } { $subsection 3|| } "Generalized combinators:" -{ $subsection n&&-rewrite } -{ $subsection n||-rewrite } +{ $subsection n&& } +{ $subsection n|| } ; ABOUT: "combinators.short-circuit" diff --git a/basis/combinators/short-circuit/short-circuit.factor b/basis/combinators/short-circuit/short-circuit.factor index e6a4bfe913..2b4e522789 100644 --- a/basis/combinators/short-circuit/short-circuit.factor +++ b/basis/combinators/short-circuit/short-circuit.factor @@ -3,12 +3,10 @@ locals generalizations macros fry ; IN: combinators.short-circuit MACRO:: n&& ( quots n -- quot ) - [let | pairs [ - quots [| q | { [ drop n ndup q dup not ] [ drop n ndrop f ] } ] map - { [ t ] [ n nnip ] } suffix - ] | - [ f pairs cond ] - ] ; + [ f ] + quots [| q | { [ drop n ndup q call dup not ] [ drop n ndrop f ] } ] map + [ n nnip ] suffix 1array + [ cond ] 3append ; MACRO: 0&& ( quots -- quot ) '[ _ 0 n&& ] ; MACRO: 1&& ( quots -- quot ) '[ _ 1 n&& ] ; @@ -16,13 +14,11 @@ MACRO: 2&& ( quots -- quot ) '[ _ 2 n&& ] ; MACRO: 3&& ( quots -- quot ) '[ _ 3 n&& ] ; MACRO:: n|| ( quots n -- quot ) - [let | pairs [ - quots - [| q | { [ drop n ndup q dup ] [ n nnip ] } ] map - { [ drop n ndrop t ] [ f ] } suffix - ] | - [ f pairs cond ] - ] ; + [ f ] + quots + [| q | { [ drop n ndup q call dup ] [ n nnip ] } ] map + { [ drop n ndrop t ] [ f ] } suffix 1array + [ cond ] 3append ; MACRO: 0|| ( quots -- quot ) '[ _ 0 n|| ] ; MACRO: 1|| ( quots -- quot ) '[ _ 1 n|| ] ; diff --git a/basis/combinators/short-circuit/smart/smart.factor b/basis/combinators/short-circuit/smart/smart.factor index ca659cacbe..b80e7294d1 100644 --- a/basis/combinators/short-circuit/smart/smart.factor +++ b/basis/combinators/short-circuit/smart/smart.factor @@ -1,7 +1,5 @@ - USING: kernel sequences math stack-checker effects accessors macros - combinators.short-circuit ; - +fry combinators.short-circuit ; IN: combinators.short-circuit.smart -MACRO: && ( quots -- quot ) dup arity n&&-rewrite ; +MACRO: && ( quots -- quot ) dup arity '[ _ _ n&& ] ; -MACRO: || ( quots -- quot ) dup arity n||-rewrite ; +MACRO: || ( quots -- quot ) dup arity '[ _ _ n|| ] ;