forked from mvolkmann/mvolkmann.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClojure.html
More file actions
2346 lines (2340 loc) · 64.2 KB
/
Copy pathClojure.html
File metadata and controls
2346 lines (2340 loc) · 64.2 KB
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR.html1/DTD.html1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Clojure</title>
<link rel="stylesheet" type="text/css" href="../common.css"/>
</head>
<body>
<h2>Clojure</h2>
<h3><a name="Contents">Quick Links</a></h3>
<p>
<a href="http://clojure.org/api/">Clojure API</a>
from Rich Hickey<br />
<a href="http://clj-doc.s3.amazonaws.com/tmp/doc-1116/index.html">clj-doc</a>
from Mark McGranaghan<br />
<a href="http://tinyurl.com/clojure-classes/">class diagram</a> from Chris Houser<br />
<a href="http://en.wikibooks.org/wiki/Clojure_Programming">Wikibooks Clojure Programming</a><br />
<a href="http://en.wikibooks.org/wiki/Learning_Clojure">Wikibooks Learning Clojure</a><br />
<a href="http://en.wikibooks.org/wiki/Clojure_Programming/Examples/API_Examples">Wikibooks Clojure API Examples</a><br />
<a href="http://clojure-euler.wikispaces.com/">Project Euler Code</a><br />
<a href="ClojureCodingGuidelines.html">Clojure Coding Guidelines</a><br />
<a href="ClojureSnake.html">Clojure Snake Game</a><br />
<a href="#builtins">Builtins</a>
<a href="#categories">Categories</a>
<a href="#shortcuts">Shortcuts</a>
<a href="#syntax">Syntax</a>
</p>
<h3><a name="categories">Categories</a></h3>
<p>
The following table categories Clojure functions.
</p>
<table border="1">
<tr>
<th>Category</th>
<th>Functions/Macros</th>
</tr>
<tr>
<td>arrays</td>
<td>
aclone
aget
alength
amap
areduce
aset
aset-boolean
aset-byte
aset-char
aset-double
aset-float
aset-int
aset-long
aset-short
double-array
float-array
int-array
into-array
long-array
make-array
</td>
</tr>
<tr>
<td>bindings</td>
<td>
binding
declare
def
defonce
let
if-let
with-local-vars
</td>
</tr>
<tr>
<td>bitwise operations</td>
<td>
bit-and
bit-and-not
bit-clear
bit-flip
bit-not
bit-or
bit-set
bit-shift-left
bit-shift-right
bit-test
bit-xor
</td>
</tr>
<tr>
<td>bytecode</td>
<td>
compile
gen-class
gen-interface
</td>
</tr>
<tr>
<td>Clojure code access</td>
<td>
load
load-file
load-reader
load-string
loaded-libs
require
source
use
</td>
</tr>
<tr>
<td>conditional logic</td>
<td>
cond
condp
if
if-let
when
when-not
</td>
</tr>
<tr>
<td>conversions</td>
<td>
bigdec
bigint
byte
char
double
float
int
long
num
short
</td>
</tr>
<tr>
<td>exception handling</td>
<td>
assert
cast
finally
throw
throw-if
</td>
</tr>
<tr>
<td>functions and methods</td>
<td>
comp
complement
constantly
declare
defn
defn-
defmethod
defmulti
fn
partial
</td>
</tr>
<tr>
<td>input/output</td>
<td>
file-seq
flush
line-seq
newline
pr
pr-str
prn
printf
println
with-in-str
with-open
</td>
</tr>
<tr>
<td>iteration</td>
<td>
dotimes
for
iterate
loop
recur
while
</td>
</tr>
<tr>
<td>Java interop.</td>
<td>
.
..
add-classpath
bean
comparator
construct-proxy
enumeration-seq
get-proxy-class
import
iterator-seq
memfn
new
proxy
set!
</td>
</tr>
<tr>
<td>list operations</td>
<td>
list
list*
</td>
</tr>
<tr>
<td>logical expressions and predicates</td>
<td>
=
==
<
<=
>
>=
and
associative?
class?
coll?
compare
decimal?
delay?
distinct?
empty?
even?
every?
false?
float?
fn?
identical?
ifn?
instance?
integer?
isa?
keyword?
list?
macro?
map?
neg?
nil?
not
not=
not-any?
not-empty?
not-every?
number?
odd?
or
pos?
reversible?
seq?
set?
some
vector?
zero?
</td>
</tr>
<tr>
<td>macros</td>
<td>
definline
defmacro
macro?
macroexpand
macroexpand-1
</td>
</tr>
<tr>
<td>map operations</td>
<td>
array-map
assoc
assoc-in
contains?
dissoc
find
get
hash-map
key
keys
max-key
merge
min-key
merge-with
pmap
select-keys
sorted-map
sorted-map-by
update-in
vals
</td>
</tr>
<tr>
<td>math</td>
<td>
+
-
*
/
dec
inc
max
min
quot
rand
rand-int
rem
</td>
</tr>
<tr>
<td>namespaces</td>
<td>
alias
all-ns
create-ns
find-ns
find-var
in-ns
namespace
ns
ns-aliases
ns-imports
ns-interns
ns-name
ns-publics
ns-refers
ns-resolve
ns-unalias
ns-unmap
</td>
</tr>
<tr>
<td>quoting and unquoting</td>
<td>
eval
quote
</td>
</tr>
<tr>
<td>reflection and metadata</td>
<td>
ancestors
bases
class
doc
find-doc
instance?
isa?
meta
ns-publics
ns-resolve
parents
with-meta
</td>
</tr>
<tr>
<td>regular expressions</td>
<td>
re-find
re-groups
re-matcher
re-pattern
re-seq
</td>
</tr>
<tr>
<td>sequence operations to retrieve a single item</td>
<td>
ffirst
first
nth
second
last
peek
</td>
</tr>
<tr>
<td>sequence operations to retrieve multiple item</td>
<td>
butlast
drop
drop-last
drop-while
filter
frest
pop
nthrest
rest
rseq
take
take-nth
take-while
</td>
</tr>
<tr>
<td>sequence operations to do other things</td>
<td>
apply
cache-seq
concat
conj
cons
count
cycle
distinct
doall
dorun
doseq
empty
<i>flatten</i>
fnseq
iterate
interleave
interpose
into
lazy-cat
lazy-cons
map
mapcat
partition
range
remove
repeat
repeatedly
replicate
reverse
sort
sort-by
split-at
split-with
</td>
</tr>
<tr>
<td>set operations</td>
<td>
disj
hash-set
set
set?
sorted-set
</td>
</tr>
<tr>
<td>string and character operations</td>
<td>
char-escape-string
char-name-string
format
str
string?
subs
</td>
</tr>
<tr>
<td>structs</td>
<td>
accessor
create-struct
defstruct
get-in
struct
struct-map
</td>
</tr>
<tr>
<td>threads and concurrency</td>
<td>
add-watcher
agent
agent-errors
await
await-for
clear-agent-errors
get-validator
locking
monitor-enter
monitor-exit
send
send-off
</td>
</tr>
<tr>
<td>transactions and refs</td>
<td>
alter
commute
deref
dosync
ensure
ref
sync
</td>
</tr>
<tr>
<td>vector operations</td>
<td>
get
nth
split-at
split-with
subvec
vec
vector
vector?
</td>
</tr>
<tr>
<td>miscellaneous</td>
<td>
->
alter-var-root
comment
delay
delay?
derive
descendants
do
doto
force
gensym
hash
identity
intern
keyword
make-hierarchy
name
preduce
reduce
symbol
time
var
</td>
</tr>
<tr>
<td></td>
<td>
</td>
</tr>
</table>
<h3><a name="syntax">Syntax</a></h3>
<p>
The following characters have special meaning in Clojure syntax.
</p>
<table border="1">
<tr>
<th>Character</th>
<th>Meaning</th>
<th>Examples</th>
</tr>
<tr>
<td>;</td>
<td>comment</td>
<td>; This is a comment that extends to the end of the line.</td>
</tr>
<tr>
<td>=</td>
<td>expression+</td>
<td>
This tests for equality (same as the Java equals method).
If any argument is nil then false is returned.
</td>
<td>(= "bar" (subs "foobar" 3)) -> true</td>
</tr>
<tr>
<td>==</td>
<td>number+</td>
<td>for testing numbers</td>
<td>(== 2 2.0 (+ 1 1)) -> true</td>
</tr>
<tr>
<td>/</td>
<td>used for namespace scoping of functions AND
static member access (fields and methods)</td>
<td>
(my-ns/my-function arg)<br />
Math/PI<br />
(System/getProperty "user.name")
</td>
</tr>
<tr>
<td>\</td>
<td>character</td>
<td>\a \b \1 \2 \@ \space \tab \newline</td>
</tr>
<tr>
<td>"</td>
<td>string</td>
<td>"This is a string."</td>
</tr>
<tr>
<td>:</td>
<td>keyword</td>
<td>:name</td>
</tr>
<tr>
<td>'</td>
<td>
This has several uses.
It can appear before a name to create a symbol.
It can appear before a list to "quote" it
which prevents evaluation where
the first item is treated as the name of a function.
It can also appear before a vector which quotes
every item inside it to prevent their evaluation.
</td>
<td>'(+ 2 3) -> (+ 2 3) ; without the quote it would evalute to 5</td>
</tr>
<tr>
<td>`</td>
<td>appears before a list to "backquote" it which prevents
immediate evaluation and resolves symbols to their namespaces</td>
<td>`(+ 2 3) -> (clojure.core/+ 2 3)</td>
</tr>
<tr>
<td>( )</td>
<td>list</td>
<td>("one" 2 :three) or (list "one" 2 :three)</td>
</tr>
<tr>
<td>[ ]</td>
<td>vector</td>
<td>["one" 2 :three] or (vector "one" 2 :three)</td>
</tr>
<tr>
<td>#{ }</td>
<td>set</td>
<td>#{"red" "green" "blue"}</td>
</tr>
<tr>
<td>{ }</td>
<td>map</td>
<td>{:key1 "value1" :key2 2}</td>
</tr>
<tr>
<td>~</td>
<td>forces evaluation of a sublist inside a backquoted list</td>
<td></td>
</tr>
<tr>
<td>&</td>
<td>precedes the last argument to a function,
followed by a space, to set the last argument to a list
of the remaining arguments</td>
<td>(defn foo [p1 p2 & remaining] ...)</td>
</tr>
<tr>
<td>_</td>
<td>anonymous parameter name; used when a function requires
a certain number of parameters, but doesn't use all of them</td>
<td>(fn [p1 _ p3] (+ p1 p3))</td>
</tr>
<tr>
<td>#'name</td>
<td>a reader macro that expands to (var name)</td>
<td>(meta #'str)</td>
</tr>
<tr>
<td>^name</td>
<td>a reader macro that expands to (meta name) to retrieve
the map of metadata associated with an object</td>
<td>^object</td>
</tr>
<tr>
<td>#^java-class-name</td>
<td>
This is the metadata dispatch macro that causes
the form after it to be added to the metadata
for the form after that.
As a special case, #^java-class is equivalent to
#^{:tag java-class} .
This provides a type hint to help the compiler
avoid using reflection, thereby improving performance.
Note that this is not equivalent to <code>with-meta</code>.
</td>
<td>#^String</td>
</tr>
<tr>
<td>#(function arg*)</td>
<td>returns a closure instead of invoking the function</td>
<td>#(prn "Hello")</td>
</tr>
<tr>
<td>@ref</td>
<td>
a reader macro that expands to (deref ref);
If outside a transaction,
returns the most recently committed value.
If inside a transaction, returns the "in transaction value".
</td>
<td>@myRef</td>
</tr>
</table>
<h3><a name="shortcuts">Shortcuts</a></h3>
<p>
The following syntax shortcuts are supported.
</p>
<table border="1">
<tr>
<th>Long Form</th>
<th>Short Form</th>
</tr>
<tr>
<td>(list 1 2 3)</td>
<td>'(1 2 3) or (quote (1 2 3))</td>
</tr>
<tr>
<td>(fn [arg*] body)</td>
<td>#(body) using %1, %2, ... for arguments (or just % if there is only one argument)</td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
</tr>
</table>
<h3><a name="builtins">Builtins</a></h3>
<p>
The table below documents some of the functions, macros and
special forms I have encountered so far in my study of Clojure.
</p>
<p>
For more information on these,
run <code>(doc <i>name</i>)</code> in a REPL.
</p>
<table border="1">
<tr>
<th width="110px">Name</th>
<th width="150px">Arguments</th>
<th width="300px">Description</th>
<th width="340px">Example</th>
</tr>
<tr>
<td>.</td>
<td>
instance-expr member-symbol<br />
instance-expr method-symbol arg*<br />
instance-expr (method-symbol arg*)<br />
classname-symbol member-symbol<br />
classname-symbol method-symbol arg*<br />
classname-symbol (method-symbol arg*)<br />
</td>
<td>
This is a way to access Java methods.
The dot is read "in the scope of".
Using the next form, <code>.<i>member</i>,</code> is preferred.
</td>
<td> </td>
</tr>
<tr>
<td>.<i>member</i></td>
<td>target arg*</td>
<td>
This is the same as <code>(. target member arg*)</code>,
however, this form is preferred.
</td>
<td> </td>
</tr>
<tr>
<td>..</td>
<td>
instance-expr member+<br />
classname-symbol member+
</td>
<td>This invokes multiple methods on the same receiver
or returns several instance variable values.</td>
<td> </td>
</tr>
<tr>
<td>=</td>
<td>expr1 expr2</td>
<td>
This determines if two expressions evaluate to the same value.
Also see not=.
</td>
<td> </td>
</tr>
<tr>
<td>add-classpath</td>
<td>string or URL object</td>
<td>This adds to the Java classpath.</td>
<td>(add-classpath "./lib")</td>
</tr>
<tr>
<td>aget</td>
<td>java-array index</td>
<td>This returns the value at the given index in a Java array.</td>
<td> </td>
</tr>
<tr>
<td>all-ns</td>
<td>none</td>
<td>This returns a sequence of all namespaces.</td>
<td>(all-ns)</td>
</tr>
<tr>
<td>and</td>
<td>expression*</td>
<td>
This evaluates the expressions from left to right,
stopping and returning false as soon as
one of them evaluates to nil or false.
If none do, the value of the last expression is returned.
</td>
<td>(and (< x 10) y) -> false or y</td>
</tr>
<tr>
<td>apply</td>
<td>function collection</td>
<td>
This returns a non-lazy sequence of the results of
invoking the function using
each item in the collection as an argument
instead of passing the collection as a single argument.
It expands the list into individual arguments.
Compare this to <code>map</code> which is lazy.
</td>
<td>
(apply + '(1 2 3)) -> 6<br />
(apply + [1 2 3]) -> 6
</td>
</tr>
<tr>
<td>array-map</td>
<td></td>
<td>
This creates an array map which is a map that maintains input order.
</td>
<td> </td>
</tr>
<tr>
<td>assoc</td>
<td>map (key value)+ or<br />
vector (index value)+</td>
<td>
When passed a map,
this creates a new one that is a copy of the one passed
with some number of key/value pairs added or replaced.
When passed a vector,
this creates a new one that is a copy of the one passed
with some number of values set at specified, existing indexes.
Indexes must be from zero to one past the last index used.
</td>
<td>
(assoc myMap :a 1 :b 2)<br/>
(assoc myVector 2 "two" 3 "three")
</td>
</tr>
<tr>
<td>await</td>
<td>agent+</td>
<td>This blocks the current thread until all dispatched actions
from this thread or agent to the specified agents have completed.</td>
<td> </td>
</tr>
<tr>
<td>bean</td>
<td></td>
<td>This returns a map of the JavaBean properties
in a given Java object.</td>
<td>(bean java.awt.Color/YELLOW)</td>
</tr>
<tr>
<td>binding-values</td>
<td></td>
<td></td>
<td> </td>
</tr>
<tr>
<td>bit-shift-left</td>
<td></td>
<td></td>
<td> </td>
</tr>
<tr>
<td>bit-shift-right</td>
<td></td>
<td></td>
<td> </td>
</tr>
<tr>
<td>boolean</td>
<td>x</td>
<td>This coerces x to a boolean value.</td>
<td> </td>
</tr>
<tr>
<td>byte</td>
<td>x</td>
<td>This coerces x to a byte value.</td>
<td> </td>
</tr>
<tr>
<td>char</td>
<td>x</td>
<td>This coerces x to a char value.</td>
<td> </td>
</tr>
<tr>
<td>class</td>
<td>x</td>
<td>This returns the Java class of x.</td>
<td>
(class true) -> java.lang.Boolean<br />
(class 19) -> java.lang.Integer<br />
(class 3.14) -> java.lang.Double<br />
(class :foo) -> clojure.lang.Keyword<br />
(class "foo") -> java.lang.String<br />
(class []) -> clojure.lang.PersistentVector<br />
(class '()) -> clojure.lang.PersistentList$EmptyList<br />
(class #{}) -> clojure.lang.PersistentHashSet<br />
(class {}) -> clojure.lang.PersistentHashMap
</td>
</tr>
<tr>
<td>commute</td>
<td>ref function arg*</td>
<td>
This must be called inside a transaction
(commonly created with <code>dosync</code>).
It modifies the value of a ref inside that transaction.
Also see <code>dosync</code> and <code>update-in</code>.
</td>
<td>(dosync (commute myStruct update-in [:field] function))</td>
</tr>
<tr>
<td>comp</td>
<td>function+</td>
<td>
This returns a new function that is a composition
of the specified functions from right to left.
When the new function is invoked
it applies the right-most function to the arguments,
then the next function to the left to that result,
and so on, returning the last result.
</td>
<td>
(def remove-ends (comp rest reverse rest reverse))<br />
(remove-ends [1 2 3 4]) -> (2 3)
</td>
</tr>
<tr>
<td>concat</td>
<td>collection*</td>
<td>This returns a lazy sequence of the concatenation
of the items in the collections.</td>
<td>(concat [1 2] [3 4]) -> (1 2 3 4)</td>
</tr>
<tr>
<td>cond</td>
<td>(test expression)*</td>
<td>
This is like a switch statement.
It returns the result of the expression
for the first test that evaluates to true,
or nil if none evaluate to true.
Also see if.
</td>
<td> </td>
</tr>
<tr>
<td>conj</td>
<td>collection item</td>
<td>
This returns a new collection with item added or "conjoined".
Where it is added depends on the collection type.
To append all the items in another vector,
use (reduce conj vector1 vector2).
Note how the argument order and the return type
differs from that of <code>cons</code>.
Also see <code>concat</code> and <code>into</code>.
</td>
<td>
(conj [1 2] 3) -> [1 2 3]<br />
(conj [1 2] [3 4]) -> [1 2 [3 4]]</td>
</td>
</tr>
<tr>
<td>cons</td>
<td>item seq</td>
<td>
This returns a new <b>sequence</b> where
x is the first element and seq is the rest.
Note how the argument order and the return type