-
Notifications
You must be signed in to change notification settings - Fork 788
/
core.cljs
9117 lines (7707 loc) · 253 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
^{: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"))))
(defn set-print-fn!
"Set *print-fn* to f."
[f] (set! *print-fn* f))
(def ^:dynamic *flush-on-newline* true)
(def ^:dynamic *print-newline* true)
(def ^:dynamic *print-readably* true)
(def ^:dynamic *print-meta* false)
(def ^:dynamic *print-dup* false)
(def ^:dynamic *print-length* nil)
(def ^:dynamic *print-level* 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)))))
(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)
(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? [x]
(cljs.core/array? x))
(defn ^boolean number? [n]
(cljs.core/number? n))
(defn ^boolean not
"Returns true if x is logical false, false otherwise."
[x] (if x false true))
(defn ^boolean some?
"Returns true if x is not nil, false otherwise."
[x] (not (nil? x)))
(defn ^boolean object? [x]
(if-not (nil? x)
(identical? (.-constructor x) js/Object)
false))
(defn ^boolean string? [x]
(goog/isString 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 [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)))
(if (and (exists? js/Symbol)
(identical? (goog/typeOf js/Symbol) "function"))
(def ITER_SYMBOL (.-iterator js/Symbol))
(def ITER_SYMBOL "@@iterator"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; arrays ;;;;;;;;;;;;;;;;
(defn ^array make-array
([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]
(.. js/Array -prototype -slice (call (cljs.core/js-arguments))))
(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
([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
(-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
(^clj -clone [value]))
(defprotocol ICounted
(^number -count [coll] "constant time count"))
(defprotocol IEmptyableCollection
(-empty [coll]))
(defprotocol ICollection
(^clj -conj [coll o]))
#_(defprotocol IOrdinal
(-index [coll]))
(defprotocol IIndexed
(-nth [coll n] [coll n not-found]))
(defprotocol ASeq)
(defprotocol ISeq
(-first [coll])
(^clj -rest [coll]))
(defprotocol INext
(^clj-or-nil -next [coll]))
(defprotocol ILookup
(-lookup [o k] [o k not-found]))
(defprotocol IAssociative
(^boolean -contains-key? [coll k])
#_(-entry-at [coll k])
(^clj -assoc [coll k v]))
(defprotocol IMap
#_(-assoc-ex [coll k v])
(^clj -dissoc [coll k]))
(defprotocol IMapEntry
(-key [coll])
(-val [coll]))
(defprotocol ISet
(^clj -disjoin [coll v]))
(defprotocol IStack
(-peek [coll])
(^clj -pop [coll]))
(defprotocol IVector
(^clj -assoc-n [coll n val]))
(defprotocol IDeref
(-deref [o]))
(defprotocol IDerefWithTimeout
(-deref-with-timeout [o msec timeout-val]))
(defprotocol IMeta
(^clj-or-nil -meta [o]))
(defprotocol IWithMeta
(^clj -with-meta [o meta]))
(defprotocol IReduce
(-reduce [coll f] [coll f start]))
(defprotocol IKVReduce
(-kv-reduce [coll f init]))
(defprotocol IEquiv
(^boolean -equiv [o other]))
(defprotocol IHash
(-hash [o]))
(defprotocol ISeqable
(^clj-or-nil -seq [o]))
(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
(^clj -rseq [coll]))
(defprotocol ISorted
(^clj -sorted-seq [coll ascending?])
(^clj -sorted-seq-from [coll k ascending?])
(-entry-key [coll entry])
(-comparator [coll]))
(defprotocol IWriter
(-write [writer s])
(-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
(^boolean -realized? [d]))
(defprotocol IWatchable
(-notify-watches [this oldval newval])
(-add-watch [this key f])
(-remove-watch [this key]))
(defprotocol IEditableCollection
(^clj -as-transient [coll]))
(defprotocol ITransientCollection
(^clj -conj! [tcoll val])
(^clj -persistent! [tcoll]))
(defprotocol ITransientAssociative
(^clj -assoc! [tcoll key val]))
(defprotocol ITransientMap
(^clj -dissoc! [tcoll key]))
(defprotocol ITransientVector
(^clj -assoc-n! [tcoll n val])
(^clj -pop! [tcoll]))
(defprotocol ITransientSet
(^clj -disjoin! [tcoll v]))
(defprotocol IComparable
(^number -compare [x y]))
(defprotocol IChunk
(-drop-first [coll]))
(defprotocol IChunkedSeq
(-chunked-first [coll])
(-chunked-rest [coll]))
(defprotocol IChunkedNext
(-chunked-next [coll]))
(defprotocol INamed
(^string -name [x])
(^string -namespace [x]))
(defprotocol IAtom)
(defprotocol IReset
(-reset! [o new-value]))
(defprotocol ISwap
(-swap! [o f] [o f a] [o f a b] [o f a b xs]))
(defprotocol IVolatile
(-vreset! [o new-value]))
(defprotocol IIterable
(-iterator [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 0xcc9e2d51)
(def m3-C2 0x1b873593)
(defn ^number m3-mix-K1 [k1]
(-> k1 (imul m3-C1) (int-rotate-left 15) (imul m3-C2)))
(defn ^number m3-mix-H1 [h1 k1]
(-> h1 (bit-xor k1) (int-rotate-left 13) (imul 5) (+ 0xe6546b64)))
(defn ^number m3-fmix [h1 len]
(as-> h1 h1
(bit-xor h1 len)
(bit-xor h1 (unsigned-bit-shift-right h1 16))
(imul h1 0x85ebca6b)
(bit-xor h1 (unsigned-bit-shift-right h1 13))
(imul h1 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 [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? [t o]
(cljs.core/instance? t o))
(defn ^boolean 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))))
(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]
(-lookup coll sym nil))
(-invoke [sym coll not-found]
(-lookup 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
(symbol nil name)))
([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]
IDeref
(-deref [_] val)
IMeta
(-meta [_] _meta))
;;;;;;;;;;;;;;;;;;; fundamentals ;;;;;;;;;;;;;;;
(declare array-seq prim-seq IndexedSeq)
(defn iterable? [x]
(satisfies? IIterable x))
(defn clone [value]
(-clone value))
(defn cloneable? [value]
(satisfies? ICloneable value))
(defn ^seq seq
"Returns a seq on the collection. If the collection is
empty, returns nil. (seq nil) returns nil. seq also works on
Strings."
[coll]
(when-not (nil? coll)
(cond
(implements? ISeqable coll)
(-seq ^not-native coll)
(array? coll)
(when-not (zero? (alength coll))
(IndexedSeq. coll 0))
(string? coll)
(when-not (zero? (alength coll))
(IndexedSeq. coll 0))
(native-satisfies? ISeqable coll)
(-seq coll)
:else (throw (js/Error. (str coll " is not ISeqable"))))))
(defn first
"Returns the first item in the collection. Calls seq on its
argument. If coll is nil, returns nil."
[coll]
(when-not (nil? coll)
(if (implements? ISeq coll)
(-first ^not-native coll)
(let [s (seq coll)]
(when-not (nil? s)
(-first s))))))
(defn ^seq rest
"Returns a possibly empty seq of the items after the first. Calls seq on its
argument."
[coll]
(if-not (nil? coll)
(if (implements? ISeq coll)
(-rest ^not-native coll)
(let [s (seq coll)]
(if s
(-rest ^not-native s)
())))
()))
(defn ^seq next
"Returns a seq of the items after the first. Calls seq on its
argument. If there are no more items, returns nil"
[coll]
(when-not (nil? coll)
(if (implements? INext coll)
(-next ^not-native coll)
(seq (rest coll)))))
(defn ^boolean =
"Equality. Returns true if x equals y, false if not. Compares
numbers and collections in a type-independent manner. Clojure's immutable data
structures define -equiv (and thus =) as a value, not an identity,
comparison."
([x] true)
([x y]
(if (nil? x)
(nil? y)
(or (identical? x y)
^boolean (-equiv x y))))
([x y & more]
(if (= x y)
(if (next more)
(recur y (first more) (next more))
(= y (first more)))
false)))
;; EXPERIMENTAL: subject to change
(deftype ES6Iterator [^:mutable s]
Object
(next [_]
(if-not (nil? s)
(let [x (first s)]
(set! s (next s))
#js {:value x :done false})
#js {:value nil :done true})))
(defn es6-iterator [coll]
(ES6Iterator. (seq coll)))
(declare es6-iterator-seq)
(deftype ES6IteratorSeq [value iter ^:mutable _rest]
ISeqable
(-seq [this] this)
ISeq
(-first [_] value)
(-rest [_]
(when (nil? _rest)
(set! _rest (es6-iterator-seq iter)))
_rest))
(defn es6-iterator-seq [iter]
(let [v (.next iter)]
(if (.-done v)
()
(ES6IteratorSeq. (.-value v) iter nil))))
;;;;;;;;;;;;;;;;;;; Murmur3 Helpers ;;;;;;;;;;;;;;;;
(defn ^number mix-collection-hash
"Mix final collection hash for ordered or unordered collections.
hash-basis is the combined collection hash, count is the number
of elements included in the basis. Note this is the hash code
consistent with =, different from .hashCode.
See http://clojure.org/data_structures#hash for full algorithms."
[hash-basis count]
(let [h1 m3-seed
k1 (m3-mix-K1 hash-basis)
h1 (m3-mix-H1 h1 k1)]
(m3-fmix h1 count)))
(defn ^number hash-ordered-coll
"Returns the hash code, consistent with =, for an external ordered
collection implementing Iterable.
See http://clojure.org/data_structures#hash for full algorithms."
[coll]
(loop [n 0 hash-code 1 coll (seq coll)]
(if-not (nil? coll)
(recur (inc n) (bit-or (+ (imul 31 hash-code) (hash (first coll))) 0)
(next coll))
(mix-collection-hash hash-code n))))
(defn ^number hash-unordered-coll
"Returns the hash code, consistent with =, for an external unordered
collection implementing Iterable. For maps, the iterator should
return map entries whose hash is computed as
(hash-ordered-coll [k v]).
See http://clojure.org/data_structures#hash for full algorithms."
[coll]
(loop [n 0 hash-code 0 coll (seq coll)]
(if-not (nil? coll)
(recur (inc n) (bit-or (+ hash-code (hash (first coll))) 0) (next coll))
(mix-collection-hash hash-code n))))
;;;;;;;;;;;;;;;;;;; protocols on primitives ;;;;;;;;
(declare hash-map list equiv-sequential)
(extend-type nil
ICounted
(-count [_] 0))
;; TODO: we should remove this and handle date equality checking
;; by some other means, probably by adding a new primitive type
;; case to the hash table lookup - David
(extend-type js/Date
IEquiv
(-equiv [o other]
(and (instance? js/Date other)
(identical? (.toString o) (.toString other)))))
(extend-type number
IEquiv
(-equiv [x o] (identical? x o)))
(declare with-meta)
(extend-type function
Fn
IMeta
(-meta [_] nil))
(extend-type default
IHash
(-hash [o]
(goog/getUid o)))
;;this is primitive because & emits call to array-seq
(defn inc
"Returns a number one greater than num."
[x] (cljs.core/+ x 1))
(declare deref)
(deftype Reduced [val]
IDeref
(-deref [o] val))
(defn reduced
"Wraps x in a way such that a reduce will terminate with the value x"
[x]
(Reduced. x))
(defn ^boolean reduced?
"Returns true if x is the result of a call to reduced"
[r]
(instance? Reduced r))
(defn ensure-reduced
"If x is already reduced?, returns it, else returns (reduced x)"
[x]
(if (reduced? x) x (reduced x)))
(defn unreduced
"If x is reduced?, returns (deref x), else returns x"
[x]
(if (reduced? x) (deref x) x))
;; generic to all refs
;; (but currently hard-coded to atom!)
(defn deref
[o]
(-deref o))
(defn- ci-reduce
"Accepts any collection which satisfies the ICount and IIndexed protocols and
reduces them without incurring seq initialization"
([cicoll f]
(let [cnt (-count cicoll)]
(if (zero? cnt)
(f)
(loop [val (-nth cicoll 0), n 1]
(if (< n cnt)
(let [nval (f val (-nth cicoll n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val)))))
([cicoll f val]
(let [cnt (-count cicoll)]
(loop [val val, n 0]
(if (< n cnt)
(let [nval (f val (-nth cicoll n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val))))
([cicoll f val idx]
(let [cnt (-count cicoll)]
(loop [val val, n idx]
(if (< n cnt)
(let [nval (f val (-nth cicoll n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val)))))
(defn- array-reduce
([arr f]
(let [cnt (alength arr)]
(if (zero? (alength arr))
(f)
(loop [val (aget arr 0), n 1]
(if (< n cnt)
(let [nval (f val (aget arr n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val)))))
([arr f val]
(let [cnt (alength arr)]
(loop [val val, n 0]
(if (< n cnt)
(let [nval (f val (aget arr n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val))))
([arr f val idx]
(let [cnt (alength arr)]
(loop [val val, n idx]
(if (< n cnt)
(let [nval (f val (aget arr n))]
(if (reduced? nval)
@nval
(recur nval (inc n))))
val)))))
(declare hash-coll cons RSeq List)
(defn ^boolean counted?
"Returns true if coll implements count in constant time"
[x] (satisfies? ICounted x))
(defn ^boolean indexed?
"Returns true if coll implements nth in constant time"
[x] (satisfies? IIndexed x))
(deftype IndexedSeqIterator [arr ^:mutable i]
Object
(hasNext [_]
(< i (alength arr)))
(next [_]
(let [ret (aget arr i)]
(set! i (inc i))
ret)))
(deftype IndexedSeq [arr i]
Object
(toString [coll]
(pr-str* coll))
(equiv [this other]
(-equiv this other))
ICloneable
(-clone [_] (IndexedSeq. arr i))
ISeqable
(-seq [this] this)
ASeq
ISeq
(-first [_] (aget arr i))
(-rest [_] (if (< (inc i) (alength arr))
(IndexedSeq. arr (inc i))
(list)))
INext
(-next [_] (if (< (inc i) (alength arr))
(IndexedSeq. arr (inc i))
nil))
ICounted
(-count [_] (- (alength arr) i))
IIndexed
(-nth [coll n]
(let [i (+ n i)]
(when (< i (alength arr))
(aget arr i))))
(-nth [coll n not-found]
(let [i (+ n i)]
(if (< i (alength arr))
(aget arr i)
not-found)))
ISequential
IEquiv
(-equiv [coll other] (equiv-sequential coll other))
IIterable
(-iterator [coll]
(IndexedSeqIterator. arr i))
ICollection
(-conj [coll o] (cons o coll))
IEmptyableCollection
(-empty [coll] (.-EMPTY List))
IReduce
(-reduce [coll f]
(array-reduce arr f (aget arr i) (inc i)))
(-reduce [coll f start]
(array-reduce arr f start i))
IHash
(-hash [coll] (hash-ordered-coll coll))
IReversible