* Re: a vector of colors
2022-08-10 1:58 a vector of colors jgart
@ 2022-09-05 16:37 ` zimoun
0 siblings, 0 replies; 2+ messages in thread
From: zimoun @ 2022-09-05 16:37 UTC (permalink / raw)
To: jgart, Guix Help
Hi,
A bit late. :-)
On mar., 09 août 2022 at 20:58, jgart <jgart@dismail.de> wrote:
> Why was this defined as a vector instead of a list?
>
> (define %colors
> ;; See colortbl.h in Graphviz.
> #("red" "magenta" "blue" "cyan3" "darkseagreen"
> "peachpuff4" "darkviolet" "dimgrey" "darkgoldenrod"))
and you mean replace ’pop-color’ by,
--8<---------------cut here---------------start------------->8---
(define (pop-color hint)
"Return a Graphviz color based on HINT, an arbitrary object."
(let ((index (hash hint (length %colors))))
(list-ref %colors index)))
--8<---------------cut here---------------end--------------->8---
but I guess ’list-ref’ is slower than ’vector-ref’; as specified by the
documentation:
Vectors are sequences of Scheme objects. Unlike lists, the
length of a vector, once the vector is created, cannot be
changed. The advantage of vectors over lists is that the time
required to access one element of a vector given its position
(synonymous with index), a zero-origin number, is constant,
whereas lists have an access time linear to the position of the
accessed element in the list.
However, the difference is probably not visible here.
Cheers,
simon
^ permalink raw reply [flat|nested] 2+ messages in thread