forked from dbmcclain/LispPlotter
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplotter-macros.lisp
More file actions
69 lines (53 loc) · 1.58 KB
/
Copy pathplotter-macros.lisp
File metadata and controls
69 lines (53 loc) · 1.58 KB
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
(in-package :plotter)
;;; Convenience macros
(defmacro with-color ((pane color) &body body)
`(gp:with-graphics-state (,pane :foreground ,color)
,@body))
(defmacro with-mask ((pane mask) &body body)
`(gp:with-graphics-state (,pane :mask ,mask)
,@body))
;;; Useful macros
(defun raw-mkstr (&rest args)
(with-output-to-string (s)
(dolist (a args)
(princ a s))))
(defun mkstr (&rest args)
(with-standard-io-syntax
(apply 'raw-mkstr args)))
(defun mklist (obj)
(if (listp obj)
obj
(list obj)))
(defun single (arg)
(and
(consp arg)
(null (cdr (the cons arg)))))
(defun last1 (lst)
(car (the cons (last lst))))
(defun curry (fn &rest pref-args)
(lambda (&rest suf-args)
(apply fn (append pref-args suf-args))))
(defun rcurry (fn &rest suf-args)
(lambda (&rest pref-args)
(apply fn (nconc pref-args suf-args))))
(defmacro expanded-curry ((&rest suf-args) f &rest pref-args)
`(lambda ,suf-args
(funcall ,f ,@pref-args ,@suf-args)))
(defun foldl (fn init seq)
"fn should be a function of (accum item)"
(reduce fn seq :initial-value init))
(defun foldr (fn seq init)
"fn should be a function of (item accum)"
(reduce fn seq :from-end t :initial-value init))
(defun compose (&rest fns)
(cond
((null fns) 'identity)
((single fns) (car fns))
((single (rest fns))
(destructuring-bind (fn1 fn2) fns
(lambda (&rest args)
(funcall fn1 (apply fn2 args)))))
(t (let ((fn1 (last1 fns))
(fns (butlast fns)))
(lambda (&rest args)
(foldr 'funcall fns (apply fn1 args)))))))