[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] Alist versus Hash-table

From: Loïc Faure-Lacroix
Subject: Re: [Chicken-users] Alist versus Hash-table
Date: Wed, 13 Nov 2013 02:44:04 +0400

As Peter said, the Query String format doesn’t prevent you from using multiple time the same key for multiple values. One concrete example would be a form with a couple of checkboxes. A group of checkbox could be named “ingredients”. Each checkbox could have different values. Like Onions, Ananas, Pepperoni… When you select multiple ingredients you should have something like this:


Which should be converted to a alist like this:

((ingredient . ananas) (ingredient . onions) (ingredient . cheese))

As far as I can tell, in python this kind of structure is implemented as a MultiDict. It’s a hash-table that allows multiple values (in a list) for a key. The Query string is one of the rare cases where the alist makes more sense but a multi-dict could be implemented easily using the hash-table. 

(define (multi-hash-ref htable key)
   (let ((val (hash-table-ref htable key)))
      (if (or (eq? val #f) (null? val))
         (car val))))

(define (multi-hash-ref-all htable key)
   (hash-table-ref htable key))

Here, we assume that each value for a key is supposed to be a list. The list can be empty.

Loïc Faure-Lacroix
Sent with Airmail

On November 13, 2013 at 1:39:52 AM, Peter Bex (address@hidden) wrote:

On Tue, Nov 12, 2013 at 04:34:51PM -0500, John Cowan wrote:
> > Also, in some cases duplicate entries really mean something different
> > (for example in alist->uri query attribute mappings and such).
> I don't understand this example.

((a . "foo") (a . "bar")) => ?a=foo;a=bar

A HTTP server might interpret this query string differently from ?a=foo


reply via email to

[Prev in Thread] Current Thread [Next in Thread]