Skip to content

Commit ed99e53

Browse files
committed
Merge pull request #441 from thomasmulvaney/range-seq
Improves the performance of range dramatically
2 parents b3e8b21 + 793e074 commit ed99e53

1 file changed

Lines changed: 26 additions & 9 deletions

File tree

pixie/stdlib.pxi

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -890,9 +890,18 @@ If further arguments are passed, invokes the method named by symbol, passing the
890890
:signatures [[coll]]
891891
:added "0.1"}
892892
[coll]
893-
(if (next coll)
894-
(recur (next coll))
895-
(first coll)))
893+
(cond
894+
(satisfies? IIndexed coll)
895+
(when (pos? (count coll))
896+
(nth coll (dec (count coll))))
897+
898+
(satisfies? ISeq coll)
899+
(if (next coll)
900+
(recur (next coll))
901+
(first coll))
902+
903+
(satisfies? ISeqable coll)
904+
(recur (seq coll))))
896905

897906
(defn butlast
898907
{:doc "Returns all elements but the last from the collection."
@@ -2050,18 +2059,26 @@ For more information, see http://clojure.org/special_forms#binding-forms"}
20502059
(if (cmp val stop)
20512060
val
20522061
not-found)))
2062+
ISeq
2063+
(-first [this]
2064+
(when (not= start stop)
2065+
start))
2066+
(-next [this]
2067+
(let [i (+ step start)]
2068+
(when (or (and (> step 0) (< i stop))
2069+
(and (< step 0) (> i stop))
2070+
(and (= step 0)))
2071+
(range i stop step))))
20532072
ISeqable
2054-
(-seq [self]
2055-
(when (or (and (> step 0) (< start stop))
2056-
(and (< step 0) (> start stop)))
2057-
(cons start (lazy-seq* #(range (+ start step) stop step))))))
2073+
(-seq [self] self))
20582074

20592075
(extend -str Range
20602076
(fn [v]
2061-
(-str (seq v))))
2077+
(str "(" (transduce (interpose " ") string-builder v) ")")))
2078+
20622079
(extend -repr Range
20632080
(fn [v]
2064-
(-repr (seq v))))
2081+
(str "(" (transduce (interpose " ") string-builder v) ")")))
20652082

20662083
(defn range
20672084
{:doc "Returns a range of numbers."

0 commit comments

Comments
 (0)