Deutsch   English   Français   Italiano  
<v74noh$13hi0$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp
Subject: Re: Please critique my code
Date: Tue, 16 Jul 2024 03:00:37 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <v74noh$13hi0$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 16 Jul 2024 05:00:37 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="b361cae61b07268813c127883801cddd";
	logging-data="1164864"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/erF4kyQln7/j+rBVEZ3Ky"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:FXXimQ7+e6gVlITVvSoJ72ifDNQ=
Bytes: 1903

Pascal J. Bourguignon wrote:

> (defun hashtable-to-array (hashtable)
>   "Convert a hash table to a 2d array.
> Keys are stored in the column 0, and values in the column 1"
>   (let ((array (make-array (list (hash-table-count hashtable) 2)))
>         (i 0))
>      (maphash (lambda (k v)
>                   (setf (aref array i 0) k
>                         (aref array i 1) v)
>                   (incf i))
>               hashtable)
>      array))
> 
> C/USER[35]> (HASHTABLE-TO-ARRAY #S(HASH-TABLE :test eql (one . 1) (two . 2) (thr
> ee . 3)))
> #2A((THREE 3) (TWO 2) (ONE 1))

Gauche Scheme

(use gauche.array)
(use gauche.collection)
(use util.match)

(define (hashtable->array ht)
  (rlet1 ary (make-array (shape  0 (size-of ht)  0 2))
    (for-each
      (match-lambda* [((k . v) i)
        (array-set! ary i 0 k)
        (array-set! ary i 1 v)])
      ht
      (lrange 0))))

(pretty-print-array
  (hashtable->array 
    (hash-table 'eqv? '(one . 1) '(two . 2) '(three . 3))))

#,(<array> (0 3 0 2)
three 3
  two 2
  one 1
  )