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,
Slava
On 28-Sep-07, at 7:16 AM, Phil Dawes wrote:
> implementation
> --------------
>
>
> <PRIVATE
>
> SYMBOL: current-id
> SYMBOL: ids-hash
>
> : set-id ( id string -- )
> ids-hash get set-at ;
>
> : get-id ( string -- id/f )
> ids-hash get at ;
>
> : new-id ( -- id )
> current-id dup inc get ;
>
> : str>new-id ( string -- id )
> new-id dup rot set-id ;
>
> PRIVATE>
>
> : with-idmap ( quotation -- )
> [ H{ } clone ids-hash set
> -1 current-id set
> call ] with-scope ;
>
> : str>id ( string -- id )
> dup get-id [ swap drop ] [ str>new-id ] if* ;
>
>
> ----------------------------------------------------------------------
> ---
> 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
-------------------------------------------------------------------------
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