<?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: You&#8217;d naturally write flatMap yourself if asked the question</title>
	<atom:link href="http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/</link>
	<description>The weblog of Tony Morris</description>
	<pubDate>Fri, 05 Dec 2008 09:26:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
		<item>
		<title>By: Jedai</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2357</link>
		<dc:creator>Jedai</dc:creator>
		<pubDate>Sat, 19 Jul 2008 21:31:10 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2357</guid>
		<description>That's because just before this flatten was generalised to take a function of type A =&#38;gt; List[B] instead, so in our case (x =&#38;gt; x) is of type (List[X] =&#38;gt; List[X]) where X is a single type, A is List[X] and B is X.
Then it all works out ! ^^</description>
		<content:encoded><![CDATA[<p>That&#8217;s because just before this flatten was generalised to take a function of type A =&#38;gt; List[B] instead, so in our case (x =&#38;gt; x) is of type (List[X] =&#38;gt; List[X]) where X is a single type, A is List[X] and B is X.<br />
Then it all works out ! ^^</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: DavidLG</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2356</link>
		<dc:creator>DavidLG</dc:creator>
		<pubDate>Fri, 27 Jun 2008 09:09:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2356</guid>
		<description>Hmm, I don't quite understand.  I think part of my confusion is that identity function.  How does (foo:List[A]).flatten(x=&#38;gt;x) make sense if flatten takes an argument of type A=&#38;gt;List[A]?  Unless I'm missing something basic, the types just don't work.</description>
		<content:encoded><![CDATA[<p>Hmm, I don&#8217;t quite understand.  I think part of my confusion is that identity function.  How does (foo:List[A]).flatten(x=&#38;gt;x) make sense if flatten takes an argument of type A=&#38;gt;List[A]?  Unless I&#8217;m missing something basic, the types just don&#8217;t work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2355</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Thu, 26 Jun 2008 13:59:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2355</guid>
		<description>Actually, Scala's syntax for currying doesn't let you drop the parentheses.  In ML, you can do something like:

fun add a b = a + b

...and then invoke:

add 1 2

But unfortunately, Scala's grammar does not allow this.  The real advantage to currying is partially-applied functions.  So you can define your add function as above, and then define a new function value which *specifically* adds a value to 10:

val addTen = add 10

Now you can invoke:

addTen 5

And the result is 15.  This isn't so useful in the trivial example, but once you start using it for larger things, you'll never go back.

As mentioned above though, Scala allows partial application even for non-curried functions:

def add(a: Int, b: Int) = a + b

val addTen = add(10, _: Int)

Not really so clean as the curried alternative, but it still works.  The curried form would be as follows:

def add(a: Int)(b: Int) = a + b

val addTen = add(10)</description>
		<content:encoded><![CDATA[<p>Actually, Scala&#8217;s syntax for currying doesn&#8217;t let you drop the parentheses.  In ML, you can do something like:</p>
<p>fun add a b = a + b</p>
<p>&#8230;and then invoke:</p>
<p>add 1 2</p>
<p>But unfortunately, Scala&#8217;s grammar does not allow this.  The real advantage to currying is partially-applied functions.  So you can define your add function as above, and then define a new function value which *specifically* adds a value to 10:</p>
<p>val addTen = add 10</p>
<p>Now you can invoke:</p>
<p>addTen 5</p>
<p>And the result is 15.  This isn&#8217;t so useful in the trivial example, but once you start using it for larger things, you&#8217;ll never go back.</p>
<p>As mentioned above though, Scala allows partial application even for non-curried functions:</p>
<p>def add(a: Int, b: Int) = a + b</p>
<p>val addTen = add(10, _: Int)</p>
<p>Not really so clean as the curried alternative, but it still works.  The curried form would be as follows:</p>
<p>def add(a: Int)(b: Int) = a + b</p>
<p>val addTen = add(10)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jörn Zaefferer</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2354</link>
		<dc:creator>Jörn Zaefferer</dc:creator>
		<pubDate>Thu, 26 Jun 2008 07:55:01 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2354</guid>
		<description>Thanks guys for the explanation. (A, A) =&#38;gt; Boolean makes more sense to me, that was the missing link. I guess you prefer the curried signature because you can drop the parentheses.</description>
		<content:encoded><![CDATA[<p>Thanks guys for the explanation. (A, A) =&#38;gt; Boolean makes more sense to me, that was the missing link. I guess you prefer the curried signature because you can drop the parentheses.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Morris</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2353</link>
		<dc:creator>Tony Morris</dc:creator>
		<pubDate>Thu, 26 Jun 2008 00:47:04 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2353</guid>
		<description>Gah woops, that error in transcribing the type signature is potentially confusing, so I will repair it. The danger is of course, that this discussion now looks a little absurd.

Nevertheless, I think my mistake must be corrected in the main article.

Thanks for bringing it to my attention Daniel.</description>
		<content:encoded><![CDATA[<p>Gah woops, that error in transcribing the type signature is potentially confusing, so I will repair it. The danger is of course, that this discussion now looks a little absurd.</p>
<p>Nevertheless, I think my mistake must be corrected in the main article.</p>
<p>Thanks for bringing it to my attention Daniel.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2352</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Wed, 25 Jun 2008 23:08:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2352</guid>
		<description>Oh, with regards to the API being broken: yeah, I'll agree with that.  It is a little absurd that with a type system as powerful as Scala's, we're still left working with super-defined methods of non-specific type.  I wonder if flatMap actually casts the return result of the function value so that it can return the specific type of List[A].  Quite nasty.</description>
		<content:encoded><![CDATA[<p>Oh, with regards to the API being broken: yeah, I&#8217;ll agree with that.  It is a little absurd that with a type system as powerful as Scala&#8217;s, we&#8217;re still left working with super-defined methods of non-specific type.  I wonder if flatMap actually casts the return result of the function value so that it can return the specific type of List[A].  Quite nasty.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2351</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Wed, 25 Jun 2008 23:04:27 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2351</guid>
		<description>To further elaborate on the A =&#38;gt; A =&#38;gt; Boolean issue, that's not *actually* the signature of the function value.  The given signature represents the curried form of (A, A) =&#38;gt; Boolean, which is the type that the API actually accepts.

I do prefer the curried form in most languages, but Scala is forced to impose a bit more syntactic overhead on curried functions than pure-functional languages like ML or Haskell.  I guess it depends on your needs, which is better in Scala.  Neither is really more capable than the other since a) you can easily convert between the forms, and b) partially-applied functions are fairly easy in Scala even with uncurried methods.</description>
		<content:encoded><![CDATA[<p>To further elaborate on the A =&#38;gt; A =&#38;gt; Boolean issue, that&#8217;s not *actually* the signature of the function value.  The given signature represents the curried form of (A, A) =&#38;gt; Boolean, which is the type that the API actually accepts.</p>
<p>I do prefer the curried form in most languages, but Scala is forced to impose a bit more syntactic overhead on curried functions than pure-functional languages like ML or Haskell.  I guess it depends on your needs, which is better in Scala.  Neither is really more capable than the other since a) you can easily convert between the forms, and b) partially-applied functions are fairly easy in Scala even with uncurried methods.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Tony Morris</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2350</link>
		<dc:creator>Tony Morris</dc:creator>
		<pubDate>Wed, 25 Jun 2008 19:49:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2350</guid>
		<description>Daniel, I agree. Change "completely" to "almost completely" or "mostly". The fact that List[A].flatMap takes a forall B. (B =&gt; Iterable[B]) as an argument instead of a (A =&gt; List[B]) is completely absurd. Granted, it may have been written at a time when higher kinds did not exist in the language, but it is so fundamentally broken, it should be repaired. The fact that these methods are not final offers no resolve. To observe a correction, take a look at the Monad trait in Scalaz.

Jörn, sorry if that caused confusion. The method that takes that argument is called a Higher-Order Function (HOF) because that argument is itself, a function. It is a function takes two arguments of type A and returns a Boolean.

To represent similar in Java, you'd have to use an interface:

&lt;pre&gt;
interface Whatever {
  &#38;lt;A&#38;gt; boolean whatever(A a1, A a2);
}
&lt;/pre&gt;

Then you would substitute A =&gt; A =&gt; Boolean with Whatever&#38;lt;A&#38;gt; in the method signature. It is simply the strategy for resolving the order of the element types of the List or specifically, whether the first argument is less than the second.

I hope this helps.</description>
		<content:encoded><![CDATA[<p>Daniel, I agree. Change &#8220;completely&#8221; to &#8220;almost completely&#8221; or &#8220;mostly&#8221;. The fact that List[A].flatMap takes a forall B. (B => Iterable[B]) as an argument instead of a (A => List[B]) is completely absurd. Granted, it may have been written at a time when higher kinds did not exist in the language, but it is so fundamentally broken, it should be repaired. The fact that these methods are not final offers no resolve. To observe a correction, take a look at the Monad trait in Scalaz.</p>
<p>Jörn, sorry if that caused confusion. The method that takes that argument is called a Higher-Order Function (HOF) because that argument is itself, a function. It is a function takes two arguments of type A and returns a Boolean.</p>
<p>To represent similar in Java, you&#8217;d have to use an interface:</p>
<pre>
interface Whatever {
  &#38;lt;A&#38;gt; boolean whatever(A a1, A a2);
}
</pre>
<p>Then you would substitute A => A => Boolean with Whatever&#38;lt;A&#38;gt; in the method signature. It is simply the strategy for resolving the order of the element types of the List or specifically, whether the first argument is less than the second.</p>
<p>I hope this helps.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jörn Zaefferer</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2349</link>
		<dc:creator>Jörn Zaefferer</dc:creator>
		<pubDate>Wed, 25 Jun 2008 19:42:05 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2349</guid>
		<description>You lost me at "A =&#38;gt; A =&#38;gt; Boolean", I don't get what that means. Could you explain that, assuming a reader with no actual experience in functional programming?</description>
		<content:encoded><![CDATA[<p>You lost me at &#8220;A =&#38;gt; A =&#38;gt; Boolean&#8221;, I don&#8217;t get what that means. Could you explain that, assuming a reader with no actual experience in functional programming?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Spiewak</title>
		<link>http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2348</link>
		<dc:creator>Daniel Spiewak</dc:creator>
		<pubDate>Wed, 25 Jun 2008 16:27:29 +0000</pubDate>
		<guid isPermaLink="false">http://blog.tmorris.net/youd-naturally-write-flatmap-yourself-if-asked-the-question/#comment-2348</guid>
		<description>I'm not sure I agree that Scala's collections API is *completely* broken.  :-)  Granted, methods like flatMap and similar are a little inefficient when applied to general Iterable containers, but the methods are not final.  For example, List overrides map, fold*, flatMap, etc covariantly to return of type List.  I haven't looked at the actual implementation, but I assume that it's a fairly efficient design.

To what specific breakage were you referring?</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure I agree that Scala&#8217;s collections API is *completely* broken.  <img src='http://blog.tmorris.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  Granted, methods like flatMap and similar are a little inefficient when applied to general Iterable containers, but the methods are not final.  For example, List overrides map, fold*, flatMap, etc covariantly to return of type List.  I haven&#8217;t looked at the actual implementation, but I assume that it&#8217;s a fairly efficient design.</p>
<p>To what specific breakage were you referring?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
