]> gitweb.factorcode.org Git - factor.git/commitdiff
add initialize word to namespaces. foo global [ [ bar ] unless* ] curry => foo [...
authorJoe Groff <arcata@gmail.com>
Tue, 10 Feb 2009 19:02:33 +0000 (13:02 -0600)
committerJoe Groff <arcata@gmail.com>
Tue, 10 Feb 2009 19:02:33 +0000 (13:02 -0600)
core/namespaces/namespaces-docs.factor
core/namespaces/namespaces-tests.factor
core/namespaces/namespaces.factor

index 1cc3d86e9866a9e2f5501f5191780b366abdd3a4..ff0542a7b87da8b877c0c7f326033d9d48f6b60f 100644 (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." } ;
index 4c11e2389f1605ebeb1679d59b8be01c6e03c702..616ddef7fc70299d23dfa05c7bdddaca66343760 100644 (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
index 36559095cba3902b824c842c39dd31231d4bfb45..24095fd38203122bcfb9e214148b5a35727715ad 100644 (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 ;