<?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: Response to the critique for my last post and OneElementIterator</title>
	<atom:link href="http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/feed/" rel="self" type="application/rss+xml" />
	<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/</link>
	<description></description>
	<lastBuildDate>Mon, 15 Mar 2010 13:03:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: James Iry</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146252</link>
		<dc:creator>James Iry</dc:creator>
		<pubDate>Sat, 09 Aug 2008 02:16:50 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146252</guid>
		<description>If you don&#039;t care about performance, Collections.singleton(value).iterator.  :-)

If you want performance and null handling, add a mutable &quot;hasNext&quot; boolean flag to your implementation.

It&#039;s important to mention that, unlike Java style external iterators, the Scala solution based on map and flatMap or the Haskell solution based on bind and &quot;return&quot; (not the same thing as Java style return) require no mutation.  E.g., here&#039;s a complete Option monad in Scala - it&#039;s even covariant, which is a pain in the arse in Java

sealed abstract class MyOption[ A] {
   def map[B](f:A=&gt;B):MyOption[B] = flatMap(x =&gt; MySome(f(x)))
   def flatMap[B](f:A=&gt;MyOption[B]):MyOption[B] = this match {
      case MySome(x) =&gt; f(x)
      case _ =&gt; MyNone
   }
}
case class MySome[ A](value:A) extends MyOption[A]
case object MyNone extends MyOption[Nothing]

println(for {x &lt;- MySome(3);y &lt;- MySome(4);} yield x   y)

The Haskell option monad is even tighter with less syntactic noise (although, I know this will get totally borked by word press, I hope something makes it through)

data MyOption a = MySome a &#124; MyNone deriving Show

instance Monad MyOption where
  (MySome x) &gt;&gt;= k   =  k x
  MyNone  &gt;&gt;= k   =  MyNone
  return = MySome
  fail s = MyNone

