On 21 June 2013 13:37, Jim - FooBar(); <jimpil1...@gmail.com> wrote: > Hi all, > > what do you guys do when you want to map a fn over a list but you want to > produce a list as the result (not vector or lazy-seq). What is the fastest > way of doing this? > > My first attempt was this: > > (->> (mapv f coll) > rseq ;;reverse fast > (into '())) > > but I quickly realised that lists do not have transient counterparts. > Therefore, the call to 'into' will be slow...
Your logic here is incorrect. To say "transients == fast, persistents == slow" is to grossly oversimplify things. The default data structures are *fast*. Transients are offered, where they make sense, as optimization to make things go *faster*. Note that this does not change the big-O complexity. Transients make sense for vectors and maps since they are 32-wide tree-based data structures, and often a conj or assoc operation on a persistent data structure will entirely replace a tree node with a copy with one entry replaced. This operation can be optimized to a mutable bash-the-node-in-place operation provided you know you won't want the old value afterwards. There is no analogous case in lists. Every conj operation necessarly allocates a new cons cell; there is no scope for saving time by bashing in place. Therefore, there is no transient for lists. The fact that no transient optimization exists for lists does not mean that lists are slow. This logic simply does not follow. > in addition, I'm performing 2 > passes with this... > > any ideas? If you're afraid of performing 2 passes, you could take advantage of laziness: (->> (rseq v) (map f) (into '())) Since map is lazy, this will only perform one pass (though it appears on first glance that it performs two). but I would be wary of your use of the word "fastest" at the top. Normally on the JVM, the answer to a "what is the fastest way to..?" question is "it depends". A much easier question to answer is "Is X fast enough?" Phil > > Jim > > -- > -- > 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 unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > -- -- 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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.