add initialize word to namespaces. foo global [ [ bar ] unless* ] curry => foo [ bar ] initialize

db4
Joe Groff 2009-02-10 13:02:33 -06:00
parent b75f6f88da
commit 1708d10c9a
3 changed files with 22 additions and 3 deletions

View File

@ -1,6 +1,6 @@
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"
@ -20,7 +20,8 @@ ARTICLE: "namespaces-global" "Global variables"
{ $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."
@ -159,3 +160,7 @@ HELP: ndrop
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." } ;

View File

@ -12,3 +12,14 @@ H{ } clone "test-namespace" set
[ 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

View File

@ -37,4 +37,7 @@ PRIVATE>
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 ;