/
core.cljs
6269 lines (5258 loc) · 177 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.string.StringBuffer :as gstringbuf]
[goog.object :as gobject]
[goog.array :as garray]))
(def *unchecked-if* false)
(def
^{:doc "Each runtime environment provides a diffenent way to print output.
Whatever function *print-fn* is bound to will be passed any
Strings which should be printed."}
*print-fn*
(fn [_]
(throw (js/Error. "No *print-fn* fn set for evaluation environment"))))
(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]
(js* "(~{x} != null && ~{x} !== false)"))
(defn type_satisfies_
"Internal - do not use!"
[p x]
(or
(aget p (goog.typeOf x))
(aget p "_")
false))
(defn is_proto_
[x]
(js* "(~{x}).constructor.prototype === ~{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 missing-protocol [proto obj]
(js/Error (js* "~{}+~{}+~{}+~{}+~{}+~{}"
"No protocol method " proto
" defined for type " (goog/typeOf obj) ": " obj)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; arrays ;;;;;;;;;;;;;;;;
(defn aclone
"Returns a javascript array, cloned from the passed in array"
[array-like]
#_(goog.array.clone array-like)
(js* "Array.prototype.slice.call(~{array-like})"))
(defn 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(arguments)"))
(defn make-array
([size]
(js* "new Array(~{size})"))
([type size]
(make-array size)))
(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))
(defn alength
"Returns the length of the array. Works on arrays of all types."
[array]
(.-length array))
(declare reduce)
(defn into-array
([aseq]
(into-array nil aseq))
([type aseq]
(reduce (fn [a x] (.push a x) a) (array) aseq)))
;;;;;;;;;;;;;;;;;;;;;;;;;;; core protocols ;;;;;;;;;;;;;
(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 s]
[this a b c d e f g h i j k l m n o p q s t]
[this a b c d e f g h i j k l m n o p q s t rest]))
(defprotocol ICounted
(-count [coll] "constant time count"))
(defprotocol IEmptyableCollection
(-empty [coll]))
(defprotocol ICollection
(-conj [coll o]))
#_(defprotocol IOrdinal
(-index [coll]))
(defprotocol IIndexed
(-nth [coll n] [coll n not-found]))
(defprotocol ISeq
(-first [coll])
(-rest [coll]))
(defprotocol ILookup
(-lookup [o k] [o k not-found]))
(defprotocol IAssociative
(-contains-key? [coll k])
#_(-entry-at [coll k])
(-assoc [coll k v]))
(defprotocol IMap
#_(-assoc-ex [coll k v])
(-dissoc [coll k]))
(defprotocol IMapEntry
(-key [coll])
(-val [coll]))
(defprotocol ISet
(-disjoin [coll v]))
(defprotocol IStack
(-peek [coll])
(-pop [coll]))
(defprotocol IVector
(-assoc-n [coll n val]))
(defprotocol IDeref
(-deref [o]))
(defprotocol IDerefWithTimeout
(-deref-with-timeout [o msec timeout-val]))
(defprotocol IMeta
(-meta [o]))
(defprotocol IWithMeta
(-with-meta [o meta]))
(defprotocol IReduce
(-reduce [coll f] [coll f start]))
(defprotocol IKVReduce
(-kv-reduce [coll f init]))
(defprotocol IEquiv
(-equiv [o other]))
(defprotocol IHash
(-hash [o]))
(defprotocol ISeqable
(-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
(-rseq [coll]))
(defprotocol ISorted
(-sorted-seq [coll ascending?])
(-sorted-seq-from [coll k ascending?])
(-entry-key [coll entry])
(-comparator [coll]))
(defprotocol IPrintable
(-pr-seq [o opts]))
(defprotocol IPending
(-realized? [d]))
(defprotocol IWatchable
(-notify-watches [this oldval newval])
(-add-watch [this key f])
(-remove-watch [this key]))
(defprotocol IEditableCollection
(-as-transient [coll]))
(defprotocol ITransientCollection
(-conj! [tcoll val])
(-persistent! [tcoll]))
(defprotocol ITransientAssociative
(-assoc! [tcoll key val]))
(defprotocol ITransientMap
(-dissoc! [tcoll key]))
(defprotocol ITransientVector
(-assoc-n! [tcoll n val])
(-pop! [tcoll]))
(defprotocol ITransientSet
(-disjoin! [tcoll v]))
;;;;;;;;;;;;;;;;;;; fundamentals ;;;;;;;;;;;;;;;
(defn ^boolean identical?
"Tests if 2 arguments are the same object"
[x y]
(cljs.core/identical? x y))
(declare first next)
(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] (or (identical? x y) (-equiv x y)))
([x y & more]
(if (= x y)
(if (next more)
(recur y (first more) (next more))
(= y (first more)))
false)))
(defn ^boolean nil?
"Returns true if x is nil, false otherwise."
[x]
(identical? x nil))
(defn type [x]
(if (or (nil? x) (undefined? x))
nil
(js* "(~{x}).constructor")))
;;;;;;;;;;;;;;;;;;; protocols on primitives ;;;;;;;;
(declare hash-map list equiv-sequential)
(extend-type nil
IEquiv
(-equiv [_ o] (nil? o))
ICounted
(-count [_] 0)
IEmptyableCollection
(-empty [_] nil)
ICollection
(-conj [_ o] (list o))
IPrintable
(-pr-seq [o] (list "nil"))
IIndexed
(-nth
([_ n] nil)
([_ n not-found] not-found))
ISeq
(-first [_] nil)
(-rest [_] (list))
ILookup
(-lookup
([o k] nil)
([o k not-found] not-found))
IAssociative
(-assoc [_ k v] (hash-map k v))
IMap
(-dissoc [_ k] nil)
ISet
(-disjoin [_ v] nil)
IStack
(-peek [_] nil)
(-pop [_] nil)
IMeta
(-meta [_] nil)
IWithMeta
(-with-meta [_ meta] nil)
IReduce
(-reduce
([_ f] (f))
([_ f start] start))
IHash
(-hash [o] 0))
(extend-type js/Date
IEquiv
(-equiv [o other] (identical? (. o (toString)) (. other (toString)))))
(extend-type number
IEquiv
(-equiv [x o] (identical? x o))
IHash
(-hash [o] o))
(extend-type boolean
IHash
(-hash [o] (js* "((~{o} === true) ? 1 : 0)")))
(extend-type function
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))
(defn- ci-reduce
"Accepts any collection which satisfies the ICount and IIndexed protocols and
reduces them without incurring seq initialization"
([cicoll f]
(if (= 0 (-count cicoll))
(f)
(loop [val (-nth cicoll 0), n 1]
(if (< n (-count cicoll))
(recur (f val (-nth cicoll n)) (inc n))
val))))
([cicoll f val]
(loop [val val, n 0]
(if (< n (-count cicoll))
(recur (f val (-nth cicoll n)) (inc n))
val)))
([cicoll f val idx]
(loop [val val, n idx]
(if (< n (-count cicoll))
(recur (f val (-nth cicoll n)) (inc n))
val))))
(declare hash-coll cons pr-str counted?)
(deftype IndexedSeq [a i]
Object
(toString [this]
(pr-str this))
ISeqable
(-seq [this] this)
ISeq
(-first [_] (aget a i))
(-rest [_] (if (< (inc i) (.-length a))
(IndexedSeq. a (inc i))
(list)))
ICounted
(-count [_] (- (.-length a) i))
IIndexed
(-nth [coll n]
(let [i (+ n i)]
(when (< i (.-length a))
(aget a i))))
(-nth [coll n not-found]
(let [i (+ n i)]
(if (< i (.-length a))
(aget a i)
not-found)))
ISequential
IEquiv
(-equiv [coll other] (equiv-sequential coll other))
ICollection
(-conj [coll o] (cons o coll))
IReduce
(-reduce [coll f]
(if (counted? a)
(ci-reduce a f (aget a i) (inc i))
(ci-reduce coll f (aget a i) 0)))
(-reduce [coll f start]
(if (counted? a)
(ci-reduce a f start i)
(ci-reduce coll f start 0)))
IHash
(-hash [coll] (hash-coll coll)))
(defn prim-seq [prim i]
(when-not (= 0 (.-length prim))
(IndexedSeq. prim i)))
(defn array-seq [array i]
(prim-seq array i))
(extend-type array
ISeqable
(-seq [array] (array-seq array 0))
ICounted
(-count [a] (.-length a))
IIndexed
(-nth
([array n]
(if (< n (.-length array)) (aget array n)))
([array n not-found]
(if (< n (.-length array)) (aget array n)
not-found)))
ILookup
(-lookup
([array k]
(aget array k))
([array k not-found]
(-nth array k not-found)))
IReduce
(-reduce
([array f]
(ci-reduce array f))
([array f start]
(ci-reduce array f start))))
(defn 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 coll
(-seq coll)))
(defn first
"Returns the first item in the collection. Calls seq on its
argument. If coll is nil, returns nil."
[coll]
(when-let [s (seq coll)]
(-first s)))
(defn rest
"Returns a possibly empty seq of the items after the first. Calls seq on its
argument."
[coll]
(-rest (seq coll)))
(defn 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 coll
(seq (rest coll))))
(defn second
"Same as (first (next x))"
[coll]
(first (next coll)))
(defn ffirst
"Same as (first (first x))"
[coll]
(first (first coll)))
(defn nfirst
"Same as (next (first x))"
[coll]
(next (first coll)))
(defn fnext
"Same as (first (next x))"
[coll]
(first (next coll)))
(defn nnext
"Same as (next (next x))"
[coll]
(next (next coll)))
(defn last
"Return the last item in coll, in linear time"
[s]
(if (next s)
(recur (next s))
(first s)))
(extend-type default
IEquiv
(-equiv [x o] (identical? x o)))
(defn ^boolean not
"Returns true if x is logical false, false otherwise."
[x] (if x false true))
(defn conj
"conj[oin]. Returns a new collection with the xs
'added'. (conj nil item) returns (item). The 'addition' may
happen at different 'places' depending on the concrete type."
([coll x]
(-conj coll x))
([coll x & xs]
(if xs
(recur (conj coll x) (first xs) (next xs))
(conj coll x))))
(defn empty
"Returns an empty collection of the same category as coll, or nil"
[coll]
(-empty coll))
(defn count
"Returns the number of items in the collection. (count nil) returns
0. Also works on strings, arrays, and Maps"
[coll]
(-count coll))
(defn nth
"Returns the value at the index. get returns nil if index out of
bounds, nth throws an exception unless not-found is supplied. nth
also works for strings, arrays, regex Matchers and Lists, and,
in O(n) time, for sequences."
([coll n]
(-nth coll (.floor js/Math n)))
([coll n not-found]
(-nth coll (.floor js/Math n) not-found)))
(defn get
"Returns the value mapped to key, not-found or nil if key not present."
([o k]
(-lookup o k))
([o k not-found]
(-lookup o k not-found)))
(defn assoc
"assoc[iate]. When applied to a map, returns a new map of the
same (hashed/sorted) type, that contains the mapping of key(s) to
val(s). When applied to a vector, returns a new vector that
contains val at index."
([coll k v]
(-assoc coll k v))
([coll k v & kvs]
(let [ret (assoc coll k v)]
(if kvs
(recur ret (first kvs) (second kvs) (nnext kvs))
ret))))
(defn dissoc
"dissoc[iate]. Returns a new map of the same (hashed/sorted) type,
that does not contain a mapping for key(s)."
([coll] coll)
([coll k]
(-dissoc coll k))
([coll k & ks]
(let [ret (dissoc coll k)]
(if ks
(recur ret (first ks) (next ks))
ret))))
(defn with-meta
"Returns an object of the same type and value as obj, with
map m as its metadata."
[o meta]
(-with-meta o meta))
(defn meta
"Returns the metadata of obj, returns nil if there is no metadata."
[o]
(when (satisfies? IMeta o)
(-meta o)))
(defn peek
"For a list or queue, same as first, for a vector, same as, but much
more efficient than, last. If the collection is empty, returns nil."
[coll]
(-peek coll))
(defn pop
"For a list or queue, returns a new list/queue without the first
item, for a vector, returns a new vector without the last item.
Note - not the same as next/butlast."
[coll]
(-pop coll))
(defn disj
"disj[oin]. Returns a new set of the same (hashed/sorted) type, that
does not contain key(s)."
([coll] coll)
([coll k]
(-disjoin coll k))
([coll k & ks]
(let [ret (disj coll k)]
(if ks
(recur ret (first ks) (next ks))
ret))))
(defn hash [o]
(-hash o))
(defn ^boolean empty?
"Returns true if coll has no items - same as (not (seq coll)).
Please use the idiom (seq x) rather than (not (empty? x))"
[coll] (not (seq coll)))
(defn ^boolean coll?
"Returns true if x satisfies ICollection"
[x]
(if (nil? x)
false
(satisfies? ICollection x)))
(defn ^boolean set?
"Returns true if x satisfies ISet"
[x]
(if (nil? x)
false
(satisfies? ISet x)))
(defn ^boolean associative?
"Returns true if coll implements Associative"
[x] (satisfies? IAssociative x))
(defn ^boolean sequential?
"Returns true if coll satisfies ISequential"
[x] (satisfies? ISequential x))
(defn ^boolean counted?
"Returns true if coll implements count in constant time"
[x] (satisfies? ICounted x))
(defn ^boolean map?
"Return true if x satisfies IMap"
[x]
(if (nil? x)
false
(satisfies? IMap x)))
(defn ^boolean vector?
"Return true if x satisfies IVector"
[x] (satisfies? IVector x))
;;;;;;;;;;;;;;;;;;;; js primitives ;;;;;;;;;;;;
(defn js-obj
([]
(js* "{}"))
([& keyvals]
(apply gobject/create keyvals)))
(defn js-keys [obj]
(let [keys (array)]
(goog.object/forEach obj (fn [val key obj] (.push keys key)))
keys))
(defn js-delete [obj key]
(js* "delete ~{obj}[~{key}]"))
(defn- array-copy
([from i to j len]
(loop [i i j j len len]
(if (zero? len)
to
(do (aset to j (aget from i))
(recur (inc i) (inc j) (dec len)))))))
(defn- array-copy-downward
([from i to j len]
(loop [i (+ i (dec len)) j (+ j (dec len)) len len]
(if (zero? len)
to
(do (aset to j (aget from i))
(recur (dec i) (dec j) (dec len)))))))
;;;;;;;;;;;;;;;; preds ;;;;;;;;;;;;;;;;;;
(def ^:private lookup-sentinel (js-obj))
(defn ^boolean false?
"Returns true if x is the value false, false otherwise."
[x] (cljs.core/false? x))
(defn ^boolean true?
"Returns true if x is the value true, false otherwise."
[x] (cljs.core/true? x))
(defn ^boolean undefined? [x]
(cljs.core/undefined? x))
(defn ^boolean instance? [t o]
(js* "(~{o} != null && (~{o} instanceof ~{t} || ~{o}.constructor === ~{t} || ~{t} === Object))"))
(defn ^boolean seq?
"Return true if s satisfies ISeq"
[s]
(if (nil? s)
false
(satisfies? ISeq s)))
(defn ^boolean seqable?
"Return true if s satisfies ISeqable"
[s]
(if (nil? s)
false
(satisfies? ISeqable s)))
(defn ^boolean boolean [x]
(if x true false))
(defn ^boolean string? [x]
(and (goog/isString x)
(not (or (= (.charAt x 0) \uFDD0)
(= (.charAt x 0) \uFDD1)))))
(defn ^boolean keyword? [x]
(and (goog/isString x)
(= (.charAt x 0) \uFDD0)))
(defn ^boolean symbol? [x]
(and (goog/isString x)
(= (.charAt x 0) \uFDD1)))
(defn ^boolean number? [n]
(goog/isNumber n))
(defn ^boolean fn? [f]
(goog/isFunction f))
(defn ^boolean ifn? [f]
(or (fn? f) (satisfies? IFn f)))
(defn ^boolean integer?
"Returns true if n is an integer. Warning: returns true on underflow condition."
[n]
(and (number? n)
(js* "(~{n} == ~{n}.toFixed())")))
(defn ^boolean contains?
"Returns true if key is present in the given collection, otherwise
returns false. Note that for numerically indexed collections like
vectors and arrays, this tests if the numeric key is within the
range of indexes. 'contains?' operates constant or logarithmic time;
it will not perform a linear search for a value. See also 'some'."
[coll v]
(if (identical? (-lookup coll v lookup-sentinel) lookup-sentinel)
false
true))
(defn find
"Returns the map entry for key, or nil if key not present."
[coll k]
(when (and coll
(associative? coll)
(contains? coll k))
[k (-lookup coll k)]))
(defn ^boolean distinct?
"Returns true if no two of the arguments are ="
([x] true)
([x y] (not (= x y)))
([x y & more]
(if (not (= x y))
(loop [s #{x y} xs more]
(let [x (first xs)
etc (next xs)]
(if xs
(if (contains? s x)
false
(recur (conj s x) etc))
true)))
false)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Seq fns ;;;;;;;;;;;;;;;;
(defn compare
"Comparator. Returns a negative number, zero, or a positive number
when x is logically 'less than', 'equal to', or 'greater than'
y. Uses google.array.defaultCompare for objects of the same type
and special-cases nil to be less than any other object."
[x y]
(cond
(identical? (type x) (type y)) (garray/defaultCompare x y)
(nil? x) -1
(nil? y) 1
:else (throw (js/Error. "compare on non-nil objects of different types"))))
(defn ^:private fn->comparator
"Given a fn that might be boolean valued or a comparator,
return a fn that is a comparator."
[f]
(if (= f compare)
compare
(fn [x y]
(let [r (f x y)]
(if (number? r)
r
(if r
-1
(if (f y x) 1 0)))))))
(declare to-array)
(defn sort
"Returns a sorted sequence of the items in coll. Comp can be
boolean-valued comparison funcion, or a -/0/+ valued comparator.
Comp defaults to compare."
([coll]
(sort compare coll))
([comp coll]
(if (seq coll)
(let [a (to-array coll)]
;; matching Clojure's stable sort, though docs don't promise it
(garray/stableSort a (fn->comparator comp))
(seq a))
())))
(defn sort-by
"Returns a sorted sequence of the items in coll, where the sort
order is determined by comparing (keyfn item). Comp can be
boolean-valued comparison funcion, or a -/0/+ valued comparator.
Comp defaults to compare."
([keyfn coll]
(sort-by keyfn compare coll))
([keyfn comp coll]
(sort (fn [x y] ((fn->comparator comp) (keyfn x) (keyfn y))) coll)))
(defn reduce
"f should be a function of 2 arguments. If val 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. If coll contains no
items, f must accept no arguments as well, and reduce returns the
result of calling f with no arguments. If coll has only 1 item, it
is returned and f is not called. If val is supplied, returns the
result of applying f to val and the first item in coll, then
applying f to that result and the 2nd item, etc. If coll contains no
items, returns val and f is not called."
([f coll]
(-reduce coll f))
([f val coll]
(-reduce coll f val)))
(defn reduce-kv
"Reduces an associative collection. f should be a function of 3
arguments. Returns the result of applying f to init, the first key
and the first value in coll, then applying f to that result and the
2nd key and value, etc. If coll contains no entries, returns init
and f is not called. Note that reduce-kv is supported on vectors,
where the keys will be the ordinals."
([f init coll]
(-kv-reduce coll f init)))
; simple reduce based on seqs, used as default
(defn- seq-reduce
([f coll]
(if-let [s (seq coll)]
(reduce f (first s) (next s))
(f)))
([f val coll]
(loop [val val, coll (seq coll)]
(if coll
(recur (f val (first coll)) (next coll))
val))))
(extend-type default
IReduce
(-reduce
([coll f]
(seq-reduce f coll))
([coll f start]
(seq-reduce f start coll))))
(deftype Reduced [val]
IDeref
(-deref [o] val))
(defn reduced?
"Returns true if x is the result of a call to reduced"
[r]
(instance? Reduced r))
(defn reduced
"Wraps x in a way such that a reduce will terminate with the value x"
[x]
(Reduced. x))
;;; Math - variadic forms will not work until the following implemented:
;;; first, next, reduce
(defn +
"Returns the sum of nums. (+) returns 0."
([] 0)
([x] x)
([x y] (cljs.core/+ x y))
([x y & more] (reduce + (cljs.core/+ x y) more)))
(defn -
"If no ys are supplied, returns the negation of x, else subtracts
the ys from x and returns the result."
([x] (cljs.core/- x))
([x y] (cljs.core/- x y))
([x y & more] (reduce - (cljs.core/- x y) more)))
(defn *
"Returns the product of nums. (*) returns 1."
([] 1)
([x] x)
([x y] (cljs.core/* x y))
([x y & more] (reduce * (cljs.core/* x y) more)))
(defn /
"If no denominators are supplied, returns 1/numerator,
else returns numerator divided by all of the denominators."
([x] (/ 1 x))
([x y] (js* "(~{x} / ~{y})")) ;; FIXME: waiting on cljs.core//
([x y & more] (reduce / (/ x y) more)))
(defn ^boolean <
"Returns non-nil if nums are in monotonically increasing order,
otherwise false."
([x] true)
([x y] (cljs.core/< x y))
([x y & more]
(if (cljs.core/< x y)
(if (next more)
(recur y (first more) (next more))
(cljs.core/< y (first more)))
false)))
(defn ^boolean <=
"Returns non-nil if nums are in monotonically non-decreasing order,
otherwise false."
([x] true)
([x y] (cljs.core/<= x y))
([x y & more]
(if (cljs.core/<= x y)
(if (next more)
(recur y (first more) (next more))
(cljs.core/<= y (first more)))
false)))
(defn ^boolean >
"Returns non-nil if nums are in monotonically decreasing order,
otherwise false."
([x] true)
([x y] (cljs.core/> x y))
([x y & more]
(if (cljs.core/> x y)
(if (next more)
(recur y (first more) (next more))
(cljs.core/> y (first more)))