Hi Slava,
Thanks v. much for the pointers, much appreciated.
The counter abstraction is a really handy one; unfortunately in this
case I found it uses the global namespace so it's not incrementing the
counter in the namespace created using with-scope.
Definition
USING: kernel ;
IN: namespaces
: counter ( variable -- n ) global [ dup inc get ] bind ;
But a local-counter version is easily made. N.B. the docs aren't clear
that it's using the global namespace - maybe it could be called
'global-counter'?
BTW I'm really enjoying programming in factor so far - there's a real
satisfaction in making code small and readable and Factor's uncluttered
syntax provides lots of opportunities to do this.
Thanks again,
Phil
Slava Pestov wrote:
> Hi,
>
> Your code looks good. All you're missing is knowledge of two library
> words.
>
> First, take a look at the counter word:
>
> ( scratchpad ) SYMBOL: current-id
> ( scratchpad ) current-id counter .
> 1
> ( scratchpad ) current-id counter .
> 2
> ( scratchpad ) current-id counter .
> 3
>
> Having an incrementing counter in a variable, where you increment the
> counter then return it immediately, is a very common idiom, used in
> the compiler and elsewhere, so there's a word for this.
>
> We can combine this with the cache combinator -- again, another very
> common idiom, where you look at a hashtable, and if a key is missing,
> you fill it in, otherwise you return the value:
>
> SYMBOL: current-id
> SYMBOL: ids
>
> : str>id ( string -- id )
> ids get [ drop current-id counter ] cache ;
>
> Your with-idmap word is totally fine except I'd indent it differently:
>
> : with-idmap ( quot -- )
> [
> H{ } clone ids set
> 0 current-id set
> call
> ] with-scope ; inline
>
> Have fun,
>
-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk