-
Notifications
You must be signed in to change notification settings - Fork 786
/
core.cljs
10159 lines (8666 loc) · 285 KB
/
core.cljs
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
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.
(ns cljs.core
(:require [goog.string :as gstring]
[goog.object :as gobject]
[goog.array :as garray])
(:import [goog.string StringBuffer]))
;; next line is auto-generated by the build-script - Do not edit!
(def *clojurescript-version*)
(def *unchecked-if* false)
(def
^{:dynamic true
:doc "Var bound to the name value of the compiler build :target option.
For example, if the compiler build :target is :nodejs, *target* will be bound
to \"nodejs\". *target* is a Google Closure define and can be set by compiler
:closure-defines option."
:jsdoc ["@define {string}"]}
*target* "default")
(def
^{:dynamic true
:doc "Var bound to the current namespace. Only used for bootstrapping."}
*ns* nil)
(def
^{:dynamic true}
*out* nil)
(def
^{:dynamic true}
*assert* true)
(defonce
^{:doc "Each runtime environment provides a different way to print output.
Whatever function *print-fn* is bound to will be passed any
Strings which should be printed." :dynamic true}
*print-fn*
(fn [_]
(throw (js/Error. "No *print-fn* fn set for evaluation environment"))))
(defonce
^{:doc "Each runtime environment provides a different way to print error output.
Whatever function *print-fn* is bound to will be passed any
Strings which should be printed." :dynamic true}
*print-err-fn*
(fn [_]
(throw (js/Error. "No *print-err-fn* fn set for evaluation environment"))))
(defn set-print-fn!
"Set *print-fn* to f."
[f] (set! *print-fn* f))
(defn set-print-err-fn!
"Set *print-err-fn* to f."
[f] (set! *print-err-fn* f))
(def
^{:dynamic true
:doc "When set to true, output will be flushed whenever a newline is printed.
Defaults to true."}
*flush-on-newline* true)
(def
^{:dynamic true
:doc "When set to logical false will drop newlines from printing calls.
This is to work around the implicit newlines emitted by standard JavaScript
console objects."}
*print-newline* true)
(def
^{:dynamic true
:doc "When set to logical false, strings and characters will be printed with
non-alphanumeric characters converted to the appropriate escape sequences.
Defaults to true"}
*print-readably* true)
(def
^{:dynamic true
:doc "If set to logical true, when printing an object, its metadata will also
be printed in a form that can be read back by the reader.
Defaults to false."}
*print-meta* false)
(def
^{:dynamic true
:doc "When set to logical true, objects will be printed in a way that preserves
their type when read in later.
Defaults to false."}
*print-dup* false)
(def
^{:dynamic true
:doc "When set to logical true, objects will be printed in a way that preserves
their type when read in later.
Defaults to false."}
*print-length* nil)
(def
^{:dynamic true
:doc "*print-level* controls how many levels deep the printer will
print nested objects. If it is bound to logical false, there is no
limit. Otherwise, it must be bound to an integer indicating the maximum
level to print. Each argument to print is at level 0; if an argument is a
collection, its items are at level 1; and so on. If an object is a
collection and is at a level greater than or equal to the value bound to
*print-level*, the printer prints '#' to represent it. The root binding
is nil indicating no limit."}
*print-level* nil)
(defonce ^:dynamic *loaded-libs* nil)
(defn- pr-opts []
{:flush-on-newline *flush-on-newline*
:readably *print-readably*
:meta *print-meta*
:dup *print-dup*
:print-length *print-length*})
(declare into-array)
(defn enable-console-print!
"Set *print-fn* to console.log"
[]
(set! *print-newline* false)
(set! *print-fn*
(fn [& args]
(.apply (.-log js/console) js/console (into-array args))))
(set! *print-err-fn*
(fn [& args]
(.apply (.-error js/console) js/console (into-array args))))
nil)
(def
^{:doc "bound in a repl thread to the most recent value printed"}
*1)
(def
^{:doc "bound in a repl thread to the second most recent value printed"}
*2)
(def
^{:doc "bound in a repl thread to the third most recent value printed"}
*3)
(def
^{:doc "bound in a repl thread to the most recent exception caught by the repl"}
*e)
(defn truth_
"Internal - do not use!"
[x]
(cljs.core/truth_ x))
(def not-native nil)
(declare instance? Keyword)
(defn ^boolean identical?
"Tests if 2 arguments are the same object"
[x y]
(cljs.core/identical? x y))
(defn ^boolean nil?
"Returns true if x is nil, false otherwise."
[x]
(coercive-= x nil))
(defn ^boolean array?
"Returns true if x is a JavaScript array."
[x]
(if (identical? *target* "nodejs")
(.isArray js/Array x)
(instance? js/Array x)))
(defn ^boolean number?
"Returns true if x is a JavaScript number."
[n]
(cljs.core/number? n))
(defn ^boolean not
"Returns true if x is logical false, false otherwise."
[x]
(cond
(nil? x) true
(false? x) true
:else false))
(defn ^boolean some?
"Returns true if x is not nil, false otherwise."
[x] (not (nil? x)))
(defn ^boolean object?
"Returns true if x's constructor is Object"
[x]
(if-not (nil? x)
(identical? (.-constructor x) js/Object)
false))
(defn ^boolean string?
"Returns true if x is a JavaScript string."
[x]
(goog/isString x))
(defn ^boolean char?
"Returns true if x is a JavaScript char."
[x]
(gstring/isUnicodeChar x))
(set! *unchecked-if* true)
(defn ^boolean native-satisfies?
"Internal - do not use!"
[p x]
(let [x (if (nil? x) nil x)]
(cond
(aget p (goog/typeOf x)) true
(aget p "_") true
:else false)))
(set! *unchecked-if* false)
(defn is_proto_
[x]
(identical? (.-prototype (.-constructor x)) x))
(def
^{:doc "When compiled for a command-line target, whatever
function *main-fn* is set to will be called with the command-line
argv as arguments"}
*main-cli-fn* nil)
(defn type
"Return x's constructor."
[x]
(when-not (nil? x)
(.-constructor x)))
(defn missing-protocol [proto obj]
(let [ty (type obj)
ty (if (and ty (.-cljs$lang$type ty))
(.-cljs$lang$ctorStr ty)
(goog/typeOf obj))]
(js/Error.
(.join (array "No protocol method " proto
" defined for type " ty ": " obj) ""))))
(defn type->str [ty]
(if-let [s (.-cljs$lang$ctorStr ty)]
s
(str ty)))
;; INTERNAL - do not use, only for Node.js
(defn load-file [file]
(when-not js/COMPILED
(cljs.core/load-file* file)))
(if (and (exists? js/Symbol)
(identical? (goog/typeOf js/Symbol) "function"))
(def ITER_SYMBOL (.-iterator js/Symbol))
(def ITER_SYMBOL "@@iterator"))
(def ^{:jsdoc ["@enum {string"]}
CHAR_MAP
#js {"-" "_"
":" "_COLON_"
"+" "_PLUS_"
">" "_GT_"
"<" "_LT_"
"=" "_EQ_"
"~" "_TILDE_"
"!" "_BANG_"
"@" "_CIRCA_"
"#" "_SHARP_"
"'" "_SINGLEQUOTE_"
"\\\"" "_DOUBLEQUOTE_"
"%" "_PERCENT_"
"^" "_CARET_"
"&" "_AMPERSAND_"
"*" "_STAR_"
"|" "_BAR_"
"{" "_LBRACE_"
"}" "_RBRACE_"
"[" "_LBRACK_"
"]" "_RBRACK_"
"/" "_SLASH_"
"\\\\" "_BSLASH_"
"?" "_QMARK_"})
(def ^{:jsdoc ["@enum {string}"]}
DEMUNGE_MAP
#js {"_" "-"
"_COLON_" ":"
"_PLUS_" "+"
"_GT_" ">"
"_LT_" "<"
"_EQ_" "="
"_TILDE_" "~"
"_BANG_" "!"
"_CIRCA_" "@"
"_SHARP_" "#"
"_SINGLEQUOTE_" "'"
"_DOUBLEQUOTE_" "\\\""
"_PERCENT_" "%"
"_CARET_" "^"
"_AMPERSAND_" "&"
"_STAR_" "*"
"_BAR_" "|"
"_LBRACE_" "{"
"_RBRACE_" "}"
"_LBRACK_" "["
"_RBRACK_" "]"
"_SLASH_" "/"
"_BSLASH_" "\\\\"
"_QMARK_" "?"})
(def DEMUNGE_PATTERN nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; arrays ;;;;;;;;;;;;;;;;
(defn ^array make-array
"Construct a JavaScript array of specified size. Accepts ignored type
argument for compatibility with Clojure."
([size]
(js/Array. size))
([type size]
(make-array size)))
(defn aclone
"Returns a javascript array, cloned from the passed in array"
[arr]
(let [len (alength arr)
new-arr (make-array len)]
(dotimes [i len]
(aset new-arr i (aget arr i)))
new-arr))
(defn ^array array
"Creates a new javascript array.
@param {...*} var_args" ;;array is a special case, don't emulate this doc string
[var-args] ;; [& items]
(let [a (js/Array. (alength (cljs.core/js-arguments)))]
(loop [i 0]
(if (< i (alength a))
(do
(aset a i (aget (cljs.core/js-arguments) i))
(recur (inc i)))
a))))
(declare apply)
(defn aget
"Returns the value at the index."
([array i]
(cljs.core/aget array i))
([array i & idxs]
(apply aget (aget array i) idxs)))
(defn aset
"Sets the value at the index."
([array i val]
(cljs.core/aset array i val))
([array idx idx2 & idxv]
(apply aset (aget array idx) idx2 idxv)))
(defn ^number alength
"Returns the length of the array. Works on arrays of all types."
[array]
(cljs.core/alength array))
(declare reduce)
(defn ^array into-array
"Returns an array with components set to the values in aseq. Optional type
argument accepted for compatibility with Clojure."
([aseq]
(into-array nil aseq))
([type aseq]
(reduce (fn [a x] (.push a x) a) (array) aseq)))
(defn js-invoke
"Invoke JavaScript object method via string. Needed when the
string is not a valid unquoted property name."
[obj s & args]
(.apply (aget obj s) obj (into-array args)))
;;;;;;;;;;;;;;;;;;;;;;;;;;; core protocols ;;;;;;;;;;;;;
(defprotocol Fn
"Marker protocol")
(defprotocol IFn
"Protocol for adding the ability to invoke an object as a function.
For example, a vector can also be used to look up a value:
([1 2 3 4] 1) => 2"
(-invoke
[this]
[this a]
[this a b]
[this a b c]
[this a b c d]
[this a b c d e]
[this a b c d e f]
[this a b c d e f g]
[this a b c d e f g h]
[this a b c d e f g h i]
[this a b c d e f g h i j]
[this a b c d e f g h i j k]
[this a b c d e f g h i j k l]
[this a b c d e f g h i j k l m]
[this a b c d e f g h i j k l m n]
[this a b c d e f g h i j k l m n o]
[this a b c d e f g h i j k l m n o p]
[this a b c d e f g h i j k l m n o p q]
[this a b c d e f g h i j k l m n o p q r]
[this a b c d e f g h i j k l m n o p q r s]
[this a b c d e f g h i j k l m n o p q r s t]
[this a b c d e f g h i j k l m n o p q r s t rest]))
(defprotocol ICloneable
"Protocol for cloning a value."
(^clj -clone [value]
"Creates a clone of value."))
(defprotocol ICounted
"Protocol for adding the ability to count a collection in constant time."
(^number -count [coll]
"Calculates the count of coll in constant time. Used by cljs.core/count."))
(defprotocol IEmptyableCollection
"Protocol for creating an empty collection."
(-empty [coll]
"Returns an empty collection of the same category as coll. Used
by cljs.core/count."))
(defprotocol ICollection
"Protocol for adding to a collection."
(^clj -conj [coll o]
"Returns a new collection of coll with o added to it. The new item
should be added to the most efficient place, e.g.
(conj [1 2 3 4] 5) => [1 2 3 4 5]
(conj '(2 3 4 5) 1) => '(1 2 3 4 5)"))
#_(defprotocol IOrdinal
(-index [coll]))
(defprotocol IIndexed
"Protocol for collections to provide idexed-based access to their items."
(-nth [coll n] [coll n not-found]
"Returns the value at the index n in the collection coll.
Returns not-found if index n is out of bounds and not-found is supplied."))
(defprotocol ASeq
"Marker protocol indicating an array sequence.")
(defprotocol ISeq
"Protocol for collections to provide access to their items as sequences."
(-first [coll]
"Returns the first item in the collection coll. Used by cljs.core/first.")
(^clj -rest [coll]
"Returns a new collection of coll without the first item. It should
always return a seq, e.g.
(rest []) => ()
(rest nil) => ()"))
(defprotocol INext
"Protocol for accessing the next items of a collection."
(^clj-or-nil -next [coll]
"Returns a new collection of coll without the first item. In contrast to
rest, it should return nil if there are no more items, e.g.
(next []) => nil
(next nil) => nil"))
(defprotocol ILookup
"Protocol for looking up a value in a data structure."
(-lookup [o k] [o k not-found]
"Use k to look up a value in o. If not-found is supplied and k is not
a valid value that can be used for look up, not-found is returned."))
(defprotocol IAssociative
"Protocol for adding associativity to collections."
(^boolean -contains-key? [coll k]
"Returns true if k is a key in coll.")
#_(-entry-at [coll k])
(^clj -assoc [coll k v]
"Returns a new collection of coll with a mapping from key k to
value v added to it."))
(defprotocol IMap
"Protocol for adding mapping functionality to collections."
#_(-assoc-ex [coll k v])
(^clj -dissoc [coll k]
"Returns a new collection of coll without the mapping for key k."))
(defprotocol IMapEntry
"Protocol for examining a map entry."
(-key [coll]
"Returns the key of the map entry.")
(-val [coll]
"Returns the value of the map entry."))
(defprotocol ISet
"Protocol for adding set functionality to a collection."
(^clj -disjoin [coll v]
"Returns a new collection of coll that does not contain v."))
(defprotocol IStack
"Protocol for collections to provide access to their items as stacks. The top
of the stack should be accessed in the most efficient way for the different
data structures."
(-peek [coll]
"Returns the item from the top of the stack. Is used by cljs.core/peek.")
(^clj -pop [coll]
"Returns a new stack without the item on top of the stack. Is used
by cljs.core/pop."))
(defprotocol IVector
"Protocol for adding vector functionality to collections."
(^clj -assoc-n [coll n val]
"Returns a new vector with value val added at position n."))
(defprotocol IDeref
"Protocol for adding dereference functionality to a reference."
(-deref [o]
"Returns the value of the reference o."))
(defprotocol IDerefWithTimeout
(-deref-with-timeout [o msec timeout-val]))
(defprotocol IMeta
"Protocol for accessing the metadata of an object."
(^clj-or-nil -meta [o]
"Returns the metadata of object o."))
(defprotocol IWithMeta
"Protocol for adding metadata to an object."
(^clj -with-meta [o meta]
"Returns a new object with value of o and metadata meta added to it."))
(defprotocol IReduce
"Protocol for seq types that can reduce themselves.
Called by cljs.core/reduce."
(-reduce [coll f] [coll f start]
"f should be a function of 2 arguments. If start is not supplied,
returns the result of applying f to the first 2 items in coll, then
applying f to that result and the 3rd item, etc."))
(defprotocol IKVReduce
"Protocol for associative types that can reduce themselves
via a function of key and val. Called by cljs.core/reduce-kv."
(-kv-reduce [coll f init]
"Reduces an associative collection and returns the result. f should be
a function that takes three arguments."))
(defprotocol IEquiv
"Protocol for adding value comparison functionality to a type."
(^boolean -equiv [o other]
"Returns true if o and other are equal, false otherwise."))
(defprotocol IHash
"Protocol for adding hashing functionality to a type."
(-hash [o]
"Returns the hash code of o."))
(defprotocol ISeqable
"Protocol for adding the ability to a type to be transformed into a sequence."
(^clj-or-nil -seq [o]
"Returns a seq of o, or nil if o is empty."))
(defprotocol ISequential
"Marker interface indicating a persistent collection of sequential items")
(defprotocol IList
"Marker interface indicating a persistent list")
(defprotocol IRecord
"Marker interface indicating a record object")
(defprotocol IReversible
"Protocol for reversing a seq."
(^clj -rseq [coll]
"Returns a seq of the items in coll in reversed order."))
(defprotocol ISorted
"Protocol for a collection which can represent their items
in a sorted manner. "
(^clj -sorted-seq [coll ascending?]
"Returns a sorted seq from coll in either ascending or descending order.")
(^clj -sorted-seq-from [coll k ascending?]
"Returns a sorted seq from coll in either ascending or descending order.
If ascending is true, the result should contain all items which are > or >=
than k. If ascending is false, the result should contain all items which
are < or <= than k, e.g.
(-sorted-seq-from (sorted-set 1 2 3 4 5) 3 true) => (3 4 5)
(-sorted-seq-from (sorted-set 1 2 3 4 5) 3 false) => (3 2 1)")
(-entry-key [coll entry]
"Returns the key for entry.")
(-comparator [coll]
"Returns the comparator for coll."))
(defprotocol IWriter
"Protocol for writing. Currently only implemented by StringBufferWriter."
(-write [writer s]
"Writes s with writer and returns the result.")
(-flush [writer]
"Flush writer."))
(defprotocol IPrintWithWriter
"The old IPrintable protocol's implementation consisted of building a giant
list of strings to concatenate. This involved lots of concat calls,
intermediate vectors, and lazy-seqs, and was very slow in some older JS
engines. IPrintWithWriter implements printing via the IWriter protocol, so it
be implemented efficiently in terms of e.g. a StringBuffer append."
(-pr-writer [o writer opts]))
(defprotocol IPending
"Protocol for types which can have a deferred realization. Currently only
implemented by Delay."
(^boolean -realized? [d]
"Returns true if a value for d has been produced, false otherwise."))
(defprotocol IWatchable
"Protocol for types that can be watched. Currently only implemented by Atom."
(-notify-watches [this oldval newval]
"Calls all watchers with this, oldval and newval.")
(-add-watch [this key f]
"Adds a watcher function f to this. Keys must be unique per reference,
and can be used to remove the watch with -remove-watch.")
(-remove-watch [this key]
"Removes watcher that corresponds to key from this."))
(defprotocol IEditableCollection
"Protocol for collections which can transformed to transients."
(^clj -as-transient [coll]
"Returns a new, transient version of the collection, in constant time."))
(defprotocol ITransientCollection
"Protocol for adding basic functionality to transient collections."
(^clj -conj! [tcoll val]
"Adds value val to tcoll and returns tcoll.")
(^clj -persistent! [tcoll]
"Creates a persistent data structure from tcoll and returns it."))
(defprotocol ITransientAssociative
"Protocol for adding associativity to transient collections."
(^clj -assoc! [tcoll key val]
"Returns a new transient collection of tcoll with a mapping from key to
val added to it."))
(defprotocol ITransientMap
"Protocol for adding mapping functionality to transient collections."
(^clj -dissoc! [tcoll key]
"Returns a new transient collection of tcoll without the mapping for key."))
(defprotocol ITransientVector
"Protocol for adding vector functionality to transient collections."
(^clj -assoc-n! [tcoll n val]
"Returns tcoll with value val added at position n.")
(^clj -pop! [tcoll]
"Returns tcoll with the last item removed from it."))
(defprotocol ITransientSet
"Protocol for adding set functionality to a transient collection."
(^clj -disjoin! [tcoll v]
"Returns tcoll without v."))
(defprotocol IComparable
"Protocol for values that can be compared."
(^number -compare [x y]
"Returns a negative number, zero, or a positive number when x is logically
'less than', 'equal to', or 'greater than' y."))
(defprotocol IChunk
"Protocol for accessing the items of a chunk."
(-drop-first [coll]
"Return a new chunk of coll with the first item removed."))
(defprotocol IChunkedSeq
"Protocol for accessing a collection as sequential chunks."
(-chunked-first [coll]
"Returns the first chunk in coll.")
(-chunked-rest [coll]
"Return a new collection of coll with the first chunk removed."))
(defprotocol IChunkedNext
"Protocol for accessing the chunks of a collection."
(-chunked-next [coll]
"Returns a new collection of coll without the first chunk."))
(defprotocol INamed
"Protocol for adding a name."
(^string -name [x]
"Returns the name String of x.")
(^string -namespace [x]
"Returns the namespace String of x."))
(defprotocol IAtom
"Marker protocol indicating an atom.")
(defprotocol IReset
"Protocol for adding resetting functionality."
(-reset! [o new-value]
"Sets the value of o to new-value."))
(defprotocol ISwap
"Protocol for adding swapping functionality."
(-swap! [o f] [o f a] [o f a b] [o f a b xs]
"Swaps the value of o to be (apply f current-value-of-atom args)."))
(defprotocol IVolatile
"Protocol for adding volatile functionality."
(-vreset! [o new-value]
"Sets the value of volatile o to new-value without regard for the
current value. Returns new-value."))
(defprotocol IIterable
"Protocol for iterating over a collection."
(-iterator [coll]
"Returns an iterator for coll."))
;; Printing support
(deftype StringBufferWriter [sb]
IWriter
(-write [_ s] (.append sb s))
(-flush [_] nil))
(defn pr-str*
"Support so that collections can implement toString without
loading all the printing machinery."
[^not-native obj]
(let [sb (StringBuffer.)
writer (StringBufferWriter. sb)]
(-pr-writer obj writer (pr-opts))
(-flush writer)
(str sb)))
;;;;;;;;;;;;;;;;;;; Murmur3 ;;;;;;;;;;;;;;;
;;http://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/file/8c2c5d63a17e/src/share/classes/java/lang/Integer.java
(defn ^number int-rotate-left [x n]
(bit-or
(bit-shift-left x n)
(unsigned-bit-shift-right x (- n))))
;; http://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/imul
(if (and (exists? Math/imul)
(not (zero? (Math/imul 0xffffffff 5))))
(defn ^number imul [a b] (Math/imul a b))
(defn ^number imul [a b]
(let [ah (bit-and (unsigned-bit-shift-right a 16) 0xffff)
al (bit-and a 0xffff)
bh (bit-and (unsigned-bit-shift-right b 16) 0xffff)
bl (bit-and b 0xffff)]
(bit-or
(+ (* al bl)
(unsigned-bit-shift-right
(bit-shift-left (+ (* ah bl) (* al bh)) 16) 0)) 0))))
;; http://smhasher.googlecode.com/svn/trunk/MurmurHash3.cpp
(def m3-seed 0)
(def m3-C1 (int 0xcc9e2d51))
(def m3-C2 (int 0x1b873593))
(defn ^number m3-mix-K1 [k1]
(-> (int k1) (imul m3-C1) (int-rotate-left 15) (imul m3-C2)))
(defn ^number m3-mix-H1 [h1 k1]
(int (-> (int h1) (bit-xor (int k1)) (int-rotate-left 13) (imul 5) (+ (int 0xe6546b64)))))
(defn ^number m3-fmix [h1 len]
(as-> (int h1) h1
(bit-xor h1 len)
(bit-xor h1 (unsigned-bit-shift-right h1 16))
(imul h1 (int 0x85ebca6b))
(bit-xor h1 (unsigned-bit-shift-right h1 13))
(imul h1 (int 0xc2b2ae35))
(bit-xor h1 (unsigned-bit-shift-right h1 16))))
(defn ^number m3-hash-int [in]
(if (zero? in)
in
(let [k1 (m3-mix-K1 in)
h1 (m3-mix-H1 m3-seed k1)]
(m3-fmix h1 4))))
(defn ^number m3-hash-unencoded-chars [in]
(let [h1 (loop [i 1 h1 m3-seed]
(if (< i (alength in))
(recur (+ i 2)
(m3-mix-H1 h1
(m3-mix-K1
(bit-or (.charCodeAt in (dec i))
(bit-shift-left (.charCodeAt in i) 16)))))
h1))
h1 (if (== (bit-and (alength in) 1) 1)
(bit-xor h1 (m3-mix-K1 (.charCodeAt in (dec (alength in)))))
h1)]
(m3-fmix h1 (imul 2 (alength in)))))
;;;;;;;;;;;;;;;;;;; symbols ;;;;;;;;;;;;;;;
(declare list Symbol = compare)
;; Simple caching of string hashcode
(def string-hash-cache (js-obj))
(def string-hash-cache-count 0)
;;http://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/file/8c2c5d63a17e/src/share/classes/java/lang/String.java
(defn hash-string* [s]
(if-not (nil? s)
(let [len (alength s)]
(if (pos? len)
(loop [i 0 hash 0]
(if (< i len)
(recur (inc i) (+ (imul 31 hash) (.charCodeAt s i)))
hash))
0))
0))
(defn add-to-string-hash-cache [k]
(let [h (hash-string* k)]
(aset string-hash-cache k h)
(set! string-hash-cache-count (inc string-hash-cache-count))
h))
(defn hash-string [k]
(when (> string-hash-cache-count 255)
(set! string-hash-cache (js-obj))
(set! string-hash-cache-count 0))
(let [h (aget string-hash-cache k)]
(if (number? h)
h
(add-to-string-hash-cache k))))
(defn hash
"Returns the hash code of its argument. Note this is the hash code
consistent with =."
[o]
(cond
(implements? IHash o)
(-hash ^not-native o)
(number? o)
(js-mod (Math/floor o) 2147483647)
(true? o) 1
(false? o) 0
(string? o)
(m3-hash-int (hash-string o))
(instance? js/Date o)
(.valueOf o)
(nil? o) 0
:else
(-hash o)))
(defn hash-combine [seed hash]
; a la boost
(bit-xor seed
(+ hash 0x9e3779b9
(bit-shift-left seed 6)
(bit-shift-right seed 2))))
(defn ^boolean instance?
"Evaluates x and tests if it is an instance of the type
c. Returns true or false"
[t o]
(cljs.core/instance? t o))
(defn ^boolean symbol?
"Return true if x is a Symbol"
[x]
(instance? Symbol x))
(defn- hash-symbol [sym]
(hash-combine
(m3-hash-unencoded-chars (.-name sym))
(hash-string (.-ns sym))))
(defn- compare-symbols [a b]
(cond
(identical? (.-str a) (.-str b)) 0
(and (not (.-ns a)) (.-ns b)) -1
(.-ns a) (if-not (.-ns b)
1
(let [nsc (garray/defaultCompare (.-ns a) (.-ns b))]
(if (== 0 nsc)
(garray/defaultCompare (.-name a) (.-name b))
nsc)))
:default (garray/defaultCompare (.-name a) (.-name b))))
(declare get)
(deftype Symbol [ns name str ^:mutable _hash _meta]
Object
(toString [_] str)
(equiv [this other] (-equiv this other))
IEquiv
(-equiv [_ other]
(if (instance? Symbol other)
(identical? str (.-str other))
false))
IFn
(-invoke [sym coll]
(get coll sym))
(-invoke [sym coll not-found]
(get coll sym not-found))
IMeta
(-meta [_] _meta)
IWithMeta
(-with-meta [_ new-meta] (Symbol. ns name str _hash new-meta))
IHash
(-hash [sym]
(caching-hash sym hash-symbol _hash))
INamed
(-name [_] name)
(-namespace [_] ns)
IPrintWithWriter
(-pr-writer [o writer _] (-write writer str)))
(defn symbol
([name]
(if (symbol? name)
name
(let [idx (.indexOf name "/")]
(if (== idx -1)
(symbol nil name)
(symbol (.substring name 0 idx)
(.substring name (inc idx) (. name -length)))))))
([ns name]
(let [sym-str (if-not (nil? ns)
(str ns "/" name)
name)]
(Symbol. ns name sym-str nil nil))))
(deftype Var [val sym _meta]
Object
(isMacro [_]
(. (val) -cljs$lang$macro))
IDeref
(-deref [_] (val))
IMeta
(-meta [_] _meta)
IWithMeta
(-with-meta [_ new-meta]
(Var. val sym new-meta))
IEquiv
(-equiv [this other]
(if (instance? Var other)
(= (.-sym this) (.-sym other))
false))
Fn
IFn
(-invoke [_]
((val)))
(-invoke [_ a]
((val) a))
(-invoke [_ a b]
((val) a b))
(-invoke [_ a b c]
((val) a b c))
(-invoke [_ a b c d]
((val) a b c d))
(-invoke [_ a b c d e]
((val) a b c d e))
(-invoke [_ a b c d e f]
((val) a b c d e f))
(-invoke [_ a b c d e f g]
((val) a b c d e f g))
(-invoke [_ a b c d e f g h]
((val) a b c d e f g h))
(-invoke [_ a b c d e f g h i]
((val) a b c d e f g h i))
(-invoke [_ a b c d e f g h i j]
((val) a b c d e f g h i j))
(-invoke [_ a b c d e f g h i j k]
((val) a b c d e f g h i j k))