-
Notifications
You must be signed in to change notification settings - Fork 0
/
fp.el
399 lines (349 loc) · 13.1 KB
/
fp.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
;;; fp.el --- Collection of combinators for Emacs Lisp -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Karim Aziiev <[email protected]>
;; Author: Karim Aziiev <[email protected]>
;; URL: https://github.com/KarimAziev/fp
;; Keywords: lisp, extensions
;; Version: 2.2.0
;; Package-Requires: ((emacs "26.1"))
;; This file is NOT part of GNU Emacs.
;; 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. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Collection of combinators for Emacs Lisp
;;; Code:
(defun fp--expand (init-fn)
"If INIT-FN is a non-quoted symbol, add a sharp quote.
Otherwise, return it as is."
(setq init-fn (macroexpand init-fn))
(if (symbolp init-fn)
`(#',init-fn)
`(,init-fn)))
(defmacro fp-pipe (&rest functions)
"Return a left-to-right composition from FUNCTIONS.
The first argument may have any arity; the remaining arguments must be unary."
(declare (debug t)
(pure t)
(side-effect-free t))
(let ((args (make-symbol "args")))
`(lambda (&rest ,args)
,@(let ((init-fn (pop functions)))
(list
(seq-reduce
(lambda (acc fn)
`(funcall ,@(fp--expand fn) ,acc))
functions
`(apply ,@(fp--expand init-fn) ,args)))))))
(defmacro fp-compose (&rest functions)
"Return a right-to-left composition from FUNCTIONS.
The last function may have any arity; the remaining arguments must be unary."
(declare (debug t)
(pure t)
(side-effect-free t))
`(fp-pipe ,@(reverse functions)))
(defmacro fp-or (&rest functions)
"Expand to a unary function that calls FUNCTIONS until first non-nil result.
Return that first non-nil result without calling the remaining functions.
If all functions returned nil, the result will be also nil."
(declare (debug t)
(pure t)
(side-effect-free t))
(let ((it (make-symbol "it")))
`(lambda (,it)
(or
,@(mapcar (lambda (v)
`(funcall ,@(fp--expand v) ,it))
functions)))))
(defmacro fp-and (&rest functions)
"Return a unary function that calls FUNCTIONS until one of them yields nil.
If all functions return non-nil, return the last such value."
(declare (debug t)
(pure t)
(side-effect-free t))
(let ((it (make-symbol "it")))
`(lambda (,it)
(and
,@(mapcar (lambda (v)
`(funcall ,@(fp--expand v) ,it))
functions)))))
(defmacro fp-partial (fn &rest args)
"Return a partial application of a function FN to left-hand ARGS.
ARGS is a list of the last N arguments to pass to FN. The result is a new
function that does the same as FN, except that the last N arguments are fixed
at the values with which this function was called."
(declare (side-effect-free t))
(let ((pre-args (make-symbol "pre-args")))
`(lambda (&rest ,pre-args)
,(car (list
`(apply ,@(fp--expand fn)
(append (list ,@args) ,pre-args)))))))
(defmacro fp-rpartial (fn &rest args)
"Return a partial application of a function FN to right-hand ARGS.
ARGS is a list of the last N arguments to pass to FN. The result is a new
function which does the same as FN, except that the last N arguments are fixed
at the values with which this function was called."
(declare (side-effect-free t))
(let ((pre-args (make-symbol "pre-args")))
`(lambda (&rest ,pre-args)
,(car (list
`(apply ,@(fp--expand fn)
(append ,pre-args (list ,@args))))))))
(defmacro fp-converge (combine-fn &rest functions)
"Return a function to apply COMBINE-FN with the results of branching FUNCTIONS.
If the first element of FUNCTIONS is a vector, it will be used instead.
Example:
\(funcall (fp-converge concat [upcase downcase]) \"John\").
\(funcall (fp-converge concat upcase downcase) \"John\")
Result: \"JOHNjohn\"."
(let ((args (make-symbol "args")))
`(lambda (&rest ,args)
(apply
,@(fp--expand combine-fn)
(list
,@(mapcar (lambda (v)
`(apply ,@(fp--expand v) ,args))
(if (vectorp (car functions))
(append (car functions) nil)
functions)))))))
(defmacro fp-use-with (combine-fn &rest functions)
"Return a function with the arity of length FUNCTIONS.
Call every branching function with an argument at the same index,
and finally, COMBINE-FN will be applied to the supplied values.
Example:
\(funcall (fp-use-with concat [upcase downcase]) \"hello \" \"world\")
If first element of FUNCTIONS is vector, it will be used instead:
\(funcall (fp-use-with + [(fp-partial 1+) identity]) 2 2)
=> Result: 5
\(funcall (fp-use-with + (fp-partial 1+) identity) 2 2)
=> Result: 5
=> Result: \"HELLO world\"."
(let ((args (make-symbol "args")))
`(lambda (&rest ,args)
(apply
,@(fp--expand combine-fn)
(list
,@(seq-map-indexed (lambda (v idx)
`(funcall ,@(fp--expand v)
(nth ,idx ,args)))
(if (vectorp (car functions))
(append (car functions) nil)
functions)))))))
(defmacro fp-when (pred fn)
"Return a function that calls FN if the result of calling PRED is non-nil.
Both PRED and FN are called with one argument.
If the result of PRED is nil, return the argument as is."
(declare
(indent defun))
(let ((arg (make-symbol "arg")))
`(lambda (,arg)
(if
(funcall ,@(fp--expand pred) ,arg)
(funcall ,@(fp--expand fn) ,arg)
,arg))))
(defmacro fp-unless (pred fn)
"Return a unary function that invokes FN if the result of calling PRED is nil.
Accept one argument and pass it both to PRED and FN.
If the result of PRED is non-nil, return the argument as is."
(let ((arg (make-symbol "arg")))
`(lambda (,arg)
(if (funcall ,@(fp--expand pred) ,arg)
,arg
(funcall ,@(fp--expand fn) ,arg)))))
(defmacro fp-const (value)
"Return a function that always returns VALUE.
This function accepts any number of arguments but ignores them."
(declare (pure t)
(side-effect-free error-free))
(let ((arg (make-symbol "_")))
`(lambda (&rest ,arg) ,value)))
(defmacro fp-ignore-args (fn)
"Return a function that invokes FN without args.
This function accepts any number of arguments but ignores them."
(declare
(indent defun))
(let ((arg (make-symbol "_")))
`(lambda (&rest ,arg)
(funcall ,@(fp--expand fn)))))
(defmacro fp-cond (&rest pairs)
"Return a function that expands a list of PAIRS to cond clauses.
Every pair should be either:
- a vector of [predicate transformer],
- a list of (predicate transformer).
The predicate can also be t.
All of the arguments to function are applied to each of the predicates in turn
until one returns a \"truthy\" value, at which point fn returns the result of
applying its arguments to the corresponding transformer."
(declare (pure t)
(indent defun)
(side-effect-free error-free))
(setq pairs (mapcar (lambda (it)
(if (listp it)
(apply #'vector it)
it))
pairs))
(let ((args (make-symbol "args")))
`(lambda (&rest ,args)
(cond ,@(mapcar (lambda (v)
(list (if (eq (aref v 0) t) t
`(apply ,@(fp--expand (aref v 0)) ,args))
`(apply ,@(fp--expand (aref v 1)) ,args)))
pairs)))))
(defmacro fp-not (fn)
"Return a function that negates the result of function FN."
`(fp-compose not ,fn))
(defun fp-t (&rest _)
"Do nothing and return t.
This function accepts any number of arguments, but ignores them."
t)
(defun fp-nil (&rest _)
"Do nothing and return nil.
This function accepts any number of arguments but ignores them."
nil)
(defmacro fp-ignore-errors-partial (fn &rest args)
"Apply FN with ARGS ignoring errors."
(declare
(indent defun))
`(lambda (&rest right-args)
(condition-case nil
(progn
(apply ,@(fp--expand fn)
(append (list ,@args) right-args)))
(error nil))))
(defmacro fp-ignore-errors-rpartial (fn &rest args)
"Return a function that takes in arguments FN and ARGS.
This macro returns a lambda function that takes &REST PRE-ARGS.
Within the lambda function, apply FN to PRE-ARGS and ARGS. Should FN throw an
error, return nil instead."
(declare
(indent defun))
`(lambda (&rest pre-args)
(condition-case nil
(progn
(apply ,@(fp--expand fn)
(append pre-args (list ,@args))))
(error nil))))
(defun fp-rpartial-ignore-errors (fn &rest args)
"Return a function that is a partial application of FN to ARGS.
ARGS is a list of the first N arguments to pass to FN.
The result is a new function which does the same as FN,
except that:
- the first N arguments are fixed at the values with which this function
was called
- if an error occurs, return nil."
(lambda (&rest args2)
(condition-case nil
(progn
(apply fn
(append args args2)))
(error nil))))
(defun fp-partial-ignore-errors (fn &rest args)
"Return a function that is a partial application of FN to ARGS.
ARGS is a list of the first N arguments to pass to FN.
The result is a new function which does the same as FN,
except that:
- the first N arguments are fixed at the values with which this function
was called
- if an error occurs, return nil."
(lambda (&rest args2)
(condition-case nil
(progn
(apply fn
(append args args2)))
(error nil))))
(when (version<= "28.1" emacs-version)
(eval-when-compile
(require 'shortdoc nil t)
(when (fboundp 'define-short-documentation-group)
(define-short-documentation-group fp
"Combinators"
(fp-pipe
:eval (fp-pipe split-string upcase)
:eval (funcall (fp-pipe upcase split-string) "some string"))
(fp-compose
:eval (fp-compose split-string upcase)
:eval (funcall (fp-compose split-string upcase) "some string"))
(fp-partial
:eval (funcall (fp-rpartial > 3) 2))
(fp-rpartial
:eval (funcall
(fp-rpartial plist-get :name) '("John" :age 30)))
(fp-and
:eval (funcall (fp-and numberp 1+) 30))
(fp-or
:eval (fp-or floatp integerp)
:eval (funcall (fp-or floatp integerp) 3)
:eval (seq-filter
(fp-or numberp stringp)
'("a" "b" (0 1 2 3 4) "c" 34 (:name "John"
:age 30))))
(fp-converge
:eval "(funcall
(fp-converge concat [upcase downcase])
\"John\")"
:eval "(funcall
(fp-converge concat upcase downcase)
\"John\")")
(fp-when
:eval
"(funcall (fp-when
(fp-compose (fp-partial < 4) length)
(fp-rpartial substring 0 4))
\"long string\")")
(fp-unless
:eval (funcall (fp-unless zerop (fp-partial / 2)) 0))
(fp-const
:eval (funcall (fp-const 2) 4))
(fp-ignore-args
:eval "(funcall (fp-ignore-args
(lambda () (message \"No arguments\")))
4)")
(fp-use-with
:eval "(funcall
(fp-use-with concat [upcase downcase])
\"hello \" \"world\")"
:eval
"(funcall
(fp-use-with + [(fp-partial 1+) identity])
2 2)")
(fp-cond
:eval
"(funcall (fp-cond
[stringp identity]
[symbolp symbol-name]
[integerp number-to-string]
[floatp number-to-string]
[t (fp-partial format \"%s\")])
2)"
:eval "(funcall
(fp-cond
(stringp identity)
(symbolp symbol-name)
(integerp number-to-string)
(floatp number-to-string)
(t (fp-partial format \"%s\")))
2)")
(fp-not
:eval
(funcall (fp-not stringp) 4))
(fp-nil
:eval (fp-nil t)
:eval (fp-nil 23)
:eval (fp-nil))
(fp-t
:eval (fp-t nil)
:eval (fp-t)
:eval (fp-t 23))))))
(provide 'fp)
;;; fp.el ends here
;; Local Variables:
;; checkdoc-verb-check-experimental-flag: nil
;; End: