git.fiddlerwoaroof.com
Browse code

feat(emacs): add local packages, and a port of data-lens

Ed Langley authored on 01/05/2020 21:39:18
Showing 2 changed files
... ...
@@ -186,6 +186,15 @@
186 186
          (load-path (list* local-configs git-configs load-path)))
187 187
     (load conf-file)))
188 188
 
189
+(defun fwoar/load-local-packages ()
190
+  (interactive)
191
+  (mapc 'package-install-file
192
+        (directory-files (format "%s/%s" *dotfiles-repo* "emacs.d/packages/")
193
+                         t ".*[.]el")))
194
+
195
+(unless (package-installed-p 'fwoar-functional-utils)
196
+  (fwoar/load-local-packages))
197
+
189 198
 (load-package-configuration 'evil)
190 199
 
191 200
 (use-package projectile
192 201
new file mode 100644
... ...
@@ -0,0 +1,156 @@
1
+;;; fwoar-functional-utils.el --- more functional utilities for emacs -*- lexical-binding: t; -*-
2
+
3
+;; Copyright (C) 2020 Edward Langley
4
+
5
+;; Author: Edward Langley <fwoar@elangley.org>
6
+;; Version: 0.0.1
7
+;; Keywords: fp,combinators
8
+;; URL: https://fwoar.co
9
+
10
+;; This program is free software; you can redistribute it and/or modify
11
+;; it under the terms of the GNU General Public License as published by
12
+;; the Free Software Foundation, either version 3 of the License, or
13
+;; (at your option) any later version.
14
+
15
+;; This program is distributed in the hope that it will be useful,
16
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
+;; GNU General Public License for more details.
19
+
20
+;; You should have received a copy of the GNU General Public License
21
+;; along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
+
23
+;;; Commentary:
24
+
25
+;; Some functional programming utilities
26
+
27
+;;; Code:
28
+
29
+(eval-when (compile load eval)
30
+  (defvar *fwoar/namespaced-funs* ()))
31
+
32
+(cl-defmacro fwoar/def-ns-fun (name (&rest args) &body body)
33
+  (declare (indent defun))
34
+  (let ((namespaced-sym (intern (format "fwoar/%s" name))))
35
+    `(progn
36
+       (cl-pushnew '(,name ,args ,namespaced-sym)
37
+                   *fwoar/namespaced-funs*
38
+                   :test 'equal)
39
+       (cl-defun ,namespaced-sym ,args
40
+         ,@body))))
41
+
42
+(fwoar/def-ns-fun just-after (pred)
43
+  (lexical-let ((state nil))
44
+    (lambda (it)
45
+      (cond
46
+       (state it)
47
+       ((funcall pred it) (setf state t) nil)))))
48
+
49
+;;;###autoload
50
+(cl-defmacro with-unaliased (&body body)
51
+  `(flet ,(loop for (name raw-args namespaced) in *fwoar/namespaced-funs*
52
+                for rest-arg = (cl-find-if (fwoar/just-after
53
+                                            (lambda (it)
54
+                                              (member it '(&rest &body))))
55
+                                           raw-args)
56
+                for args = (cl-remove-if (lambda (it)
57
+                                           (or (eql it rest-arg)
58
+                                               (and (symbolp it)
59
+                                                    (= ?&
60
+                                                       (elt
61
+                                                        (symbol-name it)
62
+                                                        0)))))
63
+                                         raw-args)
64
+
65
+                collect `(,name ,raw-args
66
+                                (,@(if rest-arg
67
+                                       `(apply ',namespaced)
68
+                                     (list namespaced))
69
+                                 ,@(mapcar (lambda (it)
70
+                                             (if (listp it)
71
+                                                 (car it)
72
+                                               it))
73
+                                           args)
74
+                                 ,@(when rest-arg
75
+                                     (list rest-arg)))))
76
+     ,@body))
77
+
78
+(cl-defmacro fwoar/def-combinator (name (seq &rest args) &body body)
79
+  (declare (indent defun))
80
+  `(fwoar/def-ns-fun ,name ,args
81
+     (lambda (,seq)
82
+       ,@body)))
83
+
84
+(fwoar/def-ns-fun iota (count &optional (start 0))
85
+  (cl-loop for x from start
86
+           repeat count
87
+           collect x))
88
+
89
+(fwoar/def-ns-fun applying (f &rest pos-args)
90
+  (lambda (list)
91
+    (apply f (append pos-args list))))
92
+
93
+(fwoar/def-ns-fun on (fun key-fun)
94
+  (lambda (it)
95
+    (funcall fun (funcall key-fun it))))
96
+
97
+(fwoar/def-combinator over (list f &rest args)
98
+  (mapcar (lambda (it)
99
+            (apply f it args))
100
+          list))
101
+
102
+(fwoar/def-combinator filter (list f &rest args)
103
+  (cl-remove-if-not (lambda (it)
104
+                      (apply f it args))
105
+                    list))
106
+
107
+(fwoar/def-combinator zip-with (lists f)
108
+  (apply 'cl-mapcar f lists))
109
+
110
+(fwoar/def-ns-fun element (num)
111
+  (lambda (it)
112
+    (elt it num)))
113
+
114
+(cl-defgeneric fwoar/extract-key (map key)
115
+  (:method ((map hash-table) key)
116
+           (gethash key map))
117
+  (:method ((map list) key)
118
+           (typecase (car map)
119
+             (cons (cdr (cl-assoc key map :test 'equal)))
120
+             (t (loop for (a-key . value) on map by #'cddr
121
+                      when (equal key a-key) do
122
+                      (return (car value)))))))
123
+
124
+(fwoar/def-ns-fun key (key)
125
+  (lambda (map)
126
+    (fwoar/extract-key map key)))
127
+
128
+(comment
129
+ (fwoar/def-ns-fun regex-match (regex)
130
+   (lambda (data)
131
+     (cl-ppcre:scan-to-strings regex data))))
132
+
133
+(fwoar/def-ns-fun include (pred)
134
+  (lambda (seq)
135
+    (cl-remove-if-not pred seq)))
136
+
137
+(fwoar/def-ns-fun exclude (pred)
138
+  (lambda (seq)
139
+    (cl-remove-if pred seq)))
140
+
141
+(fwoar/def-ns-fun pick (selector)
142
+  (lambda (seq)
143
+    (cl-map 'list selector seq)))
144
+
145
+(fwoar/def-ns-fun slice (start &optional end)
146
+  (lambda (it)
147
+    (cl-subseq it start end)))
148
+
149
+(fwoar/def-ns-fun juxt (fun1 &rest r)
150
+  (lambda (&rest args)
151
+    (list* (apply fun1 args)
152
+           (mapcar (lambda (f)
153
+                     (apply f args))
154
+                   r))))
155
+
156
+;;; fwoar-functional-utils.el ends here