aboutsummaryrefslogtreecommitdiff
path: root/README.org
blob: 7b0b80bd62e368b29db05df24ff848e93848e0ec (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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
* derrida

`DERRIDA` is a library for destructuring common "object-like" structures in Common Lisp.

More specifically, it provides forms that act like `WITH-SLOTS` for "object-like" or "map-like" data structure.

It provides forms for digging into association lists, property lists,
and struct instances and CLOS class instances.

** Examples

*** Property Lists

#+begin_src lisp

(let ((pl 
        (list 'name \"colin\" :age 40 :|currentJob| :crumb-bum)))
  (with-plist (name (age :age) (job :|currentJob|)) pl 
    (setf age (1+ age)) 
    (format t \"~a the ~a had a birthday, and is now ~a years old~%\" 
            name job age) 
    pl))

#+end_src

The above would print out:
: colin the CRUMB-BUM had a birthday, and is now 41 years old

And would return 
: (NAME \"colin\" :AGE 41 :|currentJob| :CRUMB-BUM)"  

*** Nested Property Lists

#+begin_src lisp

> (defvar *pl* '(:x (:a 10) :y (:b 20))
*PL*

> (with-keypaths ((a :x :a) 
                  (b :y :b) 
                  (d :y :d))  *pl*
    (incf a) 
    (setf d (* a b)))
220

> *pl*
(:X (:A 11) :Y (:D 220 :B 20))"
#+end_src


*** Nested Instances


#+begin_src lisp
> (defclass point () ((x :initform 0) (y :initform 0)))
#<STANDARD-CLASS DERRIDA::POINT>

> (defclass in-space () 
     ((location :initform (make-instance 'point))))
#<STANDARD-CLASS DERRIDA::IN-SPACE>

> (defvar *thing* (make-instance 'in-space))
*THING*

> (with-slot-paths 
      ((x 'location 'x) 
       (y 'location 'y)) *thing* 
    (setf x 10 
          y -30))

-30

> (describe (slot-value *thing* 'location))
#<POINT {100781DB53}>
  [standard-object]

Slots with :INSTANCE allocation:
  X                              = 10
  Y                              = -30"
#+end_src