]> gitweb.factorcode.org Git - factor.git/blob - core/quotations/quotations-docs.factor
Merge branch 'master' into experimental
[factor.git] / core / quotations / quotations-docs.factor
1 USING: arrays help.markup help.syntax strings sbufs
2 vectors kernel combinators ;
3 IN: quotations
4
5 ARTICLE: "quotations" "Quotations"
6 "Conceptually, a quotation is an anonymous function (a value denoting a snippet of code) which can be passed around and called."
7 $nl
8 "Concretely, a quotation is an immutable sequence of objects, some of which may be words, together with a block of machine code which may be executed to achieve the effect of evaluating the quotation. The machine code is generated by a fast non-optimizing quotation compiler which is always running and is transparent to the developer."
9 $nl
10 "Quotations evaluate sequentially from beginning to end. Literals are pushed on the stack and words are executed. Details can be found in " { $link "evaluator" } "."
11 $nl
12 "Quotation literal syntax is documented in " { $link "syntax-quots" } "."
13 $nl
14 "Quotations implement the " { $link "sequence-protocol" } ", and existing sequences can be converted into quotations:"
15 { $subsection >quotation }
16 { $subsection 1quotation }
17 "Wrappers:"
18 { $subsection "wrappers" } ;
19
20 ARTICLE: "wrappers" "Wrappers"
21 "Wrappers are used to push words on the data stack; they evaluate to the object being wrapped:"
22 { $subsection wrapper }
23 { $subsection literalize }
24 { $see-also "dataflow" "combinators" } ;
25
26 ABOUT: "quotations"
27
28 HELP: callable
29 { $class-description "The class whose instances can be passed to " { $link call } ". This includes quotations and composed quotations built up with " { $link curry } " or " { $link compose } "." } ;
30
31 HELP: quotation
32 { $description "The class of quotations. See " { $link "syntax-quots" } " for syntax and " { $link "quotations" } " for general information." } ;
33
34 HELP: >quotation
35 { $values { "seq" "a sequence" } { "quot" quotation } }
36 { $description "Outputs a freshly-allocated quotation with the same elements as a given sequence." } ;
37
38 HELP: 1quotation
39 { $values { "obj" object } { "quot" quotation } }
40 { $description "Constructs a quotation holding one element." }
41 { $notes
42     "The following two phrases are equivalent:"
43     { $code "\\ reverse execute" }
44     { $code "\\ reverse 1quotation call" }
45 } ;
46
47 HELP: wrapper
48 { $description "The class of wrappers. Wrappers are created by calling " { $link literalize } ". See " { $link "syntax-words" } " for syntax." } ;
49
50 HELP: <wrapper> ( obj -- wrapper )
51 { $values { "obj" object } { "wrapper" wrapper } }
52 { $description "Creates an object which pushes " { $snippet "obj" } " on the stack when evaluated. User code should call " { $link literalize } " instead, since it avoids wrapping self-evaluating objects (which is redundant)." } ;
53
54 HELP: literalize
55 { $values { "obj" object } { "wrapped" object } }
56 { $description "Outputs an object which evaluates to " { $snippet "obj" } " when placed in a quotation. If " { $snippet "obj" } " is not self-evaluating (for example, it is a word), then it will be wrapped." }
57 { $examples
58     { $example "USING: prettyprint quotations ;" "5 literalize ." "5" }
59     { $example "USING: math prettyprint quotations sequences ;" "[ + ] [ literalize ] map ." "[ \\ + ]" }
60 } ;
61
62 { literalize curry <wrapper> POSTPONE: \ POSTPONE: W{ } related-words