Browse files

don't extend native strings & arrays to IReduce

  • Loading branch information...
1 parent e0f6fd5 commit a825a12dffc984342e97785c504f2ab4e6e4d7e1 @swannodette swannodette committed Apr 29, 2013
Showing with 44 additions and 38 deletions.
  1. +38 −30 src/cljs/cljs/core.cljs
  2. +6 −8 test/cljs/cljs/core_test.cljs
View
68 src/cljs/cljs/core.cljs
@@ -703,21 +703,17 @@ reduces them without incurring seq initialization"
(if (< n (alength array)) (aget array n)))
([array n not-found]
(if (< n (alength array)) (aget array n)
- not-found)))
+ not-found)))
ILookup
(-lookup
([array k]
- (aget array k))
+ (when (< k (alength array))
+ (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))))
+ (if (< k (alength array))
+ (aget array k)
+ not-found))))
(declare with-meta)
@@ -1257,12 +1253,28 @@ reduces them without incurring seq initialization"
applying f to that result and the 2nd item, etc. If coll contains no
items, returns val and f is not called."
([f coll]
- (if (satisfies? IReduce coll)
+ (cond
+ (satisfies? IReduce coll)
(-reduce coll f)
+
+ (or ^boolean (goog.isArray coll)
+ (and ^boolean (goog.isString coll)
+ (not (keyword? coll))))
+ (ci-reduce coll f)
+
+ :else
(seq-reduce f coll)))
([f val coll]
- (if (satisfies? IReduce coll)
+ (cond
+ (satisfies? IReduce coll)
(-reduce coll f val)
+
+ (or ^boolean (goog.isArray coll)
+ (and ^boolean (goog.isString coll)
+ (not (keyword? coll))))
+ (ci-reduce coll f val)
+
+ :else
(seq-reduce f val coll))))
(defn reduce-kv
@@ -1906,24 +1918,20 @@ reduces them without incurring seq initialization"
IIndexed
(-nth
([string n]
- (if (< n (-count string)) (.charAt string n)))
+ (if (< n (.-length string)) (.charAt string n)))
([string n not-found]
- (if (< n (-count string)) (.charAt string n)
- not-found)))
+ (if (< n (.-length string)) (.charAt string n)
+ not-found)))
ILookup
(-lookup
([string k]
- (-nth string k))
- ([string k not_found]
- (-nth string k not_found)))
-
- IReduce
- (-reduce
- ([string f]
- (ci-reduce string f))
- ([string f start]
- (ci-reduce string f start))))
+ (when (< k (.-length string))
+ (aget string k)))
+ ([string k not-found]
+ (if (< k (.-length string))
+ (aget string k)
+ not-found))))
(deftype Keyword [k]
IFn
@@ -1948,11 +1956,11 @@ reduces them without incurring seq initialization"
(get coll (.toString this) not-found))))
(set! js/String.prototype.apply
- (fn
- [s args]
- (if (< (count args) 2)
- (get (aget args 0) s)
- (get (aget args 0) s (aget args 1)))))
+ (fn
+ [s args]
+ (if (< (alength args) 2)
+ (get (aget args 0) s)
+ (get (aget args 0) s (aget args 1)))))
; could use reify
;;; LazySeq ;;;
View
14 test/cljs/cljs/core_test.cljs
@@ -269,14 +269,12 @@
(assert (= "harriet" (-lookup "abcd" 4 "harriet")))
(assert (= 4 (-lookup (array 1 2 3 4) 3)))
(assert (= "zot" (-lookup (array 1 2 3 4) 4 "zot")))
- (assert (= 10 (-reduce (array 1 2 3 4) +)))
- (assert (= 20 (-reduce (array 1 2 3 4) + 10)))
- (assert (= "cabd" (let
- [jumble (fn [a b] (str (apply str (reverse (str a))) b))]
- (-reduce "abcd" jumble))))
- (assert (= "cafrogbd" (let
- [jumble (fn [a b] (str (apply str (reverse (str a))) b))]
- (-reduce "abcd" jumble "frog"))))
+ (assert (= 10 (reduce + (array 1 2 3 4))))
+ (assert (= 20 (reduce + 10 (array 1 2 3 4))))
+ (assert (= "cabd" (let [jumble (fn [a b] (str (apply str (reverse (str a))) b))]
+ (reduce jumble "abcd"))))
+ (assert (= "cafrogbd" (let [jumble (fn [a b] (str (apply str (reverse (str a))) b))]
+ (reduce jumble "frog" "abcd"))))
(assert (= [0 0 1 0 1]
[(bit-and 1 0)
(bit-and 0 0)

0 comments on commit a825a12

Please sign in to comment.