]> gitweb.factorcode.org Git - factor.git/blob - basis/fry/fry-docs.factor
Fix permission bits
[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 \r
7 HELP: @\r
8 { $description "Fry specifier. Splices a quotation into the fried quotation." } ;\r
9 \r
10 HELP: fry\r
11 { $values { "quot" quotation } { "quot'" quotation } }\r
12 { $description "Outputs a quotation that when called, fries " { $snippet "quot" } " by taking values from the stack and substituting them in." }\r
13 { $notes "This word is used to implement " { $link POSTPONE: '[ } "; the following two lines are equivalent:"\r
14     { $code "[ X ] fry call" "'[ X ]" }\r
15 } ;\r
16 \r
17 HELP: '[\r
18 { $syntax "code... ]" }\r
19 { $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
20 { $examples "See " { $link "fry.examples" } "." } ;\r
21 \r
22 ARTICLE: "fry.examples" "Examples of fried quotations"\r
23 "The easiest way to understand fried quotations is to look at some examples."\r
24 $nl\r
25 "If a quotation does not contain any fry specifiers, then " { $link POSTPONE: '[ } " behaves just like " { $link POSTPONE: [ } ":"\r
26 { $code "{ 10 20 30 } '[ . ] each" }\r
27 "Occurrences of " { $link _ } " on the left map directly to " { $link curry } ". That is, the following three lines are equivalent:"\r
28 { $code \r
29     "{ 10 20 30 } 5 '[ _ + ] map"\r
30     "{ 10 20 30 } 5 [ + ] curry map"\r
31     "{ 10 20 30 } [ 5 + ] map"\r
32 }\r
33 "Occurrences of " { $link _ } " in the middle of a quotation map to more complex quotation composition patterns. The following three lines are equivalent:"\r
34 { $code \r
35     "{ 10 20 30 } 5 '[ 3 _ / ] map"\r
36     "{ 10 20 30 } 5 [ 3 ] swap [ / ] curry compose map"\r
37     "{ 10 20 30 } [ 3 5 / ] map"\r
38 }\r
39 "Occurrences of " { $link @ } " are simply syntax sugar for " { $snippet "_ call" } ". The following four lines are equivalent:"\r
40 { $code \r
41     "{ 10 20 30 } [ sq ] '[ @ . ] each"\r
42     "{ 10 20 30 } [ sq ] [ call . ] curry each"\r
43     "{ 10 20 30 } [ sq ] [ . ] compose each"\r
44     "{ 10 20 30 } [ sq . ] each"\r
45 }\r
46 "The " { $link _ } " and " { $link @ } " specifiers may be freely mixed:"\r
47 { $code\r
48     "{ 8 13 14 27 } [ even? ] 5 '[ @ dup _ ? ] map"\r
49     "{ 8 13 14 27 } [ even? ] 5 [ dup ] swap [ ? ] curry 3compose map"\r
50     "{ 8 13 14 27 } [ even? dup 5 ? ] map"\r
51 }\r
52 "Here are some built-in combinators rewritten in terms of fried quotations:"\r
53 { $table\r
54     { { $link literalize } { $snippet ": literalize '[ _ ] ;" } }\r
55     { { $link slip } { $snippet ": slip '[ @ _ ] call ;" } }\r
56     { { $link curry } { $snippet ": curry '[ _ @ ] ;" } }\r
57     { { $link compose } { $snippet ": compose '[ @ @ ] ;" } }\r
58     { { $link bi@ } { $snippet ": bi@ tuck '[ _ @ _ @ ] call ;" } }\r
59 } ;\r
60 \r
61 ARTICLE: "fry.philosophy" "Fried quotation philosophy"\r
62 "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
63 { $code\r
64     "'[ [ _ key? ] all? ] filter"\r
65     "[ [ key? ] curry all? ] curry filter"\r
66 }\r
67 "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 ``let'' 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
68 { $code\r
69     "'[ 3 _ + 4 _ / ]"\r
70     "[let | a [ ] b [ ] | [ 3 a + 4 b / ] ]"\r
71 } ;\r
72 \r
73 ARTICLE: "fry.limitations" "Fried quotation limitations"\r
74 "As with " { $vocab-link "locals" } ", fried quotations cannot contain " { $link >r } " and " { $link r> } ". This is not a real limitation in practice, since " { $link dip } " can be used instead." ;\r
75 \r
76 ARTICLE: "fry" "Fried quotations"\r
77 "A " { $emphasis "fried quotation" } " differs from a literal quotation in that when it is evaluated, instead of just pushing itself on the stack, it consumes zero or more stack values and inserts them into the quotation."\r
78 $nl\r
79 "Fried quotations are denoted with a special parsing word:"\r
80 { $subsection POSTPONE: '[ }\r
81 "Fried quotations contain zero or more " { $emphasis "fry specifiers" } ":"\r
82 { $subsection _ }\r
83 { $subsection @ }\r
84 "When a fried quotation is being evaluated, values are consumed from the stack and spliced into the quotation from right to left."\r
85 { $subsection "fry.examples" }\r
86 { $subsection "fry.philosophy" }\r
87 { $subsection "fry.limitations" }\r
88 "Quotations can also be fried without using a parsing word:"\r
89 { $subsection fry } ;\r
90 \r
91 ABOUT: "fry"\r