• Re: Please critique my code

    From B. Pym@21:1/5 to Pascal J. Bourguignon on Tue Jul 16 03:00:37 2024
    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
    )

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)