Version franaise
Home About Download Resources Contact us
Browse thread
The Implicit Accumulator: a design pattern using optional arguments
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Quc_Peyrot <chojin@l...>
Subject: Re: [Caml-list] The Implicit Accumulator: a design pattern using optional arguments

On Jun 27, 2007, at 3:53 PM, Jon Harrop wrote:

[...]

> I was going to mention symbol tables to Raj B for his Python JIT  
> compiler but
> he hasn't gotten back to me yet.
>
> Basically, you memoize strings:
>
> # let symbol =
>     let m = Hashtbl.create 1 in
>     fun string ->
>       try Hashtbl.find m string with Not_found ->
>       Hashtbl.add m string string;
>       string;;
> val symbol : '_a -> '_a = <fun>
>
> This is another trick I learned whilst writing my Mathematica  
> interpreter (so
> many tricks, so little time). This function looks totally  
> pointless, like a
> no-op, but if you pipe your identifiers through it (e.g. when  
> building the
> AST during parsing) then all structurally-equal strings are the  
> same physical
> string. If you're careful, this lets you use physical equality for  
> string
> comparison and that is a lot faster.

It's a really nice trick indeed.

-- 
Best Regards,
Quc