1 ! Copyright (C) 2009 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: help.markup help.syntax kernel quotations math sequences
10 { "newquot" quotation }
12 { $description "Infers the number of inputs, " { $snippet "n" } ", to " { $snippet "quot" } " and calls the " { $snippet "quot" } " with the first " { $snippet "n" } " values from a sequence." }
15 "USING: combinators.smart math prettyprint ;"
16 "{ 1 2 3 } [ + + ] input<sequence ."
24 { "newquot" quotation }
26 { $description "Infers the number or outputs from the quotation and constructs an array from those outputs." }
29 "USING: combinators combinators.smart math prettyprint ;
31 { [ 1 - ] [ 1 + ] [ sq ] } cleave
39 { "quot" quotation } { "exemplar" "an exemplar" }
40 { "newquot" quotation }
42 { $description "Infers the number of outputs from the quotation and constructs a new sequence from those objects of the same type as the exemplar." }
45 "USING: combinators.smart kernel math prettyprint ;"
46 "4 [ [ 1 + ] [ 2 + ] [ 3 + ] tri ] V{ } output>sequence ."
53 { "quot" quotation } { "operation" quotation }
54 { "newquot" quotation }
56 { $description "Infers the number of outputs from " { $snippet "quot" } " and reduces them using " { $snippet "operation" } ". The identity for the " { $link reduce } " operation is the first output." }
59 "USING: combinators.smart kernel math prettyprint ;"
60 "3 [ [ 4 * ] [ 4 / ] [ 4 - ] tri ] [ * ] reduce-outputs ."
70 { $description "Infers the number of outputs from " { $snippet "quot" } " and returns their sum." }
73 "USING: combinators.smart kernel math prettyprint ;"
74 "10 [ [ 1 - ] [ 1 + ] bi ] sum-outputs ."
84 { $description "Infers the number of outputs from " { $snippet "quot" } " and returns a sequence of the outputs appended." }
87 "USING: combinators.smart prettyprint ;"
88 "[ { 1 2 } { \"A\" \"b\" } ] append-outputs ."
93 HELP: append-outputs-as
95 { "quot" quotation } { "exemplar" sequence }
98 { $description "Infers the number of outputs from " { $snippet "quot" } " and returns a sequence of type " { $snippet "exemplar" } " of the outputs appended." }
101 "USING: combinators.smart prettyprint ;"
102 "[ { 1 2 } { \"A\" \"b\" } ] V{ } append-outputs-as ."
103 "V{ 1 2 \"A\" \"b\" }"
107 { append-outputs append-outputs-as } related-words
110 { $values { "quot" quotation } }
111 { $description "Calls a quotation and drops any values it leaves on the stack." } ;
114 { $values { "quot" quotation } }
115 { $description "Calls a quotation and preserves any values it takes off the stack." } ;
117 { drop-outputs keep-inputs } related-words
119 ARTICLE: "combinators.smart" "Smart combinators"
120 "A " { $emphasis "smart combinator" } " is a macro which reflects on the stack effect of an input quotation. The " { $vocab-link "combinators.smart" } " vocabulary implements a few simple smart combinators which look at the static stack effects of input quotations and generate code which produces or consumes the relevant number of stack values." $nl
121 "Call a quotation and discard all output values or preserve all input values:"
122 { $subsection drop-outputs }
123 { $subsection keep-inputs }
124 "Take all input values from a sequence:"
125 { $subsection input<sequence }
126 "Store all output values to a sequence:"
127 { $subsection output>sequence }
128 { $subsection output>array }
129 "Reducing the set of output values:"
130 { $subsection reduce-outputs }
131 "Summing output values:"
132 { $subsection sum-outputs }
133 "Concatenating output values:"
134 { $subsection append-outputs }
135 { $subsection append-outputs-as }
136 "New smart combinators can be created by defining " { $link "macros" } " which call " { $link infer } "." ;
138 ABOUT: "combinators.smart"