-
Notifications
You must be signed in to change notification settings - Fork 4
/
xwl-subr.el
72 lines (55 loc) · 2.34 KB
/
xwl-subr.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
;;; xwl-subr.el --- Some scheme extensions for Emacs
;; Copyright (C) 2009 William Xu
;; Author: William Xu <[email protected]>
;; Version: 0.1
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
;; MA 02110-1301, USA.
;;; Commentary:
;; Put this file into your load-path and the following into your
;; ~/.emacs:
;; (require 'xwl-subr)
;; FIXME: elisp doesn't support tail recursive, does it?
;;; Code:
(defun xs-flatmap (procedure sequence)
"map and append. e.g., map: '((a b) (c)) => flatmap: '(a b c)."
(xs-accumulate 'append '() (mapcar procedure sequence)))
(defun xs-accumulate (op initial sequence)
(if (null sequence)
initial
(funcall op (car sequence)
(xs-accumulate op initial (cdr sequence)))))
;; ;; These two are useful for customizing, so that the result is not affected even
;; ;; being evaled for multiple times.
;; (defun xs-uniq-prepend (&rest sequences)
;; (let ((r (nreverse sequences)))
;; (unless (symbolp (car r))
;; (error "Last element should be a symbol"))
;; (mapc (lambda (path) (add-to-list (car r) path))
;; (xs-flatmap 'identity (cdr r)))
;; (symbol-value (car r))))
;; (defun xs-uniq-append (&rest sequences)
;; (let ((r (nreverse sequences)))
;; (unless (symbolp (car r))
;; (error "Last element should be a symbol"))
;; (mapc (lambda (path) (add-to-list (car r) path t))
;; (xs-flatmap 'identity (cdr r)))
;; (symbol-value (car r))))
(defun xs-find-first (predicate sequence)
(cond ((null sequence)
nil)
((funcall predicate (car sequence))
(car sequence))
(t
(xs-find-first predicate (cdr sequence)))))
(provide 'xwl-subr)
;;; xwl-subr.el ends here