source: project/wiki/eggref/4/clojurian @ 27259

Last change on this file since 27259 was 27259, checked in by Moritz Heidkamp, 9 years ago

clojurian: Document version 0.0.2

File size: 5.2 KB
Line 
1[[tags: egg]]
2== clojurian
3
4A collection of modules providing syntax and utility functions
5inspired by [[http://clojure.com|Clojure]].
6
7[[toc:]]
8=== Overview
9
10Currently this egg provides two modules: {{clojurian-syntax}} and
11{{clojurian-atom}}.
12
13=== Modules
14
15==== clojurian-syntax
16
17<syntax>(doto val (proc args ...) ...)</syntax>
18
19Inserts {{val}} as the first argument of each {{(proc args ...)}}
20clause (i.e. right after {{proc}}). Returns {{val}}. This is useful
21e.g. for mutating a record on initialization.
22
23Example:
24
25<enscript highlight="scheme">
26(define boolean-vector
27  (doto (make-vector 100)
28        (vector-fill! #t 0 50)
29        (vector-fill! #f 50)))
30=>
31
32(define boolean-vector
33  (let ((vec (make-vector 100)))
34    (vector-fill! vec #t 0 50)
35    (vector-fill! vec #f 50)
36    vec))
37</enscript>
38
39
40
41<syntax>(-> val (proc args ...) ...)</syntax>
42<syntax>(->* val (proc args ...) ...)</syntax>
43
44Inserts {{val}} as the first argument of the first {{(proc args ...)}}
45clause. The resulting form is then inserted as the first argument of
46the following {{proc}} form and so on. This is known as the ''thrush
47combinator''. The starred version ({{->*}}) is multi value aware,
48i.e. each form's return values are spliced into the argument list of
49the successing form.
50
51Single value example:
52
53<enscript highlight="scheme">
54
55(-> 10 (- 2) (/ 5) (* 3))
56
57=>
58
59(* (/ (- 10 2) 5) 3)
60</enscript>
61
62Multi value example:
63
64<enscript highlight="scheme">
65
66(->* (values 1 2) (list))
67
68=>
69
70(receive args (values 1 2) (apply list args))
71</enscript>
72
73
74<syntax>(->> val (proc args ...) ...)</syntax>
75<syntax>(->>* val (proc args ...) ...)</syntax>
76
77Works just like {{->}} and {{->*}} only that the forms are inserted at
78the end of each successive clause's argument list.
79
80Example:
81
82<enscript highlight="scheme">
83(->> (iota 10)
84     (map add1)
85     (fold + 0)
86     (print))
87
88=>
89
90(print
91 (fold + 0
92  (map add1
93   (iota 10))))
94</enscript>
95
96
97<syntax>(if-let (var val) then else)</syntax>
98
99Equivalent to {{(let ((var val)) (if var then else))}}.
100
101
102<syntax>(if-let* ((x1 y1) (x2 y2) ...) then else)</syntax>
103
104Equivalent to {{(or (and-let* ((x1 y1) (x2 y2) ...) then) else)}}.
105
106
107==== clojurian-atom (experimental)
108
109This module provides an implementation of
110[[http://clojure.org/atoms|Clojure's atom reference type]]. It is in
111an experimental state right now and only provides the very basic
112functionality, e.g. validators and watches are still missing. Note
113that atoms only work with immutable data structures so they can't be
114used to coordinate updates of record slots, for example.
115
116
117<procedure>(atom value)</procedure>
118
119Returns and atom with the given initial {{value}}.
120
121
122<procedure>(atom? x)</procedure>
123
124Checks whether {{x}} is an atom.
125
126
127<procedure>(atom-value atom)</procedure>
128
129Returns the current value of {{atom}}.
130
131
132<procedure>(compare-and-set! atom old new)</procedure>
133
134Atomically sets the value of {{atom}} to {{new}} if its current value
135is {{eq?}} to the given {{old}} value. Returns {{#t}} if the update
136was successful and {{#f}} if not.
137
138
139<procedure>(swap! atom proc . args)</procedure>
140
141Atomically swaps the value of {{atom}} to be {{(apply proc (atom-value
142atom) args)}}. Because {{proc}} may be called multiple times it must
143be free of side-effects. Returns the new value of {{atom}}.
144
145
146<procedure>(reset! atom value)</procedure>
147
148Sets the value of {{atom}} to {{value}}. Returns {{value}}.
149
150
151=== About this egg
152
153==== Source
154
155The [[https://bitbucket.org/DerGuteMoritz/clojurian|source code is
156hosted at Bitbucket]]. Feel free to fork it and send pull requests
157there.
158
159==== Authors
160
161[[/users/moritz-heidkamp|Moritz Heidkamp]]
162
163Original {{doto}} implementation by Martin DeMello
164
165==== Version history
166
167; 0.0.2 : Add {{if-let}}, {{if-let*}} and {{clojurian-atom}}
168; 0.0.1 : Initial release
169
170==== License
171
172
173  Copyright (c) 2012, Moritz Heidkamp
174  All rights reserved.
175 
176  Redistribution and use in source and binary forms, with or without
177  modification, are permitted provided that the following conditions are
178  met:
179 
180  Redistributions of source code must retain the above copyright
181  notice, this list of conditions and the following disclaimer.
182 
183  Redistributions in binary form must reproduce the above copyright
184  notice, this list of conditions and the following disclaimer in the
185  documentation and/or other materials provided with the distribution.
186 
187  Neither the name of the author nor the names of its contributors may
188  be used to endorse or promote products derived from this software
189  without specific prior written permission.
190 
191  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
192  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
193  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
194  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
195  COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
196  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
197  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
198  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
200  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
201  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
202  OF THE POSSIBILITY OF SUCH DAMAGE.
203
Note: See TracBrowser for help on using the repository browser.