Re: [RFC] alist-let: let-like alist destructuring syntax
siiky 13 Aug 2022 23:49 UTC
Hi all,
Breaking another long silence.
I've been thinking on and off about a more efficient `alist-values`
(that Marc suggested) but nothing I can implement comes to mind.
The best idea I got was the following (assuming an alist `al` and N
variables/keys): initialize a vector of length N with each variable's
default value (assume #f for now). Traversing `al`, for each key/value,
if the key is one of the wanted ones then set the value in the key's
position of the vector. Of course, only the value of the first
occurrence should be kept, if any. In the end it's just a matter of
(apply values (vector->list vec)).
Something like this:
(define (alist-values al . keys)
(define (key-index k) ???)
(define vec (make-vector (length keys)))
(define (wanted-key? k)
(and (member k keys)
(not (vector-ref vec (key-index k)))))
(for-each
(lambda (kv)
(let ((k (car kv))
(v (cdr kv)))
(when (wanted-key? k)
(vector-set! vec (key-index k) v))))
al)
(apply values (vector->list vec)))
The problem is I have no idea how to associate a vector index to a
key... This is as far as I can go for now.
PS: I changed repos recently, it's on SourceHut now. It's still on
GitHub (in case anyone prefers that) which I'll keep up to date if I
don't forget.
PPS: Added a tiny usage example and some clearer docs (I couldn't
remember anymore off the top of my head the purpose of each clause...)
[0]: https://git.sr.ht/~siiky/alist-let
siiky