I have read (doc transient), (doc assoc!) and (doc persistent!), and I
don't see what I'm missing, which is why I came here for help.

To my understanding, the first creates a transient collection, which
cannot be used in other threads, and cannot be used after converting
back to a persistent collection.

user=> (doc assoc!)
-------------------------
clojure.core/assoc!
([coll key val] [coll key val & kvs])
  Alpha - subject to change.
  When applied to a transient map, adds mapping of key(s) to
  val(s). When applied to a transient vector, sets the val at index.
  Note - index must be <= (count vector). Returns coll.

assoc!'s doc says that it adds mapping of keys to values when applied
to a map, which is what this code demonstrates.

user=> (def t (transient {}))
#'user/t
user=> (assoc! t 1 1)
#<TransientArrayMap clojure.lang.PersistentArrayMap
$TransientArrayMap@1352367>
user=> (assoc! t 2 2)
#<TransientArrayMap clojure.lang.PersistentArrayMap
$TransientArrayMap@1352367>
user=> (def p (persistent! t))
#'user/p
user=> p
{1 1, 2 2}

It says that it returns col. Does that imply that I'm supposed to hang
on to col?




On Jan 27, 3:50 pm, Kevin Downey <redc...@gmail.com> wrote:
> Please don't use transients unless you read and understand the
> documentation.
> On Jan 27, 2012 12:41 PM, "Bill Robertson" <billrobertso...@gmail.com>
> wrote:
>
>
>
>
>
>
>
> > I don't understand why the two functions below (recurs and transi) do
> > not produce the same result. To the best of my understanding doseq
> > will consume an entire sequence as demonstrated here:
>
> > user=> (doseq [x (range 0 10)] (print x))
> > 0123456789nil
>
> > user=> *clojure-version*
> > {:major 1, :minor 3, :incremental 0, :qualifier nil}
>
> > user=> (defn recurs [dest src]
> >  (if-let [element (first src)]
> >    (recur (assoc dest element element) (rest src))
> >    dest))
> > #'user/recurs
> > user=> (count (recurs {} (range 0 20000)))
> > 20000
>
> > user=> (defn transi [dest src]
> >  (let [temp (transient dest)]
> >    (doseq [x src]
> >      (assoc! temp x x))
> >    (persistent! temp)))
> > #'user/transi
> > user=> (count (transi {} (range 0 20000)))
> > 8
> > user=> (count (transi {} (range 0 20)))
> > 8
> > user=> (count (transi {} (range 0 8)))
> > 8
> > user=> (count (transi {} (range 0 7)))
> > 7
>
> > Any ideas about why the first function does not behave the same as the
> > second?
>
> > Thanks
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Clojure" group.
> > To post to this group, send email to clojure@googlegroups.com
> > Note that posts from new members are moderated - please be patient with
> > your first post.
> > To unsubscribe from this group, send email to
> > clojure+unsubscr...@googlegroups.com
> > For more options, visit this group at
> >http://groups.google.com/group/clojure?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to