main = print $ do x &lt;- MySome 3; y &lt;- MySome 4; return (x   y)</description>
		<content:encoded><![CDATA[<p>If you don&#8217;t care about performance, Collections.singleton(value).iterator.  :-)</p>
<p>If you want performance and null handling, add a mutable &#8220;hasNext&#8221; boolean flag to your implementation.</p>
<p>It&#8217;s important to mention that, unlike Java style external iterators, the Scala solution based on map and flatMap or the Haskell solution based on bind and &#8220;return&#8221; (not the same thing as Java style return) require no mutation.  E.g., here&#8217;s a complete Option monad in Scala &#8211; it&#8217;s even covariant, which is a pain in the arse in Java</p>
<p>sealed abstract class MyOption[ A] {<br />
   def map[B](f:A=&gt;B):MyOption[B] = flatMap(x =&gt; MySome(f(x)))<br />
   def flatMap[B](f:A=&gt;MyOption[B]):MyOption[B] = this match {<br />
      case MySome(x) =&gt; f(x)<br />
      case _ =&gt; MyNone<br />
   }<br />
}<br />
case class MySome[ A](value:A) extends MyOption[A]<br />
case object MyNone extends MyOption[Nothing]</p>
<p>println(for {x &lt;- MySome(3);y &lt;- MySome(4);} yield x   y)</p>
<p>The Haskell option monad is even tighter with less syntactic noise (although, I know this will get totally borked by word press, I hope something makes it through)</p>
<p>data MyOption a = MySome a | MyNone deriving Show</p>
<p>instance Monad MyOption where<br />
  (MySome x) &gt;&gt;= k   =  k x<br />
  MyNone  &gt;&gt;= k   =  MyNone<br />
  return = MySome<br />
  fail s = MyNone</p>
<p>main = print $ do x &lt;- MySome 3; y &lt;- MySome 4; return (x   y)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146214</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 08 Aug 2008 22:52:51 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146214</guid>
		<description>@Eugene: I&#039;m not sure what&#039;s more important, less bugs or performance. You decide. Otherwise I fear there is only Scala left for me.

@lqd: Hmm :-)</description>
		<content:encoded><![CDATA[<p>@Eugene: I&#8217;m not sure what&#8217;s more important, less bugs or performance. You decide. Otherwise I fear there is only Scala left for me.</p>
<p>@lqd: Hmm :-)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lqd</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146180</link>
		<dc:creator>lqd</dc:creator>
		<pubDate>Fri, 08 Aug 2008 19:40:30 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146180</guid>
		<description>Nice :)

I guess we could rename OneElementIterator to Some, extend Option[T] and return this in iterator() to realign with the last post concepts.</description>
		<content:encoded><![CDATA[<p>Nice :)</p>
<p>I guess we could rename OneElementIterator to Some, extend Option[T] and return this in iterator() to realign with the last post concepts.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eugene Kuleshov</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146119</link>
		<dc:creator>Eugene Kuleshov</dc:creator>
		<pubDate>Fri, 08 Aug 2008 16:32:00 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146119</guid>
		<description>Stephan, it seem to me that you created a problem that does not actually exist. Basically, if you are always using iterator, just for the sake of syntactic sugar with the foreach loop, according to my microbenchmark you got yourself about 60% overhead (even with your optimized OneItemIterator class) comparing to simply returning a Null object. It is actually unlikely something like that ever going to be a bottle neck in the real application, but number of iterations and tiny mistakes you made while implementing such simple class is really scare. So, it would have been safer to simply use existing JRE classes.</description>
		<content:encoded><![CDATA[<p>Stephan, it seem to me that you created a problem that does not actually exist. Basically, if you are always using iterator, just for the sake of syntactic sugar with the foreach loop, according to my microbenchmark you got yourself about 60% overhead (even with your optimized OneItemIterator class) comparing to simply returning a Null object. It is actually unlikely something like that ever going to be a bottle neck in the real application, but number of iterations and tiny mistakes you made while implementing such simple class is really scare. So, it would have been safer to simply use existing JRE classes.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146108</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 08 Aug 2008 14:25:49 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146108</guid>
		<description>@Erik: The whole point was not to return null ;-)

&quot;@Eugene: You’re right of course.&quot;

So I added to my version

&lt;pre&gt;
  public T next() {
    if (!hasNext()) {
      throw new NoSuchElementException(&quot;No more elements in OneElementIterator&quot;);
    }
    try {
      return element;
    } finally {
      element = null;
    }
  }
&lt;/pre&gt;

&lt;i&gt;&quot;I’d go with Collections.singletonList(whatever).iterator(). The less code you write, the fewer bugs you write.&quot;&lt;/i&gt;

Normally I would agree. There are some posts on this topic on this blog, for example the String.reverse issue (JDK versus write-your-own).

In this case, because Option[T] is potentially used a lot, I&#039;d go for an optimization.</description>
		<content:encoded><![CDATA[<p>@Erik: The whole point was not to return null ;-)</p>
<p>&#8220;@Eugene: You’re right of course.&#8221;</p>
<p>So I added to my version</p>
<pre>
  public T next() {
    if (!hasNext()) {
      throw new NoSuchElementException("No more elements in OneElementIterator");
    }
    try {
      return element;
    } finally {
      element = null;
    }
  }
</pre>
<p><i>&#8220;I’d go with Collections.singletonList(whatever).iterator(). The less code you write, the fewer bugs you write.&#8221;</i></p>
<p>Normally I would agree. There are some posts on this topic on this blog, for example the String.reverse issue (JDK versus write-your-own).</p>
<p>In this case, because Option[T] is potentially used a lot, I&#8217;d go for an optimization.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Erik Mogensen</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146102</link>
		<dc:creator>Erik Mogensen</dc:creator>
		<pubDate>Fri, 08 Aug 2008 13:36:44 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146102</guid>
		<description>What if you want to return an iterator that contains a _null_ :-)

And of course you have the while(true) System.out.println(e.next());  which relies on the exception to be thrown.  It&#039;s not good code, but it is valid.  Violating the javadoc requirement of throwing the exception would put some unlucky user into a tight loop...

I&#039;d go with Collections.singletonList(whatever).iterator().  The less code you write, the fewer bugs you write.</description>
		<content:encoded><![CDATA[<p>What if you want to return an iterator that contains a _null_ :-)</p>
<p>And of course you have the while(true) System.out.println(e.next());  which relies on the exception to be thrown.  It&#8217;s not good code, but it is valid.  Violating the javadoc requirement of throwing the exception would put some unlucky user into a tight loop&#8230;</p>
<p>I&#8217;d go with Collections.singletonList(whatever).iterator().  The less code you write, the fewer bugs you write.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146060</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 08 Aug 2008 11:42:33 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146060</guid>
		<description>@Eugene: You&#039;re right of course.

The remove got lost with cut&amp;paste (see missing }  for class :-)

I&#039;ll add the method.</description>
		<content:encoded><![CDATA[<p>@Eugene: You&#8217;re right of course.</p>
<p>The remove got lost with cut&#038;paste (see missing }  for class :-)</p>
<p>I&#8217;ll add the method.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eugene Kulesov</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146055</link>
		<dc:creator>Eugene Kulesov</dc:creator>
		<pubDate>Fri, 08 Aug 2008 11:37:25 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146055</guid>
		<description>According to the Iterator class javadoc, next() method is supposed to throw NoSuchElementException when iteration has no more elements. Also, there is a remove() method, which should throw an UnsupportedOperationException if the remove  operation is not supported by Iterator.</description>
		<content:encoded><![CDATA[<p>According to the Iterator class javadoc, next() method is supposed to throw NoSuchElementException when iteration has no more elements. Also, there is a remove() method, which should throw an UnsupportedOperationException if the remove  operation is not supported by Iterator.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: stephan</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146022</link>
		<dc:creator>stephan</dc:creator>
		<pubDate>Fri, 08 Aug 2008 08:45:42 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146022</guid>
		<description>Yes, thought about that, but was intrigued by the beauty of the finally solution. In production I would introduce the e reference though.

But I&#039;m not sure about performance, because there is no Exception being thrown or catched.</description>
		<content:encoded><![CDATA[<p>Yes, thought about that, but was intrigued by the beauty of the finally solution. In production I would introduce the e reference though.</p>
<p>But I&#8217;m not sure about performance, because there is no Exception being thrown or catched.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://codemonkeyism.com/response-to-the-critique-for-my-last-post-and-oneelementiterator/comment-page-1/#comment-146014</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Fri, 08 Aug 2008 08:39:25 +0000</pubDate>
		<guid isPermaLink="false">http://stephan.reposita.org/archives/2008/08/08/response-to-the-critique-for-my-last-post-and-oneelementiterator/#comment-146014</guid>
		<description>Much faster to avoid the try (which as you implied, can be quite slow) and instead use a local temporary:

&lt;pre name=&quot;code&quot; class=&quot;java:nocontrols:nogutter&quot;&gt;
public T next() {
  T e = element;
  element = null;
  return e;
}
&lt;/pre&gt;

It&#039;s not concurrency safe, but then, neither is yours.  :-)</description>
		<content:encoded><![CDATA[<p>Much faster to avoid the try (which as you implied, can be quite slow) and instead use a local temporary:</p>
<pre name="code" class="java:nocontrols:nogutter">
public T next() {
  T e = element;
  element = null;
  return e;
}
</pre>
<p>It&#8217;s not concurrency safe, but then, neither is yours.  :-)</p>
]]></content:encoded>
	</item>
</channel>
</rss>
