Hello,

Here's demo of a tree of named namespaces which is is navigable. I borrowed 
names from UNIX filesystem manipulation to make it clear what's going on.

        Use the vocabulary

( scratchpad ) USE: name-tree
----------
----------

        List the variables in the current namespace

( scratchpad ) ls
{ in use }
----------
----------

        What namespace are we in?

( scratchpad ) pwd
""
----------
----------

        Let's make a namespace

( scratchpad ) "francophone" mkdir
----------
----------

        Check the variables again. There's our new namespace.

( scratchpad ) ls
{ "francophone" in use }
----------
----------

        Switch to that namespace

( scratchpad ) "francophone" cd
----------
----------

        Check the 'pwd' again

( scratchpad ) pwd
"francophone"
----------
----------

        Look at the variables

( scratchpad ) ls
{ in dir-name }
----------
----------

        Make some new variables

( scratchpad ) t "algeria" set
----------
----------
( scratchpad ) t "france" set
----------
----------
( scratchpad ) t "quebec" set
----------
----------

        List them

( scratchpad ) ls
{ dir-name "france" "quebec" "algeria" in }
----------
----------

        Let's make a namespace and switch to it

( scratchpad ) "other" mkdir
----------
----------
( scratchpad ) "other" cd
----------
----------

        What "directory" are we in?

( scratchpad ) pwd
"francophone:other"
----------
----------

        Let's switch to the parent namespace

( scratchpad ) up
----------
----------
( scratchpad ) pwd
"francophone"
----------
----------
( scratchpad ) ls
{ dir-name "france" "quebec" "algeria" "other" in }
----------
----------

As is often the case with Factor, the implementation is delightfully short; 
six one liners.

Ed

----------------------------------------------------------------------
USING: kernel namespaces namespaces.private
       sequences assocs prettyprint math newfx ;

IN: name-tree

SYMBOL: dir-name

: def ( var val -- ) swap set ;

: ls ( -- ) namespace keys . ;

: up ( -- ) namestack length 2 > [ ndrop ] when ;

: cd ( name -- ) get >n ;

: mkdir ( name -- ) H{ } clone dir-name pick is def ;

: pwd ( -- ) namestack 2 tail [ dir-name at ] map ":" join . ;
----------------------------------------------------------------------

-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to