<?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: Enumerating Enumerable: Enumerable#drop_while</title>
	<atom:link href="http://www.globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/</link>
	<description>Tech Evangelist Joey deVilla on software development, tech news and other nerdy stuff</description>
	<lastBuildDate>Thu, 19 Nov 2009 18:44:37 -0400</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Gianni Chiappetta</title>
		<link>http://www.globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/comment-page-1/#comment-1269</link>
		<dc:creator>Gianni Chiappetta</dc:creator>
		<pubDate>Mon, 28 Jul 2008 15:42:06 +0000</pubDate>
		<guid isPermaLink="false">http://globalnerdy.com/?p=1773#comment-1269</guid>
		<description>Ahh I see, thanks for clearing that up Joey!</description>
		<content:encoded><![CDATA[<p>Ahh I see, thanks for clearing that up Joey!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joey deVilla</title>
		<link>http://www.globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/comment-page-1/#comment-1264</link>
		<dc:creator>Joey deVilla</dc:creator>
		<pubDate>Sat, 26 Jul 2008 14:15:42 +0000</pubDate>
		<guid isPermaLink="false">http://globalnerdy.com/?p=1773#comment-1264</guid>
		<description>&lt;strong&gt;@Gianni Chiappetta:&lt;/strong&gt; They&#039;re similar, but not the same.

&lt;code&gt;drop_while&lt;/code&gt; and &lt;code&gt;take_while&lt;/code&gt; go through the collection&#039;s items in order and stop as soon as the condition in the block is no longer met, whereas &lt;code&gt;reject&lt;/code&gt; and &lt;code&gt;select&lt;/code&gt; don&#039;t operate under that constraint.

Here&#039;s an example showing the difference:
&lt;code&gt;&lt;pre&gt;
temperatures = [28, 25, 30, 22, 27]
=&gt; [28, 25, 30, 22, 27]

# &quot;reject&quot; returns an array that is made by using the
# original collection and discarding all of its elements
# that meet the condition in the block
temperatures.reject {&#124;temperature&#124; temperature &lt; 28}
=&gt; [28, 30]

# &quot;drop_while&quot; is like &quot;reject&quot;, but when working
# through the given collection in order, it stops discarding
# elements as soon as it encounters an element that
# *doesn&#039;t* meet the condition in the block
temperatures.drop_while {&#124;temperature&#124; temperature &lt; 28}
=&gt; [28, 25, 30, 22, 27]
&lt;/pre&gt;&lt;/code&gt;

In the &lt;code&gt;drop_while&lt;/code&gt; example above, the first element, &lt;code&gt;28&lt;/code&gt;, causes the condition in the block to return &lt;code&gt;false&lt;/code&gt;. &lt;code&gt;drop_while&lt;/code&gt; stops checking the remaining elements and returns the result array. So in the case of the &lt;code&gt;temperatures&lt;/code&gt; array above, &lt;code&gt;drop_while&lt;/code&gt;&#039;s result is equal to the original array.

&lt;code&gt;drop_while&lt;/code&gt; will return the same results as &lt;code&gt;reject&lt;/code&gt; if the array is sorted in such a way that all the elements that meet the condition in the block are moved to the beginning of the collection. In the case of the &lt;code&gt;temperatures&lt;/code&gt; array, that&#039;s easy:

&lt;code&gt;&lt;pre&gt;
temperatures.sort.drop_while {&#124;temperature&#124; temperature &lt; 28}
=&gt; [28, 30]
&lt;/pre&gt;&lt;/code&gt;

The practical upshot of all this is that you can think of &lt;code&gt;drop_while&lt;/code&gt; (and its evil twin &lt;code&gt;take_while&lt;/code&gt;) as &quot;lazy evaluation&quot; versions of &lt;code&gt;reject&lt;/code&gt; and &lt;code&gt;select&lt;/code&gt; where the order of the collection matters.

As for practical uses for &lt;code&gt;drop_while&lt;/code&gt;? I&#039;m sure there are some, but I&#039;m feeling a little lazy today and will use the standard cop-out: &quot;I&#039;ll leave that as an exercise for the reader.&quot;</description>
		<content:encoded><![CDATA[<p><strong>@Gianni Chiappetta:</strong> They&#8217;re similar, but not the same.</p>
<p><code>drop_while</code> and <code>take_while</code> go through the collection&#8217;s items in order and stop as soon as the condition in the block is no longer met, whereas <code>reject</code> and <code>select</code> don&#8217;t operate under that constraint.</p>
<p>Here&#8217;s an example showing the difference:<br />
<code>
<pre>
temperatures = [28, 25, 30, 22, 27]
=> [28, 25, 30, 22, 27]

# "reject" returns an array that is made by using the
# original collection and discarding all of its elements
# that meet the condition in the block
temperatures.reject {|temperature| temperature < 28}
=> [28, 30]

# "drop_while" is like "reject", but when working
# through the given collection in order, it stops discarding
# elements as soon as it encounters an element that
# *doesn't* meet the condition in the block
temperatures.drop_while {|temperature| temperature < 28}
=> [28, 25, 30, 22, 27]
</pre>
<p></code></p>
<p>In the <code>drop_while</code> example above, the first element, <code>28</code>, causes the condition in the block to return <code>false</code>. <code>drop_while</code> stops checking the remaining elements and returns the result array. So in the case of the <code>temperatures</code> array above, <code>drop_while</code>&#8217;s result is equal to the original array.</p>
<p><code>drop_while</code> will return the same results as <code>reject</code> if the array is sorted in such a way that all the elements that meet the condition in the block are moved to the beginning of the collection. In the case of the <code>temperatures</code> array, that&#8217;s easy:</p>
<p><code>
<pre>
temperatures.sort.drop_while {|temperature| temperature < 28}
=> [28, 30]
</pre>
<p></code></p>
<p>The practical upshot of all this is that you can think of <code>drop_while</code> (and its evil twin <code>take_while</code>) as &#8220;lazy evaluation&#8221; versions of <code>reject</code> and <code>select</code> where the order of the collection matters.</p>
<p>As for practical uses for <code>drop_while</code>? I&#8217;m sure there are some, but I&#8217;m feeling a little lazy today and will use the standard cop-out: &#8220;I&#8217;ll leave that as an exercise for the reader.&#8221;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gianni Chiappetta</title>
		<link>http://www.globalnerdy.com/2008/07/25/enumerating-enumerable-enumerabledrop_while/comment-page-1/#comment-1265</link>
		<dc:creator>Gianni Chiappetta</dc:creator>
		<pubDate>Sat, 26 Jul 2008 03:40:28 +0000</pubDate>
		<guid isPermaLink="false">http://globalnerdy.com/?p=1773#comment-1265</guid>
		<description>Aren&#039;t these virtually the same as reject and select?</description>
		<content:encoded><![CDATA[<p>Aren&#8217;t these virtually the same as reject and select?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
