USING: help.markup help.syntax kernel kernel.private
sequences words namespaces.private quotations vectors
-math.parser math ;
+math.parser math words.symbol ;
IN: namespaces
ARTICLE: "namespaces-combinators" "Namespace combinators"
{ $subsection namespace }
{ $subsection global }
{ $subsection get-global }
-{ $subsection set-global } ;
+{ $subsection set-global }
+{ $subsection initialize } ;
ARTICLE: "namespaces.private" "Namespace implementation details"
"The namestack holds namespaces."
HELP: init-namespaces
{ $description "Resets the name stack to its initial state, holding a single copy of the global namespace." }
$low-level-note ;
+
+HELP: initialize
+{ $values { "variable" symbol } { "quot" quotation } }
+{ $description "If " { $snippet "variable" } " does not have a value in the global namespace, calls " { $snippet "quot" } " and assigns the result to " { $snippet "variable" } " in the global namespace." } ;
[ f ]
[ H{ } clone [ f "some-global" set "some-global" get ] bind ]
unit-test
+
+SYMBOL: test-initialize
+test-initialize [ 1 ] initialize
+test-initialize [ 2 ] initialize
+
+[ 1 ] [ test-initialize get-global ] unit-test
+
+f test-initialize set-global
+test-initialize [ 5 ] initialize
+
+[ 5 ] [ test-initialize get-global ] unit-test
H{ } clone >n call ndrop ; inline
: with-variable ( value key quot -- )
- [ associate >n ] dip call ndrop ; inline
+ [ associate >n ] dip call ndrop ; inline
+
+: initialize ( variable quot -- )
+ [ global ] [ [ unless* ] curry ] bi* change-at ;