Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 129 lines (99 sloc) 3.18 kb
5378653 sroccaserra Added tools.el
authored
1 ;;
2 ;; Various tool functions & macros
3 ;;
4
5 (defmacro comment (&rest body)
6 "Ignores body, yields nil"
7 nil)
8
9 ;;;;;;;
10 ;; Math
11
12 (defun mean (values)
13 (/ (reduce '+ values)
14 (float (length values))))
15
16 (defun square (x)
17 (* x x))
18
19 (defun variance (values)
20 (- (->> values (mapcar 'square) mean)
21 (square (mean values))))
22
23 ;;;;;;;;;;;;
24 ;; Sequences
25
26 (defun sequence (maybe-seq)
27 "Returns the value wrapped in a sequence if it is not a sequence already."
28 (if (sequencep maybe-seq) maybe-seq
29 (list maybe-seq)))
30
31 (defun random-elt (sequence)
32 (elt sequence
33 (-> sequence length random)))
34
35 (defun seq-difference (lseq rseq)
36 (remove-if (lambda (element) (find element rseq :test 'equal))
37 lseq))
38
39 ;;;;;;;;;;
40 ;; Strings
41
42 (defun string-empty-p (str)
3b1da06 sroccaserra small tool changes
authored
43 (if str
44 (string= "" str)
45 t))
5378653 sroccaserra Added tools.el
authored
46
3b1da06 sroccaserra small tool changes
authored
47 (defun string-not-empty-p (str)
48 (not (string-empty-p str)))
5378653 sroccaserra Added tools.el
authored
49
34e771c sroccaserra Added string-blank-p
authored
50 (defun string-blank-p (str)
51 (if (string-empty-p str)
52 t
53 (not (null (string-match "^\\(?:\s*\n\\)*$" str)))))
54
55 (defun string-not-blank-p (str)
56 (not (string-blank-p str)))
57
5378653 sroccaserra Added tools.el
authored
58 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
59 ;; Undestructive alist functions
60
61 (defun alist-get (alist key &optional default)
62 (or (assoc-default key alist)
63 default))
64
65 (defun alist-remove (alist key)
66 "Doesn't change the original alist, returns a new one instead."
67 (remove-if (lambda (x) (equal key (car x)))
68 alist))
69
70 (defun alist-set (alist key value)
71 "Doesn't change the original alist, returns a new one instead."
72 (cons (cons key value) (alist-remove alist key)))
73
74 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
75 ;; Clojure's Trush operators
76
a4b7271 sroccaserra added a failing test to document anonymous function failure in trush
authored
77
78 ;; (defun -> (&rest args)
79 ;; (reduce (lambda (x y) (y x))
80 ;; (sequence args)))
81
a309645 sroccaserra Added nil safe combinators
authored
82 (defmacro -> (x &optional form &rest more)
83 (cond ((not (null more))
84 `(-> (-> ,x ,form) ,@more))
85 ((not (null form))
86 (if (sequencep form)
87 `(,(first form) ,x ,@(rest form))
88 (list form x)))
89 (t x)))
90
91 (defmacro ->> (x form &rest more)
92 (cond ((not (null more)) `(->> (->> ,x ,form) ,@more))
93 (t (if (sequencep form)
94 `(,(first form) ,@(rest form) ,x)
95 (list form x)))))
96
97 (defmacro -?> (x form &rest more)
98 (cond ((not (null more)) `(-?> (-?> ,x ,form) ,@more))
99 (t (if (sequencep form)
100 `(if (null ,x) nil
101 (,(first form) ,x ,@(rest form)))
102 `(if (null ,x) nil
103 ,(list form x))))))
104
105 (defmacro -?>> (x form &rest more)
106 (cond ((not (null more)) `(-?>> (-?>> ,x ,form) ,@more))
107 (t (if (sequencep form)
108 `(if (null ,x) nil
109 (,(first form) ,@(rest form) ,x))
110 `(if (null ,x) nil
111 ,(list form x))))))
5378653 sroccaserra Added tools.el
authored
112
113 ;;;;;;;;;;;;;;;;;;;;
114 ;; Functional tools
115
116 (defmacro partial (f &rest args)
117 `(lambda (&rest more)
118 (apply ',f ,@args more)))
119
120 (defmacro lexdef (name args &rest body)
121 "Defun with lexically-scoped parameters. Could also be called lexical-defun."
122 `(defun ,name ,args
123 (lexical-let ,(->> args
124 (remove-if (partial equal '&rest))
125 (mapcar (lambda (arg) (list arg arg))))
126 ,@body)))
127
128 (provide 'tools)
Something went wrong with that request. Please try again.