Sujet : Re: Please critique my code
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 16. Jul 2024, 05:00:37
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v74noh$13hi0$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
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
)