<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Mapping every second item</title>
	<atom:link href="http://clj-me.cgrand.net/2009/04/17/mapping-every-second-item/feed/" rel="self" type="application/rss+xml" />
	<link>http://clj-me.cgrand.net/2009/04/17/mapping-every-second-item/</link>
	<description>When the pupil is ready to learn, a teacher will appear.</description>
	<lastBuildDate>Tue, 07 Sep 2010 05:36:26 +0200</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Christophe Grand</title>
		<link>http://clj-me.cgrand.net/2009/04/17/mapping-every-second-item/comment-page-1/#comment-27</link>
		<dc:creator>Christophe Grand</dc:creator>
		<pubDate>Sat, 18 Apr 2009 07:16:00 +0000</pubDate>
		<guid isPermaLink="false">http://clj-me.cgrand.net/2009/04/17/mapping-every-second-item/#comment-27</guid>
		<description>Michael,&lt;br /&gt;&lt;br /&gt;You can redefine cycle using seq-utils/rec-seq:&lt;br /&gt;(defn my-cycle [coll] (rec-seq c (concat coll c))) which create a cyclic seq of only two  conses. &lt;br /&gt;&lt;br /&gt;(map #(%1 %2) (my-cycle [...]) coll) is obviously faster: no garbage trumps easily collectable garbage.</description>
		<content:encoded><![CDATA[<p>Michael,</p>
<p>You can redefine cycle using seq-utils/rec-seq:<br />(defn my-cycle [coll] (rec-seq c (concat coll c))) which create a cyclic seq of only two  conses. </p>
<p>(map #(%1 %2) (my-cycle [...]) coll) is obviously faster: no garbage trumps easily collectable garbage.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Christophe Grand</title>
		<link>http://clj-me.cgrand.net/2009/04/17/mapping-every-second-item/comment-page-1/#comment-26</link>
		<dc:creator>Christophe Grand</dc:creator>
		<pubDate>Sat, 18 Apr 2009 07:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://clj-me.cgrand.net/2009/04/17/mapping-every-second-item/#comment-26</guid>
		<description>Hi Michael,&lt;br /&gt;&lt;br /&gt;cycle would not create a 10,000-items collection but would lazily realize the first 10,000 conses of an infinite seq (and would not keep a reference to them, making them GCable as soon as they are processed so they have a very short life-span). &lt;br /&gt;&lt;br /&gt;I didn&#039;t benchmark them but one can count how many transient objects are created (since it&#039;s what worries you) by each other approach:&lt;br /&gt;&lt;br /&gt;(interleave (map f (take-nth 2 coll)) (take-nth 2 (rest coll))) would create 3*5,000 transient conses (each take-nth + map) -- and I&#039;m not counting seq being called twice on coll.&lt;br /&gt;&lt;br /&gt;(mapcat (fn [[a b]] [(f a) b]) (partition 2 coll)) would create 3*5,000 conses (partition yields a seq of 5000 seqs of 2 conses), the map part of mapcat would create 5000 conses and 5000 vectors. The concat part would create 10000 transient conses (seq on each vector)&lt;br /&gt;&lt;br /&gt;NB: I used &quot;cons&quot; rather liberally to mean &quot;objects which implement clojure.lang.ISeq&quot;.&lt;br /&gt;&lt;br /&gt;NB2: A quick pointless benchmark on (range 100000) seems to show that (map #(%1 %2) (cycle [...]) coll) is indeed faster but what I was after when I wrote this post is conciseness.</description>
		<content:encoded><![CDATA[<p>Hi Michael,</p>
<p>cycle would not create a 10,000-items collection but would lazily realize the first 10,000 conses of an infinite seq (and would not keep a reference to them, making them GCable as soon as they are processed so they have a very short life-span). </p>
<p>I didn&#8217;t benchmark them but one can count how many transient objects are created (since it&#8217;s what worries you) by each other approach:</p>
<p>(interleave (map f (take-nth 2 coll)) (take-nth 2 (rest coll))) would create 3*5,000 transient conses (each take-nth + map) &#8212; and I&#8217;m not counting seq being called twice on coll.</p>
<p>(mapcat (fn [[a b]] [(f a) b]) (partition 2 coll)) would create 3*5,000 conses (partition yields a seq of 5000 seqs of 2 conses), the map part of mapcat would create 5000 conses and 5000 vectors. The concat part would create 10000 transient conses (seq on each vector)</p>
<p>NB: I used &#8220;cons&#8221; rather liberally to mean &#8220;objects which implement clojure.lang.ISeq&#8221;.</p>
<p>NB2: A quick pointless benchmark on (range 100000) seems to show that (map #(%1 %2) (cycle [...]) coll) is indeed faster but what I was after when I wrote this post is conciseness.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: michaelr</title>
		<link>http://clj-me.cgrand.net/2009/04/17/mapping-every-second-item/comment-page-1/#comment-25</link>
		<dc:creator>michaelr</dc:creator>
		<pubDate>Sat, 18 Apr 2009 00:29:00 +0000</pubDate>
		<guid isPermaLink="false">http://clj-me.cgrand.net/2009/04/17/mapping-every-second-item/#comment-25</guid>
		<description>Hey CHRISTOPHE,&lt;br /&gt;&lt;br /&gt;I really enjoy reading the examples you post.&lt;br /&gt;&lt;br /&gt;Question: Suppose &#039;coll&#039; is a collection of 10,000 items, this line of code will create another collection using &#039;cycle&#039; of 10,000 items. Isn&#039;t it a bit expensive from performance point of view?</description>
		<content:encoded><![CDATA[<p>Hey CHRISTOPHE,</p>
<p>I really enjoy reading the examples you post.</p>
<p>Question: Suppose &#8216;coll&#8217; is a collection of 10,000 items, this line of code will create another collection using &#8216;cycle&#8217; of 10,000 items. Isn&#8217;t it a bit expensive from performance point of view?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
