]> gitweb.factorcode.org Git - factor.git/blob - basis/fry/fry-docs.factor
b3d2ff296e196b367706b6031cb87b9feb417c92
[factor.git] / basis / fry / fry-docs.factor
1 USING: help.markup help.syntax quotations kernel ;\r
2 IN: fry\r
3 \r
4 HELP: _\r
5 { $description "Fry specifier. Inserts a literal value into the fried quotation." }\r
6 { $examples "See " { $link "fry.examples" } "." } ;\r
7 \r
8 HELP: @\r
9 { $description "Fry specifier. Splices a quotation into the fried quotation." }\r
10 { $examples "See " { $link "fry.examples" } "." } ;\r
11 \r
12 HELP: fry\r
13 { $values { "quot" quotation } { "quot'" quotation } }\r
14 { $description "Outputs a quotation that when called, fries " { $snippet "quot" } " by taking values from the stack and substituting them in." }\r
15 { $notes "This word is used to implement " { $link POSTPONE: '[ } "; the following two lines are equivalent:"\r
16     { $code "[ X ] fry call" "'[ X ]" }\r
17 }\r
18 { $examples "See " { $link "fry.examples" } "." } ;\r
19 \r
20 HELP: '[\r
21 { $syntax "'[ code... ]" }\r
22 { $description "Literal fried quotation. Expands into code which takes values from the stack and substitutes them in place of the fry specifiers " { $link _ } " and " { $link @ } "." }\r
23 { $examples "See " { $link "fry.examples" } "." } ;\r
24 \r
25 HELP: >r/r>-in-fry-error\r
26 { $error-description "Thrown by " { $link POSTPONE: '[ } " if the fried quotation contains calls to retain stack manipulation primitives." } ;\r
27 \r
28 ARTICLE: "fry.examples" "Examples of fried quotations"\r
29 "The easiest way to understand fried quotations is to look at some examples."\r
30 $nl\r
31 "If a quotation does not contain any fry specifiers, then " { $link POSTPONE: '[ } " behaves just like " { $link POSTPONE: [ } ":"\r
32 { $code "{ 10 20 30 } '[ . ] each" }\r
33 "Occurrences of " { $link _ } " on the left map directly to " { $link curry } ". That is, the following three lines are equivalent:"\r
34 { $code \r
35     "{ 10 20 30 } 5 '[ _ + ] map"\r
36     "{ 10 20 30 } 5 [ + ] curry map"\r
37     "{ 10 20 30 } [ 5 + ] map"\r
38 }\r
39 "Occurrences of " { $link _ } " in the middle of a quotation map to more complex quotation composition patterns. The following three lines are equivalent:"\r
40 { $code \r
41     "{ 10 20 30 } 5 '[ 3 _ / ] map"\r
42     "{ 10 20 30 } 5 [ 3 ] swap [ / ] curry compose map"\r
43     "{ 10 20 30 } [ 3 5 / ] map"\r
44 }\r
45 "Occurrences of " { $link @ } " are simply syntax sugar for " { $snippet "_ call" } ". The following four lines are equivalent:"\r
46 { $code \r
47     "{ 10 20 30 } [ sq ] '[ @ . ] each"\r
48     "{ 10 20 30 } [ sq ] [ call . ] curry each"\r
49     "{ 10 20 30 } [ sq ] [ . ] compose each"\r
50     "{ 10 20 30 } [ sq . ] each"\r
51 }\r
52 "The " { $link _ } " and " { $link @ } " specifiers may be freely mixed, and the result is considerably more concise and readable than the version using " { $link curry } " and " { $link compose } " directly:"\r
53 { $code\r
54     "{ 8 13 14 27 } [ even? ] 5 '[ @ dup _ ? ] map"\r
55     "{ 8 13 14 27 } [ even? ] 5 [ dup ] swap [ ? ] curry compose compose map"\r
56     "{ 8 13 14 27 } [ even? dup 5 ? ] map"\r
57 }\r
58 "The following is a no-op:"\r
59 { $code "'[ @ ]" }\r
60 "Here are some built-in combinators rewritten in terms of fried quotations:"\r
61 { $table\r
62     { { $link literalize } { $snippet ": literalize '[ _ ] ;" } }\r
63     { { $link curry } { $snippet ": curry '[ _ @ ] ;" } }\r
64     { { $link compose } { $snippet ": compose '[ @ @ ] ;" } }\r
65 } ;\r
66 \r
67 ARTICLE: "fry.philosophy" "Fried quotation philosophy"\r
68 "Fried quotations generalize quotation-building words such as " { $link curry } " and " { $link compose } ". They can clean up code with lots of currying and composition, particularly when quotations are nested:"\r
69 { $code\r
70     "'[ [ _ key? ] all? ] filter"\r
71     "[ [ key? ] curry all? ] curry filter"\r
72 }\r
73 "There is a mapping from fried quotations to lexical closures as defined in the " { $vocab-link "locals" } " vocabulary. Namely, a fried quotation is equivalent to a " { $snippet "[| | ]" } " form where each local binding is only used once, and bindings are used in the same order in which they are defined. The following two lines are equivalent:"\r
74 { $code\r
75     "'[ 3 _ + 4 _ / ]"\r
76     "[| a b | 3 a + 4 b / ]"\r
77 } ;\r
78 \r
79 ARTICLE: "fry" "Fried quotations"\r
80 "The " { $vocab-link "fry" } " vocabulary implements " { $emphasis "fried quotation" } ". Conceptually, fried quotations are quotations with “holes” (more formally, " { $emphasis "fry specifiers" } "), and the holes are filled in when the fried quotation is pushed on the stack."\r
81 $nl\r
82 "Fried quotations are started by a special parsing word:"\r
83 { $subsections POSTPONE: '[ }\r
84 "There are two types of fry specifiers; the first can hold a value, and the second “splices” a quotation, as if it were inserted without surrounding brackets:"\r
85 { $subsections\r
86     _\r
87     @\r
88 }\r
89 "The holes are filled in with the top of stack going in the rightmost hole, the second item on the stack going in the second hole from the right, and so on."\r
90 { $subsections\r
91     "fry.examples"\r
92     "fry.philosophy"\r
93 }\r
94 "Fry is implemented as a parsing word which reads a quotation and scans for occurrences of " { $link _ } " and " { $link @ } "; these words are not actually executed, and doing so raises an error (this can happen if they're accidentally used outside of a fry)."\r
95 $nl\r
96 "Fried quotations can also be constructed without using a parsing word; this is useful when meta-programming:"\r
97 { $subsections fry }\r
98 "Fried quotations are an abstraction on top of the " { $link "compositional-combinators" } "; their use is encouraged over the combinators, because often the fry form is shorter and clearer than the combinator form." ;\r
99 \r
100 ABOUT: "fry"\r