s c h e m a t i c s : c o o k b o o k

/ Cookbook.ApplicableHashTables

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Applicable Hash Tables

Problem

You want to apply hash-table objects directly to look up or associate keys with values.

Solution

You can rely on structures as procedures to wrap hash-tables:

(module hash mzscheme
  (require (lib "match.ss")
           (lib "contract.ss"))
  (require-for-syntax (lib "cut.ss" "srfi" "26"))

  (define-values (struct:hash make-hash hash? hash-rec-ref hash-rec-set!) 
    (make-struct-type 'hash #f 2 0 #f null #f 1))

  (define (hash . args)
    (define h (apply make-hash-table args))
    (make-hash h
      (case-lambda
        [(key) (hash-table-get h key)]
        [(key value) (hash-table-put! h key value)])))

  (define (prim hash) (hash-rec-ref hash 0))

  (define-syntax (provide-prims stx)
    (define (stx-prepend str stx)
      (datum->syntax-object stx
                            (string->symbol
                             (string-append str (symbol->string (syntax-e stx))))))
    (syntax-case stx ()
      [(_ prims ...)
       (let* ([prims (syntax->list #'(prims ...))]
              [mz-prims (map (cut stx-prepend "hash-table-" <>) prims)]
              [my-prims (map (cut stx-prepend "hash-" <>) prims)]
              [defs (map (lambda (my mz)
                           #`(begin
                               (define (#,my hash . args)
                                 (apply #,mz (prim hash) args))
                               (provide #,my)))
                         my-prims mz-prims)])
         #`(begin #,@defs))]))

  (provide-prims copy count for-each get
                 iterate-first iterate-key iterate-next iterate-value
                 map put! remove!)

  (provide/contract
    [hash ((symbols 'weak 'equal) . -> . hash?)]
    [hash? (any/c . -> . boolean?)]))

And then use it:

> (require hash)
> (define h (hash 'equal))
> (h "foo" 2)
> (h "foo")
2
> (hash-for-each h (lambda (k v) (printf "~a: ~a~n" k v)))
foo: 2

Discussion


Comments about this recipe

Contributors

-- DanielSilva - 05 Feb 2008

CookbookForm
TopicType: Recipe
ParentTopic: StructureRecipes
TopicOrder: 999

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest