<?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"
	>
<channel>
	<title>Comments on: scala.Option Cheat Sheet</title>
	<atom:link href="http://blog.tmorris.net/scalaoption-cheat-sheet/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tmorris.net/scalaoption-cheat-sheet/</link>
	<description>The weblog of Tony Morris</description>
	<pubDate>Tue, 06 Jan 2009 21:51:20 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Tony Morris</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-2003</link>
		<dc:creator>Tony Morris</dc:creator>
		<pubDate>Fri, 20 Jun 2008 20:26:17 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-2003</guid>
		<description>Hi John,
Thanks for the feedback.

1) The &lt;code&gt;option&lt;/code&gt; method is intended to encapsulate the deconstruction of the &lt;code&gt;Option&lt;/code&gt; type. Consider that &lt;code&gt;Option&lt;/code&gt; has two case class constructors, &lt;code&gt;None&lt;/code&gt; and &lt;/code&gt;Some&lt;/code&gt;, therefore, the method has two arguments. Furthermore, one of the constructors (&lt;code&gt;Some&lt;/code&gt;) takes a single argument, therefore, one of the method arguments is a function that takes 1 argument.

It might be a good exercise to attempt to write the &lt;code&gt;option&lt;/code&gt; method while maintaining the given type signature. You will use pattern matching to do so. The point is, however, that you have encapsulated the most general form of &lt;code&gt;Option&lt;/code&gt; deconstruction and so you needn't (theoretically) ever manually deconstruct &lt;code&gt;Option&lt;/code&gt; again with pattern matching.

2) There is an implicit from &lt;code&gt;Option&lt;/code&gt; to &lt;code&gt;Iterable&lt;/code&gt; in the &lt;code&gt;Option&lt;/code&gt; object. http://www.scala-lang.org/docu/files/api/scala/Option$object.html#option2Iterable%28Option%5BA%5D%29

3) It may be the case that you have a general deconstruction form where you use the hypothetical &lt;code&gt;option&lt;/code&gt; method. Then, are there any recurring patterns? If so, write a more specific function.

There is no problem with your call to map then getOrElse. Efficiency should not be considered here in favour of the compiler performing what is called 'fusing'. In practice though, the compiler will not fuse this expression, nor will the efficiency cost be negligible to the gain in expressivity.

Finally, you may want to 'compose' these functions yourself and I recommend you do so even if just for your own satisfaction. You can implement it by calling &lt;code&gt;map&lt;/code&gt; then &lt;code&gt;getOrElse&lt;/code&gt; with the appropriate arguments. Then, if one day performance becomes a huge issue, change the implementation to use pattern matching. Doing this is essentially manual fusing.

I hope this helps; ask more questions if you need.</description>
		<content:encoded><![CDATA[<p>Hi John,<br />
Thanks for the feedback.</p>
<p>1) The <code>option</code> method is intended to encapsulate the deconstruction of the <code>Option</code> type. Consider that <code>Option</code> has two case class constructors, <code>None</code> and Some, therefore, the method has two arguments. Furthermore, one of the constructors (<code>Some</code>) takes a single argument, therefore, one of the method arguments is a function that takes 1 argument.</p>
<p>It might be a good exercise to attempt to write the <code>option</code> method while maintaining the given type signature. You will use pattern matching to do so. The point is, however, that you have encapsulated the most general form of <code>Option</code> deconstruction and so you needn&#8217;t (theoretically) ever manually deconstruct <code>Option</code> again with pattern matching.</p>
<p>2) There is an implicit from <code>Option</code> to <code>Iterable</code> in the <code>Option</code> object. <a href="http://www.scala-lang.org/docu/files/api/scala/Optionobject.html#option2Iterable%28Option%5BA%5D%29" onclick="javascript:pageTracker._trackPageview('/outbound/comment/www.scala-lang.org');" rel="nofollow">http://www.scala-lang.org/docu/files/api/scala/Optionobject.html#option2Iterable%28Option%5BA%5D%29</a></p>
<p>3) It may be the case that you have a general deconstruction form where you use the hypothetical <code>option</code> method. Then, are there any recurring patterns? If so, write a more specific function.</p>
<p>There is no problem with your call to map then getOrElse. Efficiency should not be considered here in favour of the compiler performing what is called &#8216;fusing&#8217;. In practice though, the compiler will not fuse this expression, nor will the efficiency cost be negligible to the gain in expressivity.</p>
<p>Finally, you may want to &#8216;compose&#8217; these functions yourself and I recommend you do so even if just for your own satisfaction. You can implement it by calling <code>map</code> then <code>getOrElse</code> with the appropriate arguments. Then, if one day performance becomes a huge issue, change the implementation to use pattern matching. Doing this is essentially manual fusing.</p>
<p>I hope this helps; ask more questions if you need.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Simmons</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-2002</link>
		<dc:creator>John Simmons</dc:creator>
		<pubDate>Fri, 20 Jun 2008 19:04:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-2002</guid>
		<description>Nice examples.  Several questions:

1. I don't understand the def option[A,X] method, since no body is given.  Is this something generic that should be so obvious I can write it myself, or is it something specific to a given situation?

2. Methods forall and exists are not listed in the scaladoc for Option, but I tried them and they do work.  Where do they come from?  Is there an implicit conversion happening here?

3.  Most seriously, I have a pattern that does not match any of the patterns given:

def process(o: Option[A], cc: C): B =
  o match {
    case Some(a) =&#38;gt; f (a, cc)  // value of type B that depends on both a and cc
    case None =&#38;gt; g(cc)  // value of type B that depends only on cc
  }

The only way that I see to replace this match is with

o.map(f(_,cc)).getOrElse(g(cc))

This seems less efficient since it uses two calls, and the first call constructs an extra Option[B] to which to apply the second call.</description>
		<content:encoded><![CDATA[<p>Nice examples.  Several questions:</p>
<p>1. I don&#8217;t understand the def option[A,X] method, since no body is given.  Is this something generic that should be so obvious I can write it myself, or is it something specific to a given situation?</p>
<p>2. Methods forall and exists are not listed in the scaladoc for Option, but I tried them and they do work.  Where do they come from?  Is there an implicit conversion happening here?</p>
<p>3.  Most seriously, I have a pattern that does not match any of the patterns given:</p>
<p>def process(o: Option[A], cc: C): B =<br />
  o match {<br />
    case Some(a) =&#38;gt; f (a, cc)  // value of type B that depends on both a and cc<br />
    case None =&#38;gt; g(cc)  // value of type B that depends only on cc<br />
  }</p>
<p>The only way that I see to replace this match is with</p>
<p>o.map(f(_,cc)).getOrElse(g(cc))</p>
<p>This seems less efficient since it uses two calls, and the first call constructs an extra Option[B] to which to apply the second call.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Morris</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-2001</link>
		<dc:creator>Tony Morris</dc:creator>
		<pubDate>Thu, 24 Apr 2008 23:22:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-2001</guid>
		<description>Hello Seth,
Yes that is correct. All examples are assumed to type-check.</description>
		<content:encoded><![CDATA[<p>Hello Seth,<br />
Yes that is correct. All examples are assumed to type-check.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Seth Tisue</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-2000</link>
		<dc:creator>Seth Tisue</dc:creator>
		<pubDate>Thu, 24 Apr 2008 20:17:53 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-2000</guid>
		<description>The equivalence you give for flatMap only holds if the return type of foo(x) is compatible with Option. Right?</description>
		<content:encoded><![CDATA[<p>The equivalence you give for flatMap only holds if the return type of foo(x) is compatible with Option. Right?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Seth Tisue</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1999</link>
		<dc:creator>Seth Tisue</dc:creator>
		<pubDate>Wed, 09 Apr 2008 18:03:18 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1999</guid>
		<description>Thank you. This is helpful.</description>
		<content:encoded><![CDATA[<p>Thank you. This is helpful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Morris</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1998</link>
		<dc:creator>Tony Morris</dc:creator>
		<pubDate>Mon, 18 Feb 2008 22:01:11 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1998</guid>
		<description>Hi Jörn, there is a way of removing that but it's very specific. This is because of the guard in your first match. You could transform that code to put the guard on the right side of the case match, so then you'd have:

&lt;pre&gt;
case Full(dbuser) =&gt; if (dbuser.password.match_?(password)) User.logUserIn(dbuser) else error(”wrong password”)
case Empty =&gt; notice(”sending verification mail”)
&lt;/pre&gt;

Then of course, you could just call the &lt;code&gt;option&lt;/code&gt; method or equivalent for Can. Then you might want to get even more specific by decomposing your if. Since you can write if yourself in Scala, you can then apply certain specialisations to it. I'll let you write your Scala if, then various specialisations without giving the answer away :)</description>
		<content:encoded><![CDATA[<p>Hi Jörn, there is a way of removing that but it&#8217;s very specific. This is because of the guard in your first match. You could transform that code to put the guard on the right side of the case match, so then you&#8217;d have:</p>
<pre>
case Full(dbuser) => if (dbuser.password.match_?(password)) User.logUserIn(dbuser) else error(”wrong password”)
case Empty => notice(”sending verification mail”)
</pre>
<p>Then of course, you could just call the <code>option</code> method or equivalent for Can. Then you might want to get even more specific by decomposing your if. Since you can write if yourself in Scala, you can then apply certain specialisations to it. I&#8217;ll let you write your Scala if, then various specialisations without giving the answer away <img src='http://blog.tmorris.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jörn Zaefferer</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1997</link>
		<dc:creator>Jörn Zaefferer</dc:creator>
		<pubDate>Mon, 18 Feb 2008 17:46:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1997</guid>
		<description>Is there any way to replace this?

User.find(By(User.email, email)) match {
  case Full(dbuser) if (dbuser.password.match_?(password)) =&#38;gt; User.logUserIn(dbuser)
  case Full(dbuser) =&#38;gt; error("wrong password")
  case Empty =&#38;gt; notice("sending verification mail")
}

Here its a Can with Full instead of Some, but that shouldn't be important.</description>
		<content:encoded><![CDATA[<p>Is there any way to replace this?</p>
<p>User.find(By(User.email, email)) match {<br />
  case Full(dbuser) if (dbuser.password.match_?(password)) =&#38;gt; User.logUserIn(dbuser)<br />
  case Full(dbuser) =&#38;gt; error(&#8221;wrong password&#8221;)<br />
  case Empty =&#38;gt; notice(&#8221;sending verification mail&#8221;)<br />
}</p>
<p>Here its a Can with Full instead of Some, but that shouldn&#8217;t be important.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Morris</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1996</link>
		<dc:creator>Tony Morris</dc:creator>
		<pubDate>Wed, 13 Feb 2008 06:12:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1996</guid>
		<description>Just a note for anyone that cares; I have updated this post with a reference to two additional entries that use the &lt;code&gt;forall&lt;/code&gt; and &lt;code&gt;exists&lt;/code&gt; methods on &lt;code&gt;Option&lt;/code&gt;.</description>
		<content:encoded><![CDATA[<p>Just a note for anyone that cares; I have updated this post with a reference to two additional entries that use the <code>forall</code> and <code>exists</code> methods on <code>Option</code>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Jimenez</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1995</link>
		<dc:creator>Daniel Jimenez</dc:creator>
		<pubDate>Fri, 18 Jan 2008 15:56:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1995</guid>
		<description>Great cheatsheet.

I'm wondering, how would one use Option in the case described in the first comment here (http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-2#numcomments)?

(Copied from there)
&lt;pre class="Scala"&gt;
abstract class Shape {
..var fillColor:Option[Color] = None
..// etc
}

class Circle(var radius:Int) extends Shape {
..def draw(g:Graphics):Unit = fillColor match {
....case None =&#38;gt;
......g.drawCircle(0, 0, radius / 2, radius / 2)
....case Some(color) =&#38;gt; {
......g.setColor(color);
......g.fillOval(0, 0, radius / 2, radius / 2)
....}
..}
}
&lt;/pre&gt;

I suspect the problem is the inherent imperative-ness of requiring setColor to be called before fillOval. Can I get around this in Scala using the examples above, or is there more to the story?</description>
		<content:encoded><![CDATA[<p>Great cheatsheet.</p>
<p>I&#8217;m wondering, how would one use Option in the case described in the first comment here (http://www.codecommit.com/blog/scala/scala-for-java-refugees-part-2#numcomments)?</p>
<p>(Copied from there)</p>
<pre class="Scala">
abstract class Shape {
..var fillColor:Option[Color] = None
..// etc
}

class Circle(var radius:Int) extends Shape {
..def draw(g:Graphics):Unit = fillColor match {
....case None =&#38;gt;
......g.drawCircle(0, 0, radius / 2, radius / 2)
....case Some(color) =&#38;gt; {
......g.setColor(color);
......g.fillOval(0, 0, radius / 2, radius / 2)
....}
..}
}
</pre>
<p>I suspect the problem is the inherent imperative-ness of requiring setColor to be called before fillOval. Can I get around this in Scala using the examples above, or is there more to the story?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nosewheelie &#38;#187; Blog Archive &#38;#187; scala.Option Cheat Sheet</title>
		<link>http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1994</link>
		<dc:creator>nosewheelie &#38;#187; Blog Archive &#38;#187; scala.Option Cheat Sheet</dc:creator>
		<pubDate>Thu, 17 Jan 2008 00:21:38 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/scalaoption-cheat-sheet/#comment-1994</guid>
		<description>[...] Source: scala.Option Cheat Sheet. [...]</description>
		<content:encoded><![CDATA[<p>[...] Source: scala.Option Cheat Sheet. [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>
