All pastes #2073228 Raw Edit

Stuff

public text v1 · immutable
#2073228 ·published 2011-06-01 13:25 UTC
rendered paste body
;#ex1: i wanted to replace if new 'attr' matched 'attrs-orig' item, or add if not.
;..i get the feeling that this isn't 'the lisp/elisp/functional programming' way though :(
(require 'cl)
(defun replace-or-add-by-car-key(plist pitem) 
  (let ((l 1) (match nil))
    (let ((nlist (mapcar (lambda (item) (message "%d. %s vs %s" l pitem item) (setq l (+ l 1))
			  (if match item (if (equal (car pitem) (car item))
					      (progn (setq match t) pitem)
						    item))) plist) ))
(if match nlist (add-to-list 'nlist pitem t)))))

(let ((attrs '((foreground-color . "#e4e4ef") (background-color . "#181818") (background-mode . dark) (cursor-color . "#ffdd33") (mouse-color . "#ffdd33")))
     (attrs-orig  '((background-color . "#181818") (background-mode . light))))
     (dolist (attr attrs) (setq attrs-orig (replace-or-add-by-car-key
					     attrs-orig attr)) (print attrs-orig)))

;#ex2: this looks cleaner, doesn't keep order though (not that it matters here)
(let ((attrs '((foreground-color . "#e4e4ef") (background-color . "#181818") (background-mode . dark) (cursor-color . "#ffdd33") (mouse-color . "#ffdd33")))
     (attrs-orig  '((background-color . "#181818") (background-mode . light))))
     (dolist (attr attrs)
       (delete* attr attrs-orig :test '(lambda (item1 item2) (if (equal (car item1) (car item2)) t)))
       (add-to-list 'attrs-orig attr))
     (print attrs-orig))
     
;#ex3: there's bound to be a one liner to do this?!?!