This repository has been archived by the owner on Oct 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
list-ext.el
69 lines (55 loc) · 1.99 KB
/
list-ext.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
;;; list-ext.el ---
;; Copyright (C) 2006 Dmitry Galinsky <dima dot exe at gmail dot com>
;; Authors: Dmitry Galinsky <dima dot exe at gmail dot com>,
;;; License
;; 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 2
;; of the License, 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; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
;;; Code:
(require 'cl)
(defun list-ext/uniq (list)
"Return a list of unique elements."
(let ((result '()))
(dolist (elem list)
(when (not (member elem result))
(push elem result)))
(nreverse result)))
(defun list-ext/group-by (list func &optional sort)
(let ((res '()))
(dolist (it list)
(let* ((key (funcall func it))
(res-key (assoc key res)))
(if res-key
(push it (cadr res-key))
(add-to-list 'res (list key (list it))))))
(setq res
(mapcar
#'(lambda(it)
(list (car it) (nreverse (cadr it))))
(nreverse res)))
(when sort
(setq res (sort* res sort :key 'car)))
res))
(defun list-ext/options-value (key list)
(cadr (memq key list)))
(defalias 'opt-val 'list-ext/options-value)
(defun list-ext/swap-tail (key list)
(let* ((list-len (length list))
(tail (member key list))
(beg (- list-len (length tail)))
(i 0))
(when tail
(while (not (zerop (+ beg 1)))
(add-to-list 'tail (nth i list) t)
(decf beg)
(incf i))
tail)))
(provide 'list-ext)