aboutsummaryrefslogtreecommitdiff
path: root/lib.macros.lisp
blob: bf0bdccca291ae54fc18e819f2d755fd3289c685 (plain)
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
;;; lib.macros.lisp - 2025-02-09

;; Copyright (C) 2025 Aryadev Chavali

;; 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 Version 2 for
;; details.

;; You may distribute and modify this code under the terms of the GNU General
;; Public License Version 2, which you should have received a copy of along with
;; this program.  If not, please go to <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Helpful macros for usage throughout the project.

;;; Code:

(in-package :lib.macros)

(defmacro --> (&rest functions)
  "Lexically bind current form as `_' for use in the next form, returning the
  result of the last form.

i.e.

(--> (a1 a2...) (b1 b2...) (c1 c2...)) =
(let* ((_ (a1 a2 ...))
       (_ (b1 b2 ...))
       (_ (c1 c2 ...)))
    _ )

Also includes transformer where symbols are considered unary functions i.e.
(--> x y) <-> (--> x (y _)).
"
  (if (null functions)
      nil
      (let ((assignment-forms
              (loop :for f :in functions
                    :for canon-f = (if (symbolp f) (list f 'lib.macros:_) f)
                    :collect `(lib.macros:_ ,canon-f))))
        `(let* ,assignment-forms
           lib.macros:_))))

(defmacro ->> (&rest functions)
  "Make current form the last argument of the next form, returning the last
  form.

i.e.
(->> (a1 a2...) (b1 b2...) (c1 c2...)) == (c1 c2 ... (b1 b2 ... (a1 a2 ...)))

Also includes transformer where fun in functions that are symbols are considered
unary functions.

Like the `|>' operator in Ocaml."
  (if (null functions)
      nil
      (loop :with acc = (car functions)
            :for func :in (cdr functions)
            :for canon-func = (if (symbolp func) (list func) func)
            :do (setq acc (append canon-func (list acc)))
            :finally (return acc))))