We're updating the issue view to help you get more done. 

Subvecs of primitive vectors cannot be reduced

Description

Reduce doesn't work on subvecs of primitive vectors.

1 2 3 4 (let [prim-vec (into (vector-of :long) (range 10000))] (reduce + (subvec prim-vec 1 500))) ->> ClassCastException clojure.core.Vec cannot be cast to clojure.lang.PersistentVector clojure.lang.APersistentVector$SubVector.iterator (APersistentVector.java:523)

If reduce on a subvec doesn't work then neither will nice ops like fold.

Cause: RT.subvec() creates an APersistentVector$SubVector. SubVector.iterator() assumes the source vector is a PersistentVector, however a primitive vector is a Vec (which is not a PersistentVector). This causes a class cast exception as observed on any attempt to iterate over the subvector.

Approach:
1. Provide a generic ranged iterator for APersistentVector, that can be used by SubVector
2. Make the iterator() method for APersistentVector$SubVector use this new iterator where possible (i.e. whenever the source vector is an APersistentVector). If not, the generic super.iterator() method is used (which is slower, but safe for any source vector that implements IPersistentVector)

Patch: clj-1082-patch-v3.diff

Screened by: Alex Miller

Environment

1.7.0-08, OS X 10.8

Status

Assignee

Unassigned

Reporter

Ghadi Shayban

Labels

Approval

Ok

Patch

Code and Test

Fix versions

Priority

Major