File: user-guide.mmd

package info (click to toggle)
cl-metabang-bind 20230508.git0819642-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 244 kB
  • sloc: lisp: 1,607; makefile: 2
file content (377 lines) | stat: -rw-r--r-- 10,948 bytes parent folder | download | duplicates (4)
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
{comment http://metabang.com/unclogit/?p=154}

{include resources/guide-header.md}
{set-property title "metabang-bind user guide"}

{set-property html yes}
{set-property title "metabang-bind | Guide to the perplexed"}
{set-property style-sheet user-guide}
{set-property docs-package metabang-bind}

# metabang-bind user guide

{table-of-contents :start 2 :depth 3}

## Introduction

`bind` combines _let_, _destructuring-bind_, _multiple-value-bind_
*and** a whole lot more into a single form. It has two goals:

1. reduce the number of nesting levels

2. make it easier to understand all of the different forms of
destructuring and variable binding by unifying the multiple forms of
syntax and reducing special cases.

`bind` is extensible. It handles the traditional multiple-values,
destructuring, and let-forms as well as property-lists, classes, and
structures. Even better, you can create your own binding forms to make
your code cleaner and easier to follow (for others _and_ yourself!).

Simple bindings are as in _let*_. Destructuring is done if the first
item in a binding is a list. Multiple value binding is done if the
first item in a binding is a list and the first item in the list is
the keyword ':values'.


{remark
### Summary

<table>
<tr>
<td>symbol</td><td>`x` or `(x 45)`</td><td>`let`</td>
<td>array</td><td>`(#(a b) #(4 5))`</td><td>`let`</td>
</tr>
</table>
}

## Some examples

Bind mimics let in its general syntax:

    (bind (&rest bindings) <body>)
    
where each `binding` can either be an symbol or a list. If the binding is an atom, then this atom will be bound to nil within the body (just as in let). If it is a list, then it will be interpreted depending on its first form.

    (bind (a
           (...)) 
      ...)

### Bind as a replacement for let

You can use `bind` as a direct replacement for `let*`:

    (bind ((a 2) b)
      (list a b))
    => (2 nil)

As in `let*`, atoms are initially bound to `nil`.

### Bind with multiple-values and destructuring

Suppose we define two silly functions:

    (defun return-values (x y)
      (values  x y))

    (defun return-list (x y)
      (list x y))

How could we use bind for these:

    (bind (((:values a b) (return-values 1 2))
           ((c d) (return-list 3 4)))
      (list a b c d))
    => (1 2 3 4)

Note that `bind` makes it a little easier to ignore variables you don't care about. 
Suppose I've got a function `ijara` that returns 3 values and I happen to need only 
the second two. Using `destructuring-bind`, I'd write:

    (destructuring-bind (foo value-1 value-2) 
        (ijira)
      (declare (ignore foo))
      ...)

With `bind`, you use `nil` or `_` in place of a variable name and it will make up
temporary variables names and add the necessary declarations for you.

    (bind (((_ value-1 value-2) (ijira)))
      ...)


{anchor property-list-bindings}

### Bind with property lists

A property-list or `plist` is a list of alternating keywords and values. Each keyword specifies a property name; each value specifies the value of that name.

    (setf plist
      '(:start 368421722 :end 368494926 :flavor :lemon
        :content :ragged)

You can use `getf` to find the current value of a property in a list (and `setf` to change them). The optional third argument to `getf` is used to specify a default value in case the list doesn't have a binding for the requested property already.

    (let ((start (getf plist :start 0))
          (end (getf plist :end))
          (fuzz (getf plist :fuzziness 'no)))
      (list start end fuzz))
    => (368421722 368494926 no)
   
The binding form for property-lists is as follows:

    (:plist property-spec*)
    
where each property-spec is an atom or a list of up to three elements:

* atoms bind a variable with that name to
a property with the same name (converting the name to a keyword in order to do the lookup). 

* lists with a single element are treated like atoms.

* lists with two elements
specify the variable in the first and the name of the
property in the second. 

* Lists with three elements use
the third element to specify a default value (if the 
second element is #\_, then the property name is taken
to be the same as the variable name).

Putting this altogether we can code the above let statement as:

    (bind (((:plist (start _ 0) end (fuzz fuzziness 'no))
      plist))
    => (list start end fuzz))

(which takes some getting used to but has the advantage of brevity).

{anchor structure-bindings}

### Bind with structures

Structure fields are accessed using a concatenation of the structure's
`conc-name` and the name of the field. Bind therefore needs to know
two things: the conc-name and the field-names. The binding-form looks
like

    (:structure <conc-name> structure-spec*)

where each `structure-spec` is an atom or list with two elements:

* an atom specifies both the name of the variable to which the structure field is bound and the field-name in the structure.

* a list has the variable name as its first item and the structure field name as its second. 

So if we have a structure like:

    (defstruct minimal-trout
      a b c)

    (setf trout (make-minimal-trout :a 2 :b 3 :c 'yes))

We can bind these fields using:

    (bind (((:structure minimal-trout- (my-name a) b c)
            trout))
      (list my-name b c))
    => (2 3 yes)

{anchor class-bindings}

### Bind with classes

You can read the slot of an instance with an accessor (if one exists) or by using slot-value{footnote Note that if an accessor exists, it will generally be much faster than slot-value because CLOS is able to cache information about the accessor and the instance.}. Bind also provides two slot-binding mechanisms: `:slots` and `:accessors`. Both look the same:

    (:slots slot-spec*)
    (:accessors accessor-spec*)
    
Where both slot-spec and accessor-spec can be atoms or lists with two elements.

* an atom tells bind to use it as the name of the new variable _and_ to treat this name as the name of the slot or the name of the accessor, respectively.

* If the specification is a list, then bind will use the first item as the variable's name and the second item as the slot-name or accessor. 

Support we had a class like:

    (defclass wicked-cool-class ()
      ((a :initarg :a :accessor its-a)
       (b :initarg :b :accessor b) 
       (c :initarg :c :accessor just-c)))

If we don't mind using the slot-names as variable names, then we can use the simplest form of `:slots`:

    (bind (((:slots a b c) 
    	(make-instance 'wicked-cool-class
    		       :a 1 :b 2 :c 3)))
      (list a b c))
    ==> (1 2 3)

We can also change the names within the context of our bind form:

    (bind (((:slots a b (dance-count c)) 
    	(make-instance 'wicked-cool-class
    		       :a 1 :b 2 :c 3)))
      (list a b dance-count))
    ==> (1 2 3)

Similarly, we can use `:accessors` with variable names that are the same as the accessor names...

    (bind (((:accessors its-a b just-c) 
    	(make-instance 'wicked-cool-class
    		       :a 1 :b 2 :c 3)))
      (list its-a b just-c))
    ==> (1 2 3)

or that are different:

    (bind (((:accessors (a its-a) b (c just-c)) 
    	(make-instance 'wicked-cool-class
    		       :a 1 :b 2 :c 3)))
      (list a b c))
    ==> (1 2 3)

{anchor array-bindings}

### Bind with arrays

Tamas Papp had the idea of letting `bind` handle arrays too. For example, 

    (bind ((#(a b c) #(1 2 3)))
      (list a b c))
    ==> (1 2 3)

One quick method definition and a few unit-tests later and bind does!

### Bind with regular expressions

If you have CL-PPCRE or run with Allegro Common Lisp, you
can use `bind` with regular expressions too. The syntax is

    (:re expression &rest vars) string)

and will bind each grouped item in the expression to the 
corresponding var. For example:

    (bind (((:re "(\\w+)\\s+(\\w+)\\s+(\\d{1,2})\\.(\\d{1,2})\\.(\\d{4})"
    	     fname lname nil month year) "Frank Zappa 21.12.1940"))
      (list fname lname month year))

The body of bind form will be evaluated even if the expression
does not match.

### Bind with `flet` and `labels`

Bind can even be used as a replacement for `flet` and `labels`. 
The syntax is 

    (:flet function-name (arguments*)) definition)

    (:labels function-name (arguments*)) definition)

for example:

    (bind (((:flet square (x)) (* x x)))
      (square 4))
    ==> 16

    (bind (((:labels my-oddp (x))
 	   (cond ((<= x 0) nil)
	 	 ((= x 1) t)
		 (t (my-oddp (- x 2))))))
        (my-oddp 7))
    ==> t

Note that bind currently expands each binding-form into a new context. In
particular, this means that 

    (bind (((:flet x (a)) (* a 2))
           ((:flet y (b)) (+ b 2)))
      ...)

expands as 

    (flet ((x (a) (progn (* a 2)))) 
      (flet ((y (b) (progn (+ b 2))))
        ...))

rather than

    (flet ((x (a) (progn (* a 2)))
           (y (b) (progn (+ b 2))))
        ...)

Generally speaking, this shouldn't make much of a difference.

## `bind` and declarations

`bind` handles declarations transparently by splitting them
up and moving them to the correct place in the expansion. For 
example

    (bind (((:values a b) (foo x))
           (#(d e) (bar y)))
      (declare (type fixnum a d)
    	   (optimize (speed 3)))
      (list a b d e))

becomes
  
    (multiple-value-bind (a b)
        (foo x)
      (declare (type fixnum a) (optimize (speed 3)))
      (let ((#:values-258889 (bar y)))
        (let* ((d (row-major-aref #:values-258889 0))
               (e (row-major-aref #:values-258889 1)))
          (declare (optimize (speed 3)))
          (list a b d e))))

because `bind` knows to keep the variable declarations 
(like `type`) with their variables and to repeat 
other declarations (like `optimize`) at each level. 

`bind` keeps track of variables declarations that are not used.
The configuration variable `*unused-declarations-behavior*` controls
what `bind` does:

{docs *unused-declarations-behavior*}

## More bindings

Since bind is extensible and I'm fallible, there are probably
things bind can do that haven't made it into this guide. Use
the following commands to see what bind can do:

{docs binding-forms}

{docs binding-form-docstring}

{docs binding-form-groups}

{docs binding-form-synonyms}


## `lambda-bind`

Eric Schulte contributed `lambda-bind` (note, he called it
`lambdab` but I dislike abbreviations so...):

{docs lambda-bind}


## Extending bind yourself

Bind's syntax is extensible: the work for each
binding-specification is handled by a generic function. This
means that you can evolve bind to fit your program for
whatever sort of data-structure makes sense for you. To make
a binding form, you can either define a method for `bind-generate-bindings`
or you can use the `defbinding-form` macro.

{docs bind-generate-bindings}

{docs defbinding-form}

There are many more examples included in the source code.

{include resources/guide-footer.md}