Skip to content

Commit

Permalink
#4 obvious(?) things for elisp
Browse files Browse the repository at this point in the history
  • Loading branch information
nicferrier committed Oct 8, 2014
1 parent 1423696 commit 016d2ba
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
25 changes: 24 additions & 1 deletion shadchen.el
Original file line number Diff line number Diff line change
Expand Up @@ -1499,8 +1499,31 @@ the matching expression from the body."
(list ,@patterns)))))


(provide 'shadchen)
;; A few obvious EmacsLisp extras

(defun shadchen/extract (key type)
"Return a func to extract KEY from TYPE.
TYPE is either `:alist' or `:plist'."
(lambda (kvlist)
(case type
(:plist (plist-get kvlist key))
(:alist (cdr-safe (assoc key kvlist))))))

(defpattern plist (&rest kv-pairs)
`(and ,@(loop for (k v . rest) on kv-pairs by #'cddr
collect
`(funcall (shadchen/extract ,k :plist) ,v))))

(defpattern alist (&rest kv-pairs)
(cl-labels ((alist-get (key)
(lambda (alist)
(cdr (assoc key alist)))))
`(and ,@(loop for (k v . rest) on kv-pairs by #'cddr
collect
`(funcall (shadchen/extract ,k :alist) ,v)))))


(provide 'shadchen)

;;; shadchen.el ends here
21 changes: 20 additions & 1 deletion tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,23 @@
(setf (symbol-function 'test-product) nil)

(test-product (list 1 2 3))
(eq (get-recur-sigil-for 'x) (get-recur-sigil-for 'x))
(eq (get-recur-sigil-for 'x) (get-recur-sigil-for 'x))


;; Alists and Plists

(assert
(equal
(match
'(:one 1 :two 2 :three 3)
((plist :two a) a))
2)) ; because that's the value of :two in the plist

(assert
(equal
(match
'((a . 1)(b . 2)(c . 3))
((alist 'c a) a))
3)) ; because that's the value of 'c in the alist

;;

0 comments on commit 016d2ba

Please sign in to comment.