<?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: Functional programming in C++ with function objects</title>
	<atom:link href="http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/</link>
	<description>The blog of John D. Cook</description>
	<lastBuildDate>Sat, 11 Feb 2012 22:42:11 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Sohail</title>
		<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/comment-page-1/#comment-11293</link>
		<dc:creator>Sohail</dc:creator>
		<pubDate>Mon, 22 Dec 2008 20:06:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.johndcook.com/blog/?p=1122#comment-11293</guid>
		<description>John, I think if you are willing to take the overhead of a virtual function, then it is a no-brainer to have an abstract function object which has a child convenience class taking in a boost function. I can bet you 99% of the code will use the child convenience class though ;-)

Something like:

struct FunctObjBase
{
  virtual ~FunctObjBase(){}
  virtual double execute(double d) const = 0;
};

struct Helper : FunctObjBase
{
  template
  Helper(F const &amp; f):
   m_f(f){}

  virtual double execute(double x){return m_f(0);}

 private:
  boost::function m_f;
};

Atleast thats what I&#039;d do without evidence/requirements/proof to tell me otherwise.</description>
		<content:encoded><![CDATA[<p>John, I think if you are willing to take the overhead of a virtual function, then it is a no-brainer to have an abstract function object which has a child convenience class taking in a boost function. I can bet you 99% of the code will use the child convenience class though <img src='http://www.johndcook.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Something like:</p>
<p>struct FunctObjBase<br />
{<br />
  virtual ~FunctObjBase(){}<br />
  virtual double execute(double d) const = 0;<br />
};</p>
<p>struct Helper : FunctObjBase<br />
{<br />
  template<br />
  Helper(F const &amp; f):<br />
   m_f(f){}</p>
<p>  virtual double execute(double x){return m_f(0);}</p>
<p> private:<br />
  boost::function m_f;<br />
};</p>
<p>Atleast thats what I&#8217;d do without evidence/requirements/proof to tell me otherwise.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/comment-page-1/#comment-11287</link>
		<dc:creator>John</dc:creator>
		<pubDate>Mon, 22 Dec 2008 17:12:36 +0000</pubDate>
		<guid isPermaLink="false">http://www.johndcook.com/blog/?p=1122#comment-11287</guid>
		<description>Gene, 

When we wrote our numerical library, we had a debate between using abstract base classes and templates. My first thought was to use the former, but we ended up going with the latter. I don&#039;t know whether we made the right choice. 

Templates provide a form of &quot;duck typing&quot; in that you don&#039;t have to derive your function objects from a particular base class.  But using templates has lead to some ugly declarations when there are multiple function objects or nested function objects involved. It has also led to putting more code in .h files and less in .cpp files, which has its own set of pros and cons. I don&#039;t know what I&#039;d do if we started over.</description>
		<content:encoded><![CDATA[<p>Gene, </p>
<p>When we wrote our numerical library, we had a debate between using abstract base classes and templates. My first thought was to use the former, but we ended up going with the latter. I don&#8217;t know whether we made the right choice. </p>
<p>Templates provide a form of &#8220;duck typing&#8221; in that you don&#8217;t have to derive your function objects from a particular base class.  But using templates has lead to some ugly declarations when there are multiple function objects or nested function objects involved. It has also led to putting more code in .h files and less in .cpp files, which has its own set of pros and cons. I don&#8217;t know what I&#8217;d do if we started over.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sohail</title>
		<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/comment-page-1/#comment-11286</link>
		<dc:creator>Sohail</dc:creator>
		<pubDate>Mon, 22 Dec 2008 17:08:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.johndcook.com/blog/?p=1122#comment-11286</guid>
		<description>Sorry for the double post but I&#039;d like to expand.

Specifically, the original solution is not entirely unlike:

double
compute(double * a, double * b, double * c, double x)
{
  return 1000* (*a) + 100 * (*b) + 10* (*c) + x;
}

Whereas the bind solution works out to approximately:

double
compute(double a, double b, double c, double d)
{
  return 1000 * a + 100 * b + 10*c + d;
}

If we want to talk premature optimization, the first example can cause cache misses and all sorts of headache.

I&#039;m interested in that real program of yours!</description>
		<content:encoded><![CDATA[<p>Sorry for the double post but I&#8217;d like to expand.</p>
<p>Specifically, the original solution is not entirely unlike:</p>
<p>double<br />
compute(double * a, double * b, double * c, double x)<br />
{<br />
  return 1000* (*a) + 100 * (*b) + 10* (*c) + x;<br />
}</p>
<p>Whereas the bind solution works out to approximately:</p>
<p>double<br />
compute(double a, double b, double c, double d)<br />
{<br />
  return 1000 * a + 100 * b + 10*c + d;<br />
}</p>
<p>If we want to talk premature optimization, the first example can cause cache misses and all sorts of headache.</p>
<p>I&#8217;m interested in that real program of yours!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sohail</title>
		<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/comment-page-1/#comment-11285</link>
		<dc:creator>Sohail</dc:creator>
		<pubDate>Mon, 22 Dec 2008 17:04:53 +0000</pubDate>
		<guid isPermaLink="false">http://www.johndcook.com/blog/?p=1122#comment-11285</guid>
		<description>Gene,

That smells like premature optimization to me. I would be very careful to measure in a /real/ program first. If you have such a program, I&#039;d be interested.

Also, boost bind is not just for the nutters anymore: TR1 includes boost bind and boost function (of course, in the std namespace...)

Good post John.</description>
		<content:encoded><![CDATA[<p>Gene,</p>
<p>That smells like premature optimization to me. I would be very careful to measure in a /real/ program first. If you have such a program, I&#8217;d be interested.</p>
<p>Also, boost bind is not just for the nutters anymore: TR1 includes boost bind and boost function (of course, in the std namespace&#8230;)</p>
<p>Good post John.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gene</title>
		<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/comment-page-1/#comment-11284</link>
		<dc:creator>Gene</dc:creator>
		<pubDate>Mon, 22 Dec 2008 16:44:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.johndcook.com/blog/?p=1122#comment-11284</guid>
		<description>There&#039;s no doubt that bind is a lot easier to use than bind1st and bind2nd. I just haven&#039;t found it to be all that useful, compared to the technique you described in your paper. The implementation of bind used in boost is incredibly smart, I&#039;m envious of the brainiac that came up with  it.

To be honest, I really prefer to have the the function class implement an interface or abstract class rather than either of these solutions, but that&#039;s another subject entirely.</description>
		<content:encoded><![CDATA[<p>There&#8217;s no doubt that bind is a lot easier to use than bind1st and bind2nd. I just haven&#8217;t found it to be all that useful, compared to the technique you described in your paper. The implementation of bind used in boost is incredibly smart, I&#8217;m envious of the brainiac that came up with  it.</p>
<p>To be honest, I really prefer to have the the function class implement an interface or abstract class rather than either of these solutions, but that&#8217;s another subject entirely.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John</title>
		<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/comment-page-1/#comment-11280</link>
		<dc:creator>John</dc:creator>
		<pubDate>Mon, 22 Dec 2008 14:17:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.johndcook.com/blog/?p=1122#comment-11280</guid>
		<description>Sohail&#039;s solution is more elegant, analogous to the Python solution in the next post. But as Gene pointed out, the more direct approach may be more efficient; it is for me, especially since I often do a lot of pre-computing in the constructor.  I could see using the &lt;code&gt;bind&lt;/code&gt; approach for some things. I&#039;ve intended for some time to look into Boost, and this gives me one more reason.

I thought maybe Boost was unnecessary, but the standard C++ library only has &lt;code&gt;bind1st&lt;/code&gt; and &lt;code&gt;bind2nd&lt;/code&gt;, so I guess you do need Boost for a more general &lt;code&gt;bind&lt;/code&gt; function.</description>
		<content:encoded><![CDATA[<p>Sohail&#8217;s solution is more elegant, analogous to the Python solution in the next post. But as Gene pointed out, the more direct approach may be more efficient; it is for me, especially since I often do a lot of pre-computing in the constructor.  I could see using the <code>bind</code> approach for some things. I&#8217;ve intended for some time to look into Boost, and this gives me one more reason.</p>
<p>I thought maybe Boost was unnecessary, but the standard C++ library only has <code>bind1st</code> and <code>bind2nd</code>, so I guess you do need Boost for a more general <code>bind</code> function.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gene</title>
		<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/comment-page-1/#comment-11279</link>
		<dc:creator>Gene</dc:creator>
		<pubDate>Mon, 22 Dec 2008 14:04:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.johndcook.com/blog/?p=1122#comment-11279</guid>
		<description>Sohail,

The problem with your approach is that the constants must be added to the stack every time f(x) is called (bind isn&#039;t cheap to use.)  If one is using a zero finder that uses reverse communication, using bind will add significant overhead to the call. By storing the constants internally in the structure, as in the original example, you trade more or less constant heap storage for very dynamic stack storage of the constants. I also like John&#039;s approach because you don&#039;t have to drag boost into the problem.</description>
		<content:encoded><![CDATA[<p>Sohail,</p>
<p>The problem with your approach is that the constants must be added to the stack every time f(x) is called (bind isn&#8217;t cheap to use.)  If one is using a zero finder that uses reverse communication, using bind will add significant overhead to the call. By storing the constants internally in the structure, as in the original example, you trade more or less constant heap storage for very dynamic stack storage of the constants. I also like John&#8217;s approach because you don&#8217;t have to drag boost into the problem.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sohail</title>
		<link>http://www.johndcook.com/blog/2008/12/19/functional-programming-in-c-with-function-objects/comment-page-1/#comment-11205</link>
		<dc:creator>Sohail</dc:creator>
		<pubDate>Sat, 20 Dec 2008 05:48:03 +0000</pubDate>
		<guid isPermaLink="false">http://www.johndcook.com/blog/?p=1122#comment-11205</guid>
		<description>I would much rather prefer something like:

struct Function
{
  double operator()(double a, double b, double c, double x)
  {  return 1000 * a + 100 * b + 10*c + x; }
};

double maximum = Maximize(bind(Function(),3,1,4,_1),a,b)

For this example, it is kind of overkill but my general preference is to prefer free functions and boost bind to generate functions :-)</description>
		<content:encoded><![CDATA[<p>I would much rather prefer something like:</p>
<p>struct Function<br />
{<br />
  double operator()(double a, double b, double c, double x)<br />
  {  return 1000 * a + 100 * b + 10*c + x; }<br />
};</p>
<p>double maximum = Maximize(bind(Function(),3,1,4,_1),a,b)</p>
<p>For this example, it is kind of overkill but my general preference is to prefer free functions and boost bind to generate functions <img src='http://www.johndcook.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.513 seconds -->

