<?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: Higher-Order Functions with boost::lambda</title>
	<atom:link href="http://mark.santaniello.com/archives/126/feed" rel="self" type="application/rss+xml" />
	<link>http://mark.santaniello.com/archives/126</link>
	<description>the body of a very slow loop</description>
	<pubDate>Fri, 21 Nov 2008 10:14:03 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Bheeshmar</title>
		<link>http://mark.santaniello.com/archives/126#comment-737</link>
		<dc:creator>Bheeshmar</dc:creator>
		<pubDate>Mon, 21 Nov 2005 17:37:52 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=126#comment-737</guid>
		<description>Very cool, Kevin.  I hoped that an implicit conversion operator would get a nicer syntax to work, but I couldn't figure it out...</description>
		<content:encoded><![CDATA[<p>Very cool, Kevin.  I hoped that an implicit conversion operator would get a nicer syntax to work, but I couldn&#8217;t figure it out&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kevin Frei</title>
		<link>http://mark.santaniello.com/archives/126#comment-690</link>
		<dc:creator>Kevin Frei</dc:creator>
		<pubDate>Thu, 17 Nov 2005 16:23:56 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=126#comment-690</guid>
		<description>You need closures.  I'm not quite enough of a template god to get this one right, but this works fine (though the .get()(args) syntax really sucks):

&lt;pre&gt;&lt;code&gt;
#include &#60;cstdio&#62;
#include &#60;boost&#47;lambda&#47;lambda.hpp&#62;
#include &#60;boost&#47;function.hpp&#62;
#include &#60;boost&#47;shared_ptr.hpp&#62;
#include &#60;iostream&#62;
&#9;
using namespace std&#59;
using namespace boost&#59;
using namespace boost&#58;&#58;lambda&#59;


template &#60;typename T, typename D&#62;
class closure
{
    D d&#59;
    T t&#59;
public&#58;
    closure(const T &#38;t, const D &#38;d) &#58; d(d), t(t) {}
    T &#38;get() { return t&#59; }
}&#59;

template &#60;typename T&#62;
closure&#60; boost&#58;&#58;function&#60;T(T)&#62;,
boost&#58;&#58;shared_ptr&#60;T&#62; &#62; foo( T n_ ) {
    typedef boost&#58;&#58;function&#60;T(T)&#62; fn&#59;
    typedef boost&#58;&#58;shared_ptr&#60;T&#62;  ptr&#59;
    ptr n( new T(n_) )&#59;
    return closure&#60;fn, ptr&#62;(var(*n) += _1, n)&#59; }

typedef closure&#60; boost&#58;&#58;function&#60;short(short)&#62;,
boost&#58;&#58;shared_ptr&#60;short&#62; &#62; functor_short&#59; typedef
closure&#60; boost&#58;&#58;function&#60;char(char)&#62;,
boost&#58;&#58;shared_ptr&#60;char&#62; &#62; functor_char&#59;

int main()
{
    &#47;&#47; start with one
    {
        functor_short accum = foo((short)1)&#59;
        cout &#60;&#60; accum.get()(2) &#60;&#60; &#34; should be 3&#34;
&#60;&#60; endl&#59;
        cout &#60;&#60; accum.get()(10) &#60;&#60; &#34; should be 13&#34;
&#60;&#60; endl&#59;
        functor_short accum2 = foo((short)101)&#59;
        cout &#60;&#60; accum2.get()(2) &#60;&#60; &#34; should be
103&#34; &#60;&#60; endl&#59;
        cout &#60;&#60; accum2.get()(10) &#60;&#60; &#34; should be
113&#34; &#60;&#60; endl&#59;    
    }
    functor_char accum = foo((char)3)&#59;
    cout &#60;&#60; (int)accum.get()(3) &#60;&#60; &#34; should be 6&#34;
&#60;&#60; endl&#59;
    cout &#60;&#60; (int)accum.get()(13) &#60;&#60; &#34; should be
19&#34; &#60;&#60; endl&#59; 
}
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>You need closures.  I&#8217;m not quite enough of a template god to get this one right, but this works fine (though the .get()(args) syntax really sucks):</p>
<pre><code>
#include &lt;cstdio&gt;
#include &lt;boost&#47;lambda&#47;lambda.hpp&gt;
#include &lt;boost&#47;function.hpp&gt;
#include &lt;boost&#47;shared_ptr.hpp&gt;
#include &lt;iostream&gt;
&#9;
using namespace std&#59;
using namespace boost&#59;
using namespace boost&#58;&#58;lambda&#59;

template &lt;typename T, typename D&gt;
class closure
{
    D d&#59;
    T t&#59;
public&#58;
    closure(const T &amp;t, const D &amp;d) &#58; d(d), t(t) {}
    T &amp;get() { return t&#59; }
}&#59;

template &lt;typename T&gt;
closure&lt; boost&#58;&#58;function&lt;T(T)&gt;,
boost&#58;&#58;shared_ptr&lt;T&gt; &gt; foo( T n_ ) {
    typedef boost&#58;&#58;function&lt;T(T)&gt; fn&#59;
    typedef boost&#58;&#58;shared_ptr&lt;T&gt;  ptr&#59;
    ptr n( new T(n_) )&#59;
    return closure&lt;fn, ptr&gt;(var(*n) += _1, n)&#59; }

typedef closure&lt; boost&#58;&#58;function&lt;short(short)&gt;,
boost&#58;&#58;shared_ptr&lt;short&gt; &gt; functor_short&#59; typedef
closure&lt; boost&#58;&#58;function&lt;char(char)&gt;,
boost&#58;&#58;shared_ptr&lt;char&gt; &gt; functor_char&#59;

int main()
{
    &#47;&#47; start with one
    {
        functor_short accum = foo((short)1)&#59;
        cout &lt;&lt; accum.get()(2) &lt;&lt; &quot; should be 3&quot;
&lt;&lt; endl&#59;
        cout &lt;&lt; accum.get()(10) &lt;&lt; &quot; should be 13&quot;
&lt;&lt; endl&#59;
        functor_short accum2 = foo((short)101)&#59;
        cout &lt;&lt; accum2.get()(2) &lt;&lt; &quot; should be
103&quot; &lt;&lt; endl&#59;
        cout &lt;&lt; accum2.get()(10) &lt;&lt; &quot; should be
113&quot; &lt;&lt; endl&#59;
    }
    functor_char accum = foo((char)3)&#59;
    cout &lt;&lt; (int)accum.get()(3) &lt;&lt; &quot; should be 6&quot;
&lt;&lt; endl&#59;
    cout &lt;&lt; (int)accum.get()(13) &lt;&lt; &quot; should be
19&quot; &lt;&lt; endl&#59;
}
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://mark.santaniello.com/archives/126#comment-684</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Wed, 16 Nov 2005 06:27:55 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=126#comment-684</guid>
		<description>Bheesh - Ruby is bad-ass.
Ryan - Don't be thilly, you thexy boy.</description>
		<content:encoded><![CDATA[<p>Bheesh - Ruby is bad-ass.<br />
Ryan - Don&#8217;t be thilly, you thexy boy.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan</title>
		<link>http://mark.santaniello.com/archives/126#comment-678</link>
		<dc:creator>Ryan</dc:creator>
		<pubDate>Tue, 15 Nov 2005 21:02:02 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=126#comment-678</guid>
		<description>Mark -

I don't have to remind you about what society thinks of guys who lisp do I?</description>
		<content:encoded><![CDATA[<p>Mark -</p>
<p>I don&#8217;t have to remind you about what society thinks of guys who lisp do I?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bheeshmar</title>
		<link>http://mark.santaniello.com/archives/126#comment-677</link>
		<dc:creator>Bheeshmar</dc:creator>
		<pubDate>Tue, 15 Nov 2005 16:14:29 +0000</pubDate>
		<guid isPermaLink="false">http://mark.santaniello.net/?p=126#comment-677</guid>
		<description># In the language of my scarlet mistress (Ruby)
def foo( initial )
    return lambda { &#124;n&#124; n+=initial }
end

accum = foo(1)
puts "#{accum[2]}"
puts "#{accum[10]}"</description>
		<content:encoded><![CDATA[<p># In the language of my scarlet mistress (Ruby)<br />
def foo( initial )<br />
    return lambda { |n| n+=initial }<br />
end</p>
<p>accum = foo(1)<br />
puts &#8220;#{accum[2]}&#8221;<br />
puts &#8220;#{accum[10]}&#8221;</p>
]]></content:encoded>
	</item>
</channel>
</rss>
