Skip to content

Instantly share code, notes, and snippets.

@haxney
Created November 18, 2012 05:17
Show Gist options
  • Save haxney/4103741 to your computer and use it in GitHub Desktop.
Save haxney/4103741 to your computer and use it in GitHub Desktop.
Better than "Understanding OOP"
(defvar systems '((usg-unix-v "UNIX" "good")
;; `string-equal' converts symbols to string automagically
((lambda (sys) (string-equal "gnu/linux" sys)) "UNIX" "good")
("^windows" "Windows" "bad")
(darwin "Mac OS" "far superior")))
(defun assoc-match (key alist)
"Return non-nil if KEY matches the `car' of an element of ALIST.
The `car' of each element of ALIST can be a:
string - Compared to KEY with `string-match'
function - Passed KEY as an argument. It should return non-nil if
it considers KEY to match
symbol - Compared to KEY with `eq'
The value is actually the first element of ALIST whose `car' matches KEY."
(when (consp alist)
(let* ((elt (car alist))
(elt-cmp (car elt)))
(if (cond
;; `string-match' accepts only strings
((stringp elt-cmp) (string-match elt-cmp (format "%s" key)))
((functionp elt-cmp) (funcall elt-cmp key))
((symbolp elt-cmp) (eq elt-cmp key)))
elt
(assoc-match key (cdr alist))))))
(let ((box (assoc-test system-type systems)))
(if box
(message "This is a %s box and therefore %s." (cadr box) (caddr box))
(message "This is not a box.")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment