ClojureDocs

Nav

Namespaces

transient

clojure.core

Available since 1.1 (source)
  • (transient coll)
Returns a new, transient version of the collection, in constant time.
3 Examples
;; As seen on http://clojure.org/transients
;; array is initially made transient, modified, then finally made persistent.
;; See assoc! for further discussion of why it must be done this way.
;; Also see one example for conj! that contains a detailed example
;; of a wrong result that can occur if you do not use its return value.

(defn vrange2 [n]
  (loop [i 0 v (transient [])]
    (if (< i n)
      (recur (inc i) (conj! v i))
      (persistent! v))))

user=> (vrange2 10)
[0 1 2 3 4 5 6 7 8 9]
user> (def foo (transient [1 2 3]))
#'user/foo
user> (get foo 0)
1
user> (count foo)
3
user> (nth foo 0)
1
user> (def bar (transient {:honda 7 :kagawa 23 :ienaga 14}))
#'user/bar
user> (get bar :kagawa)
23
user> (count bar)
3

;; There is a known bug in Clojure 1.3 thru 1.6 where contains? always returns
;; false for transients.  http://dev.clojure.org/jira/browse/CLJ-700
;; contains? works fine for persistent data structures.
user> (contains? bar :kagawa)  ; Caution! 
false
user> (def bar2 (persistent! bar))
#'user/bar2
user> (contains? bar2 :kagawa) ; Caution!
true
;; transient works only on vectors and maps

user=> (transient (map inc (range 10)))
;; ClassCastException clojure.lang.LazySeq cannot be cast to clojure.lang.IEditableCollection

user=> (transient (range 10))
;; ClassCastException clojure.lang.LongRange cannot be cast to clojure.lang.IEditableCollection

;; but these will work

user=> (transient (into [] (map inc (range 10))))
;; #object[clojure.lang.PersistentVector$TransientVector 0x3ed4e323 "clojure.lang.PersistentVector$TransientVector@3ed4e323"]

user=> (transient (mapv inc (range 10)))
;; #object[clojure.lang.PersistentVector$TransientVector 0x4d978d5f "clojure.lang.PersistentVector$TransientVector@4d978d5f"]

user=> (transient (into {} (zipmap (range 3) (range 3))))
;; #object[clojure.lang.PersistentArrayMap$TransientArrayMap 0xa4153b7 "clojure.lang.PersistentArrayMap$TransientArrayMap@a4153b7"]
;; produce {0 0, 1 1, 2 2}
See Also

Returns a new, persistent version of the transient collection, in constant time. The transient col...

Added by gstamp

Adds x to the transient collection, and return coll. The 'addition' may happen at different 'place...

Added by gstamp

When applied to a transient map, adds mapping of key(s) to val(s). When applied to a transient vec...

Added by kumarshantanu

Removes the last item from a transient vector. If the collection is empty, throws an exception. Re...

Added by kumarshantanu

Returns a transient map that doesn't contain a mapping for key(s).

Added by kumarshantanu

disj[oin]. Returns a transient set of the same (hashed/sorted) type, that does not contain key(s).

Added by jjcomer
0 Notes
No notes for transient