reddit / emacs

Why doesn't `assq` show a value from my alist?

zu0107 · 2022-12-08 13:13
## SOLVED! OK, I'm dumb. I found the answer myself within 30 minutes. The culprit was `make-symbol`, which creates an *uninterned* symbol which is distinct from regular symbols that are checked using `assq`. Now I changed `make-symbol` to `intern` in `rangho/create-color-matrix-alist` and everything works fine! ## Original post I wanted to try using `let-alist` to simplify my colorscheme code. Basically, I have an alist of colors and its hex values, and I have a list of HSL percentage points. I want to create a new "matrix" alist where each colors are lightened by the "lighten list". This is the code: ;; Map color values to their representative colors ;; Note: An `i' next to color name represents that ;; it is the "intense" variant of the color (defvar rangho/color-name-alist `((color/fg . ,wal/foreground) (color/bg . ,wal/background) (color/cursor . ,wal/cursor) (color/black . ,wal/color0) (color/blacki . ,wal/color8) (color/red . ,wal/color1) (color/redi . ,wal/color9) (color/green . ,wal/color2) (color/greeni . ,wal/color10) (color/yellow . ,wal/color3) (color/yellowi . ,wal/color11) (color/blue . ,wal/color4) (color/bluei . ,wal/color12) (color/magenta . ,wal/color5) (color/magentai . ,wal/color13) (color/cyan . ,wal/color6) (color/cyani . ,wal/color14) (color/white . ,wal/color7) (color/whitei . ,wal/color15)) "Association list to map wal colors to friendly names.") ;; List of shades to generate for each colors (defvar rangho/color-shade-list (number-sequence -20 20 5) "List of percentage values to lighten/darken the original colors.") ;; --snip-- ;; Color shade creation utility (defun rangho/create-color-matrix-alist (color-alist lighten-list) "Create a matrix of colors based on a color alist and list of shades." ;; For each color association... (append color-alist (mapcan (lambda (original-color) ;; And then for each lighten value... (mapcar (lambda (lighten-value) ;; Create a list of... (cons (make-symbol (concat (symbol-name (car original-color)) (if (>= lighten-value 0) "+" "") (number-to-string lighten-value))) (color-lighten-hex (cdr original-color) lighten-value))) lighten-list)) color-alist))) ;; Create a color matrix (defvar rangho/color-matrix-alist (rangho/create-color-matrix-alist rangho/color-name-alist rangho/color-shade-list)) Of course, I checked the values in `rangho/color-matrix-alist` and they don't seem to have any structural problem. I tried investigating further. I ran the expanded `let-alist` macro manually, then I found out that the following code returns `nil`. (assq 'color/fg rangho/color-matrix-alist) ; => (color/fg . "#rrggbb") (good) (assq 'color/fg+0 rangho/color-matrix-alist) ; => nil (wtf???) In fact, all values prefixed generated by `rangho/create-color-matrix-alist` (and thus not present in the original `rangho/color-name-alist`) gives `nil` when applied to `assq`. It isn't a compile-time evaluation issue as well, since the result was the same when I tried running the same code in a scratch buffer AFTER a clean init script. What would be the issue? How do I fix this? Thanks in advance! Edit: `color-lighten-hex` is a helper I defined elsewhere, and it basically `color-hex-to-rgb |> color-rgb-to-hsl |> color-lighten-hsl |> color-hsl-to-rgb |> color-rgb-to-hex`.
[deleted] · 2022-12-08 23:33
[deleted]
github-alphapapa · 2022-12-09 09:35
The great thing about this post is that you wrote very clearly, and you explained what you did and how you arrived at the solution. It's a model for all Emacs users to follow. Good job. FYI, see also: `alist-get`, `map-elt`, `pcase`, and `pcase-let`.