From: Nic F. <nfe...@ta...> - 2000-09-07 14:03:05
|
One of the TODO items is hashtable code. I have a hashtable interface that I have written for my XML parser (to store attributes). I wanted to write a pure functional hashtable that could still interface to the Java implementations but I haven't been able to find a good enough description of a functional hashtable so the one I've got now is kind of a "wanna-be". Here's the code: ;;Hash handling ;;(c) Nic Ferrier - Tapsell-Ferrier Limited ;; ;;a simple Kawa interface to Java hash tables. ;;Could be implemented as native Scheme if necessary. ;; (module-export put) (module-export get) (module-export remove) ;;add a pair to a hashtable ;;=<java.util.Map> the hash ;; ;;This is a slightly recursive version that creates the hash ;;if it doesn't exist ;; (define (put key value hashtable :: <java.util.Map>) :: <java.util.Map> (if (eqv? hashtable #!null) (let ((hashtable (make <java.util.Hashtable>))) (put key value hashtable)) (let ((real-key (invoke key 'to-string))) (invoke hashtable 'put real-key value) hashtable) ) ) ;;get a value specified by key from the hashtable ;;=value assigned to the key or #f if not found ;; (define (get key hashtable :: <java.util.Map>) (let ((got (invoke hashtable 'get key))) (if (eqv? got #!null) #f got) ) ) ;;remove a value specified by key from the hashtable ;;=value removed or #f if not found ;; (define (remove key hashtable :: <java.util.Map>) (let ((removed (invoke hashtable 'remove key))) (if (eqv? removed #!null) #f got) ) ) Harsh or fair critisism appreciated. My intention is for this to be a "this is how it might be done" and for some discussion to improve it. The improved hashtable can then be adopted into BRL? Nic Ferrier |