Skip to content

Instantly share code, notes, and snippets.

@xuchunyang
Created April 21, 2015 15:00
Show Gist options
  • Save xuchunyang/15d9ad46e8e8a3d508dc to your computer and use it in GitHub Desktop.
Save xuchunyang/15d9ad46e8e8a3d508dc to your computer and use it in GitHub Desktop.
elisp-cl-lib-01.el
;;; elisp-cl-lib-01.el ---
;;;
(require 'cl-lib)
;;; Common Lisp Function Argument Lists
(cl-defun foo (a &optional b &key c d (e 17))
(message "%S %S %S %S %S" a b c d e)) ;; => foo
(foo 1 2 :d 3 :c 4) ;; => "1 2 4 3 17"
(foo 1) ;; => "1 nil nil nil 17"
(foo 1 :d 2) ;; => Error
(cl-case (read-char "CHAR: ")
(?a (message "a"))
(?b (message "b"))
((?\r ?\n) (message "RET"))
(t (message "others")))
(cl-loop for buf in (buffer-list)
collect (buffer-file-name buf))
(nil nil nil nil nil "/Users/xcy/wip/helm-zhihu-daily/helm-zhihu-daily.el" "/Users/xcy/wip/emacs-github-dashboard/github-dashboard.el" nil nil nil nil nil ...)
(cl-loop repeat 5 do (insert "hello\n"))
hello
hello
hello
hello
hello
nil
(cl-loop until (eobp) do (what-line) (sit-for 1) (forward-line 1))
(cl-loop for x from 1 to 100
for y = (* x x)
until (>= y 729)
finally return (list x (= y 720)))
(27 nil)
(setq i 'happy)
happy
(cl-loop for i from 1 to 10 do (message "%S" i))
nil
i
happy
(cl-loop for x to 10 collect x)
(0 1 2 3 4 5 6 7 8 9 10)
(cl-loop for x in '(1 2 3 4 5) collect (* x x))
(1 4 9 16 25)
(cl-loop for x in (number-sequence 1 5) by 'cddr collect (* x x))
(1 9 25)
(cl-loop for x on '(1 2 3 4) collect x)
((1 2 3 4)
(2 3 4)
(3 4)
(4))
(let ((l '(1 2 3 4 5)))
(cl-loop for x in-ref l do (cl-incf x))
l)
(2 3 4 5 6)
(cl-loop with l = '(1 2 3 4 5)
for x in-ref l
do (cl-incf x)
;; finally return l
collect x)
(2 3 4 5 6)
(cl-loop for x across [1 2 3 4 5]
when (and (> x 1) (< x 5))
collect x)
(2 3 4)
(cl-loop for x across "hello world"
collect x)
(cl-loop for x across "abcde"
concat (string x))
"abcde"
(cl-loop for name in '(fred sue alice joe june)
for kids in '((bob ken) () () (kris sunshine) ())
collect name
append kids)
(fred bob ken sue alice joe kris sunshine june)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment