]> gitweb.factorcode.org Git - factor.git/blob - core/namespaces/namespaces-docs.factor
1da3bc45db16cd3b864c518128a40c1a2d998e9b
[factor.git] / core / namespaces / namespaces-docs.factor
1 USING: help.markup help.syntax kernel kernel.private
2 sequences words namespaces.private quotations vectors
3 math.parser math ;
4 IN: namespaces
5
6 ARTICLE: "namespaces-combinators" "Namespace combinators"
7 { $subsection make-assoc }
8 { $subsection with-scope }
9 { $subsection with-variable }
10 { $subsection bind } ;
11
12 ARTICLE: "namespaces-change" "Changing variable values"
13 { $subsection on }
14 { $subsection off }
15 { $subsection inc }
16 { $subsection dec }
17 { $subsection change } ;
18
19 ARTICLE: "namespaces-global" "Global variables"
20 { $subsection namespace }
21 { $subsection global }
22 { $subsection get-global }
23 { $subsection set-global } ;
24
25 ARTICLE: "namespaces-make" "Constructing sequences"
26 "There is a lexicon of words for constructing sequences without passing the partial sequence being built on the stack. This reduces stack noise."
27 { $subsection make }
28 { $subsection , }
29 { $subsection % }
30 { $subsection # } ;
31
32 ARTICLE: "namespaces.private" "Namespace implementation details"
33 "The namestack holds namespaces."
34 { $subsection namestack }
35 { $subsection set-namestack }
36 { $subsection namespace }
37 "A pair of words push and pop namespaces on the namestack."
38 { $subsection >n }
39 { $subsection ndrop } ;
40
41 ARTICLE: "namespaces" "Variables and namespaces"
42 "The " { $vocab-link "namespaces" } " vocabulary implements simple dynamically-scoped variables."
43 $nl
44 "A variable is an entry in an assoc of bindings, where the assoc is implicit rather than passed on the stack. These assocs are termed " { $emphasis "namespaces" } ". Nesting of scopes is implemented with a search order on namespaces, defined by a " { $emphasis "namestack" } ". Since namespaces are just assoc, any object can be used as a variable, however by convention, variables are keyed by symbols (see " { $link "symbols" } ")."
45 $nl
46 "The " { $link get } " and " { $link set } " words read and write variable values. The " { $link get } " word searches up the chain of nested namespaces, while " { $link set } " always sets variable values in the current namespace only. Namespaces are dynamically scoped; when a quotation is called from a nested scope, any words called by the quotation also execute in that scope."
47 { $subsection get }
48 { $subsection set }
49 "Various utility words abstract away common variable access patterns:"
50 { $subsection "namespaces-change" }
51 { $subsection "namespaces-combinators" }
52 { $subsection "namespaces-global" }
53 "A useful facility for constructing sequences by holding an accumulator sequence in a variable:"
54 { $subsection "namespaces-make" }
55 "Implementation details your code probably does not care about:"
56 { $subsection "namespaces.private" }
57 "An alternative to dynamic scope is lexical scope. Lexically-scoped values and closures are implemented in the " { $vocab-link "locals" } " vocabulary." ;
58
59 ABOUT: "namespaces"
60
61 HELP: get
62 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value, or " { $link f } } }
63 { $description "Searches the name stack for a namespace containing the variable, and outputs the associated value. If no such namespace is found, outputs " { $link f } "." } ;
64
65 HELP: set
66 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
67 { $description "Assigns a value to the variable in the namespace at the top of the name stack." }
68 { $side-effects "variable" } ;
69
70 HELP: off
71 { $values { "variable" "a variable, by convention a symbol" } }
72 { $description "Assigns a value of " { $link f } " to the variable." }
73 { $side-effects "variable" } ;
74
75 HELP: on
76 { $values { "variable" "a variable, by convention a symbol" } }
77 { $description "Assigns a value of " { $link t } " to the variable." }
78 { $side-effects "variable" } ;
79
80 HELP: change
81 { $values { "variable" "a variable, by convention a symbol" } { "quot" "a quotation with stack effect " { $snippet "( old -- new )" } } }
82 { $description "Applies the quotation to the old value of the variable, and assigns the resulting value to the variable." }
83 { $side-effects "variable" } ;
84
85 HELP: +@
86 { $values { "n" "a number" } { "variable" "a variable, by convention a symbol" } }
87 { $description "Adds " { $snippet "n" } " to the value of the variable. A variable value of " { $link f } " is interpreted as being zero." }
88 { $side-effects "variable" }
89 { $examples
90     { $example "USING: namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: foo\n1 foo +@\n10 foo +@\nfoo get ." "11" }
91 } ;
92
93 HELP: inc
94 { $values { "variable" "a variable, by convention a symbol" } }
95 { $description "Increments the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
96 { $side-effects "variable" } ;
97
98 HELP: dec
99 { $values { "variable" "a variable, by convention a symbol" } }
100 { $description "Decrements the value of the variable by 1. A variable value of " { $link f } " is interpreted as being zero." }
101 { $side-effects "variable" } ;
102
103 HELP: counter
104 { $values { "variable" "a variable, by convention a symbol" } { "n" integer } }
105 { $description "Increments the value of the variable by 1, and returns its new value." }
106 { $notes "This word is useful for generating (somewhat) unique identifiers. For example, the " { $link gensym } " word uses it." }
107 { $side-effects "variable" } ;
108
109 HELP: with-scope
110 { $values { "quot" quotation } }
111 { $description "Calls the quotation in a new namespace. Any variables set by the quotation are discarded when it returns." } ;
112
113 HELP: with-variable
114 { $values { "value" object } { "key" "a variable, by convention a symbol" } { "quot" quotation } }
115 { $description "Calls the quotation in a new namespace where " { $snippet "key" } " is set to " { $snippet "value" } "." }
116 { $examples "The following two phrases are equivalent:"
117     { $code "[ 3 x set foo ] with-scope" }
118     { $code "3 x [ foo ] with-variable" }
119 } ;
120
121 HELP: make-assoc
122 { $values { "quot" quotation } { "exemplar" "an assoc" } { "hash" "a new hashtable" } }
123 { $description "Calls the quotation in a new namespace of the same type as " { $snippet "exemplar" } ", and outputs this namespace when the quotation returns. Useful for quickly building assocs." } ;
124
125 HELP: bind
126 { $values { "ns" "a hashtable" } { "quot" quotation } }
127 { $description "Calls the quotation in the dynamic scope of " { $snippet "ns" } ". When variables are looked up by the quotation, " { $snippet "ns" } " is checked first, and setting variables in the quotation stores them in " { $snippet "ns" } "." } ;
128
129 HELP: namespace
130 { $values { "namespace" "an assoc" } }
131 { $description "Outputs the current namespace. Calls to " { $link set } " modify this namespace." } ;
132
133 HELP: global
134 { $values { "g" "an assoc" } }
135 { $description "Outputs the global namespace. The global namespace is always checked last when looking up variable values." } ;
136
137 HELP: get-global
138 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value" } }
139 { $description "Outputs the value of a variable in the global namespace." } ;
140
141 HELP: set-global
142 { $values { "value" "the new value" } { "variable" "a variable, by convention a symbol" } }
143 { $description "Assigns a value to the variable in the global namespace." }
144 { $side-effects "variable" } ;
145
146 HELP: namestack*
147 { $values { "namestack" "a vector of assocs" } }
148 { $description "Outputs the current name stack." } ;
149
150 HELP: namestack
151 { $values { "namestack" "a vector of assocs" } }
152 { $description "Outputs a copy of the current name stack." } ;
153
154 HELP: set-namestack
155 { $values { "namestack" "a vector of assocs" } }
156 { $description "Replaces the name stack with a copy of the given vector." } ;
157
158 HELP: >n
159 { $values { "namespace" "an assoc" } }
160 { $description "Pushes a namespace on the name stack." } ;
161
162 HELP: ndrop
163 { $description "Pops a namespace from the name stack." } ;
164
165 HELP: building
166 { $var-description "Temporary mutable growable sequence holding elements accumulated so far by " { $link make } "." } ;
167
168 HELP: make
169 { $values { "quot" quotation } { "exemplar" "a sequence" } { "seq" "a new sequence" } }
170 { $description "Calls the quotation in a new " { $emphasis "dynamic scope" } ". The quotation and any words it calls can execute the " { $link , } " and " { $link % } " words to accumulate elements. When the quotation returns, all accumulated elements are collected into a sequence with the same type as " { $snippet "exemplar" } "." }
171 { $examples { $example "USING: namespaces prettyprint ;" "[ 1 , 2 , 3 , ] { } make ." "{ 1 2 3 }" } } ;
172
173 HELP: ,
174 { $values { "elt" object } }
175 { $description "Adds an element to the end of the sequence being constructed by " { $link make } "." } ;
176
177 HELP: %
178 { $values { "seq" "a sequence" } }
179 { $description "Appends a sequence to the end of the sequence being constructed by " { $link make } "." } ;
180
181 HELP: init-namespaces
182 { $description "Resets the name stack to its initial state, holding a single copy of the global namespace." }
183 $low-level-note ;