Skip to content

Commit 6846061

Browse files
author
Paul Nathan
committed
First cut on the snippets for bug #21.
1 parent 851f409 commit 6846061

File tree

2 files changed

+151
-84
lines changed

2 files changed

+151
-84
lines changed

examples~snippets.md

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
% Snippets
2+
%
3+
%
4+
5+
One of the great resources we can have as developers is snippets and
6+
example code to understand how the idiom of a language is spoken and
7+
written.
8+
9+
10+
11+
### Defining a function:
12+
13+
~~~~Commonlisp
14+
(defun function-name (param1 param2 &key (keyword1 default-value))
15+
;; implicit progn
16+
)
17+
~~~~
18+
19+
### Defining a method on a type:
20+
21+
22+
~~~~Commonlisp
23+
(defmethod method-name ((object class-name)
24+
;; implicit progn
25+
)
26+
~~~~
27+
28+
29+
### Defining a constant:
30+
31+
Note that the convention is that constants are surrounded with +
32+
33+
```Commonlisp
34+
(defconstant +name+ value "docstring")
35+
```
36+
37+
### Defining a global variable:
38+
39+
Note that the convention is that globals are surrounded with *
40+
41+
~~~~Commonlisp
42+
(defparameter *name* value "docstring")
43+
~~~~
44+
45+
46+
### Creating local variables.
47+
48+
~~~~Commonlisp
49+
(let ((variable1 value-form)
50+
(variable2 value-again))
51+
;; implicit progn where variable[12] are valid
52+
)
53+
54+
~~~~
55+
56+
### LOOP
57+
58+
LOOP is a contentious form in Common Lisp: some people love its
59+
imperative style, others hate it. Regardless, its really handy! Here
60+
are my favorite LOOPs
61+
62+
~~~~Commonlisp
63+
64+
(loop for i from 0 upto 10
65+
collect i)
66+
67+
(loop for i from 0 upto 10
68+
do
69+
(side-effect i))
70+
71+
(loop for ele in list
72+
collect
73+
(operate-on ele))
74+
75+
(loop for ele in list
76+
collect
77+
(operate-on ele))
78+
~~~~
79+
80+
### Lambda functions
81+
82+
The lambda functions is an anonymous function, i.e., unnamed.
83+
84+
Here we map over `numeric-list` and increment each element in it by 1
85+
with `INCF`, returning the incremented list.
86+
87+
~~~~Commonlisp
88+
89+
(mapcar
90+
#'(lambda (x)
91+
(incf x))
92+
numeric-list)
93+
94+
~~~~
95+
96+
### Macro
97+
98+
Note that Lisp macros should be used with care: they read source code
99+
and emit source code.
100+
101+
~~~~Commonlisp
102+
(defmacro with-resource ((resource) &body body)
103+
(allocate-resource ,resource)
104+
(unwind-protect
105+
(progn
106+
,@body)
107+
(deallocate-resource ,resource)))
108+
~~~~
109+
110+
See [UNWIND-PROTECT](http://www.lispworks.com/documentation/HyperSpec/Body/s_unwind.htm)
111+
for details on this very useful form.
112+
113+
### Writing a text file
114+
115+
116+
```Commonlisp
117+
(defun write-file (filename data)
118+
(with-open-file (stream
119+
filename
120+
:direction :output
121+
:if-exists :supersede
122+
:if-does-not-exist :create)
123+
(write-sequence
124+
data
125+
stream)))
126+
```
127+
128+
### Reading a text file
129+
130+
```Commonlisp
131+
132+
(defun read-file (filename)
133+
"Reads `filename` as a sequence of unsigned 8-bit bytes, no
134+
encoding"
135+
(with-open-file (fin filename
136+
:direction :input
137+
:if-does-not-exist :error)
138+
(let ((seq (make-array (file-length fin)
139+
:fill-pointer t)))
140+
(setf (fill-pointer seq)
141+
(read-sequence seq fin))
142+
seq)))
143+
144+
```
145+
146+
Please feel free to contribute your examples or library information to
147+
this page! Send in those pull requests and file those bugs!
148+
149+

initial~abcs.md

Lines changed: 2 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -107,87 +107,5 @@ Popular libraries that the author have not used include:
107107
* usocket - TCP socket libary
108108

109109

110-
## Snippets
111-
112-
Common templates for modifying to your needs.
113-
114-
### Defining a function:
115-
116-
~~~~Commonlisp
117-
(defun function-name (param1 param2 &key (keyword1 default-value))
118-
;; implicit progn
119-
)
120-
~~~~
121-
122-
### Defining a method on a type:
123-
124-
125-
~~~~Commonlisp
126-
(defmethod method-name ((object class-name)
127-
;; implicit progn
128-
)
129-
~~~~
130-
131-
132-
### Defining a constant:
133-
134-
Note that the convention is that constants are surrounded with +
135-
136-
```Commonlisp
137-
(defconstant +name+ value "docstring")
138-
```
139-
140-
### Defining a global variable:
141-
142-
Note that the convention is that globals are surrounded with *
143-
144-
~~~~Commonlisp
145-
(defparameter *name* value "docstring")
146-
~~~~
147-
148-
149-
### Creating local variables.
150-
151-
~~~~Commonlisp
152-
(let ((variable1 value-form)
153-
(variable2 value-again))
154-
;; implicit progn where variable[12] are valid
155-
)
156-
157-
~~~~
158-
159-
### Writing a text file
160-
161-
162-
```Commonlisp
163-
(defun write-file (filename data)
164-
(with-open-file (stream
165-
filename
166-
:direction :output
167-
:if-exists :supersede
168-
:if-does-not-exist :create)
169-
(write-sequence
170-
data
171-
stream)))
172-
```
173-
174-
### Reading a text file
175-
176-
```Commonlisp
177-
178-
(defun read-file (filename)
179-
"Reads `filename` as a sequence of unsigned 8-bit bytes, no
180-
encoding"
181-
(with-open-file (fin filename
182-
:direction :input
183-
:if-does-not-exist :error)
184-
(let ((seq (make-array (file-length fin)
185-
:fill-pointer t)))
186-
(setf (fill-pointer seq)
187-
(read-sequence seq fin))
188-
seq)))
189-
190-
```
191-
192-
Please feel free to contribute your examples or library information to
193-
this page! Send in those pull requests and file those bugs!
110+
For snippets and code samples, please see the Snippets in the Examples
111+
submenu.

0 commit comments

Comments
 (0)