This repository was archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathfunction.lisp
More file actions
180 lines (155 loc) · 3.87 KB
/
function.lisp
File metadata and controls
180 lines (155 loc) · 3.87 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
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
(import core/prelude ())
(defun slot? (symb)
"Test whether SYMB is a slot. For this, it must be a symbol, whose
contents are `<>`.
### Example
```cl
> (slot? '<>)
out = true
> (slot? 'not-a-slot)
out = false
```"
(and (symbol? symb) (= (.> symb "contents") "<>")))
(defmacro cut (&func)
"Partially apply a function FUNC, where each `<>` is replaced by an
argument to a function. Values are evaluated every time the resulting
function is called.
### Example
```cl
> (define double (cut * <> 2))
> (double 3)
out = 6
```"
(let [(args '())
(call '())]
(for-each item func
(if (slot? item)
(with (symb (gensym))
(push! args symb)
(push! call symb))
(push! call item)))
`(lambda ,args ,call)))
(defmacro cute (&func)
"Partially apply a function FUNC, where each `<>` is replaced by an
argument to a function. Values are evaluated when this function is
defined.
### Example
```cl
> (define double (cute * <> 2))
> (double 3)
out = 6
```"
(let ((args '())
(vals '())
(call '()))
(for-each item func
(with (symb (gensym))
(push! call symb)
(if (slot? item)
(push! args symb)
(push! vals `(,symb ,item)))))
`(let ,vals (lambda ,args ,call))))
(defmacro -> (x &funcs)
"Chain a series of method calls together. If the list contains `<>`
then the value is placed there, otherwise the expression is invoked
with the previous entry as an argument.
### Example
```cl
> (-> '(1 2 3)
. (map succ <>)
. (map (cut * <> 2) <>))
out = (4 6 8)
```"
(with (res x)
(for-each form funcs
(let* [(symb (gensym))
(body (if (and (list? form) (any slot? form))
(map (lambda (x) (if (slot? x) symb x)) form)
`(,form ,symb)))]
(set! res `((lambda (,symb) ,body) ,res))))
res))
(defun invokable? (x)
"Test if the expression X makes sense as something that can be applied
to a set of arguments.
### Example
```cl
> (invokable? invokable?)
out = true
> (invokable? nil)
out = false
> (invokable? (setmetatable {} { :__call (lambda (x) (print! \"hello\")) }))
out = true
```"
(or (function? x)
(and (table? x)
(table? (getmetatable x))
(invokable? (.> (getmetatable x) :__call)))))
(defun compose (f g)
"Return the pointwise composition of functions F and G.
### Example:
```cl
> ((compose (cut + <> 2) (cut * <> 2))
. 2)
out = 6
```"
(if (and (invokable? f)
(invokable? g))
(lambda (x) (f (g x)))
nil))
(defun comp (&fs)
"Return the pointwise composition of all functions in FS.
### Example:
```cl
> ((comp succ (cut + <> 2) (cut * <> 2))
. 2)
out = 7
```"
(reduce compose (lambda (x) x) fs))
(defun id (x)
"Return the value X unmodified.
### Example
```cl
> (map id '(1 2 3))
out = (1 2 3)
```"
x)
(defun as-is (x)
"Return the value X unchanged.
### Example
```cl
> (map as-is '(1 2 3))
out = (1 2 3)
```"
x)
(defun const (x)
"Return a function which always returns X. This is equivalent to the
`K` combinator in SK combinator calculus.
### Example
```cl
> (define x (const 1))
> (x 2)
out = 1
> (x \"const\")
out = 1
```"
(lambda () x))
(defun call (x key &args)
"Index X with KEY and invoke the resulting function with ARGS.
### Example
```cl
> (define tbl { :add + })
> (call tbl :add 1 2 3)
out = 6
```"
(apply (.> x key) args))
(defun self (x key &args)
"Index X with KEY and invoke the resulting function with X and ARGS.
### Example
```cl
> (define tbl { :get (lambda (self key) (.> self key))
. :x 1
. :y 2 })
> (self tbl :get :x)
out = 1
```"
(apply (.> x key) x args))