1 USING: help.markup help.syntax kernel kernel.private
2 sequences words namespaces.private quotations vectors
6 ARTICLE: "namespaces-combinators" "Namespace combinators"
7 { $subsection make-assoc }
8 { $subsection with-scope }
9 { $subsection with-variable }
10 { $subsection bind } ;
12 ARTICLE: "namespaces-change" "Changing variable values"
17 { $subsection change } ;
19 ARTICLE: "namespaces-global" "Global variables"
20 { $subsection namespace }
21 { $subsection global }
22 { $subsection get-global }
23 { $subsection set-global } ;
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."
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."
39 { $subsection ndrop } ;
41 ARTICLE: "namespaces" "Variables and namespaces"
42 "The " { $vocab-link "namespaces" } " vocabulary implements simple dynamically-scoped variables."
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" } ")."
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."
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." ;
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 } "." } ;
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" } ;
71 { $values { "variable" "a variable, by convention a symbol" } }
72 { $description "Assigns a value of " { $link f } " to the variable." }
73 { $side-effects "variable" } ;
76 { $values { "variable" "a variable, by convention a symbol" } }
77 { $description "Assigns a value of " { $link t } " to the variable." }
78 { $side-effects "variable" } ;
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" } ;
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" }
90 { $example "USING: namespaces prettyprint ;" "IN: scratchpad" "SYMBOL: foo\n1 foo +@\n10 foo +@\nfoo get ." "11" }
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" } ;
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" } ;
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" } ;
110 { $values { "quot" quotation } }
111 { $description "Calls the quotation in a new namespace. Any variables set by the quotation are discarded when it returns." } ;
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" }
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." } ;
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" } "." } ;
130 { $values { "namespace" "an assoc" } }
131 { $description "Outputs the current namespace. Calls to " { $link set } " modify this namespace." } ;
134 { $values { "g" "an assoc" } }
135 { $description "Outputs the global namespace. The global namespace is always checked last when looking up variable values." } ;
138 { $values { "variable" "a variable, by convention a symbol" } { "value" "the value" } }
139 { $description "Outputs the value of a variable in the global namespace." } ;
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" } ;
147 { $values { "namestack" "a vector of assocs" } }
148 { $description "Outputs the current name stack." } ;
151 { $values { "namestack" "a vector of assocs" } }
152 { $description "Outputs a copy of the current name stack." } ;
155 { $values { "namestack" "a vector of assocs" } }
156 { $description "Replaces the name stack with a copy of the given vector." } ;
159 { $values { "namespace" "an assoc" } }
160 { $description "Pushes a namespace on the name stack." } ;
163 { $description "Pops a namespace from the name stack." } ;
166 { $var-description "Temporary mutable growable sequence holding elements accumulated so far by " { $link 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 }" } } ;
174 { $values { "elt" object } }
175 { $description "Adds an element to the end of the sequence being constructed by " { $link make } "." } ;
178 { $values { "seq" "a sequence" } }
179 { $description "Appends a sequence to the end of the sequence being constructed by " { $link make } "." } ;
181 HELP: init-namespaces
182 { $description "Resets the name stack to its initial state, holding a single copy of the global namespace." }