Fixed the code formatting.
All checks were successful
Build & Deploy / build-or-sth (push) Successful in 22s
All checks were successful
Build & Deploy / build-or-sth (push) Successful in 22s
This commit is contained in:
parent
50fc14dc0b
commit
cb4f65c81b
@ -133,20 +133,20 @@ Now we can write functions for defining:
|
|||||||
- the constructor, with a function that takes a name,
|
- the constructor, with a function that takes a name,
|
||||||
and a list of slots, and returns a form that will define
|
and a list of slots, and returns a form that will define
|
||||||
the constructor when evaluated:
|
the constructor when evaluated:
|
||||||
```cl
|
```cl
|
||||||
(defun constructor (name slots)
|
(defun constructor (name slots)
|
||||||
`(defun ,(constructor-name name) ,slots
|
`(defun ,(constructor-name name) ,slots
|
||||||
(list ,@slots)))
|
(list ,@slots)))
|
||||||
```
|
```
|
||||||
- the slot accessors. This one will return a list of forms,
|
- the slot accessors. This one will return a list of forms,
|
||||||
that will each define an accessor for one of the slots.
|
that will each define an accessor for one of the slots.
|
||||||
```cl
|
```cl
|
||||||
(defun accessors (name slots)
|
(defun accessors (name slots)
|
||||||
(loop for slot in slots
|
(loop for slot in slots
|
||||||
for i upfrom 0 collect
|
for i upfrom 0 collect
|
||||||
`(defun ,(accessor-name name slot) (obj)
|
`(defun ,(accessor-name name slot) (obj)
|
||||||
(nth i obj))))
|
(nth i obj))))
|
||||||
```
|
```
|
||||||
- the slot setters. Note that these won't actually
|
- the slot setters. Note that these won't actually
|
||||||
be used by the users of our library. In common lisp, we don't
|
be used by the users of our library. In common lisp, we don't
|
||||||
really use separate functions for setters. For example,
|
really use separate functions for setters. For example,
|
||||||
@ -157,20 +157,20 @@ use `(setf (point-x my-point-object) some-value)` to set it to `some-value`.
|
|||||||
setter function. This provides a unified interface for accessing fields,
|
setter function. This provides a unified interface for accessing fields,
|
||||||
no matter what the underlying implementation is. Anyway, here's
|
no matter what the underlying implementation is. Anyway, here's
|
||||||
my function for defining the setters:
|
my function for defining the setters:
|
||||||
```cl
|
```cl
|
||||||
(defun setters (name slots)
|
(defun setters (name slots)
|
||||||
(loop for slot in slots
|
(loop for slot in slots
|
||||||
for i upfrom 0 collect
|
for i upfrom 0 collect
|
||||||
`(defun ,(setter-name name slot) (obj val)
|
`(defun ,(setter-name name slot) (obj val)
|
||||||
(setf (nth i obj) val))))
|
(setf (nth i obj) val))))
|
||||||
```
|
```
|
||||||
- finally, the aforementioned `defsetf` forms:
|
- finally, the aforementioned `defsetf` forms:
|
||||||
```cl
|
```cl
|
||||||
(defun setfers (name slots)
|
(defun setfers (name slots)
|
||||||
(loop for slot in slots collect
|
(loop for slot in slots collect
|
||||||
`(defsetf ,(accessor-name name slot)
|
`(defsetf ,(accessor-name name slot)
|
||||||
,(setter-name name slot))))
|
,(setter-name name slot))))
|
||||||
```
|
```
|
||||||
|
|
||||||
As Common Lisp is a highly interactive language, we can try each
|
As Common Lisp is a highly interactive language, we can try each
|
||||||
of these functions in the REPL with very little effort:
|
of these functions in the REPL with very little effort:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user