Topic: Comment on N4191 - Folding expressions


Author: David Krauss <potswa@gmail.com>
Date: Fri, 17 Oct 2014 20:43:05 +0800
Raw View
--Apple-Mail=_2EEC6574-5FA2-41CE-A331-A38446865730
Content-Type: text/plain; charset=ISO-8859-1


On 2014-10-17, at 7:50 PM, Markus Grech <markus.grech@gmail.com> wrote:

> In functional languages, the initial value of a fold can be specified by the user. Why does the proposal mandate a specific value? Is there anything that prevents the following syntax:
> (args + ... 0) // foldl args (+) 0
> (0 ... + args) // foldr args (+) 0
> Would this work?

It would be easy to do, with a tuple_fold function.

What worries me about this proposal is that its functionality is easily done with a generic template, yet no such template is in popular use, nor does a library proposal exist. We're jumping straight to the core language with no usage experience.

Here's some sort-of working library code. Only problem is, I wanted a left-fold but I got a right-fold because I'm a little drunk :P


#include <tuple>
#include <utility>

template< typename tuple_type, typename ftor, typename acc_type >
auto && tuple_fold_impl( tuple_type && t, ftor f, acc_type && acc,
    std::index_sequence<> )
    { return std::forward< acc_type >( acc ); }

template< typename tuple_type, typename ftor, typename acc_type,
    std::size_t index, std::size_t ... index_tail >
decltype(auto) tuple_fold_impl( tuple_type && t, ftor f, acc_type && acc,
    std::index_sequence< index, index_tail ... > ) {
    return f(
        tuple_fold_impl(
            std::forward< tuple_type >( t ),
            f,
            std::forward< acc_type >( acc ),
            std::index_sequence< index_tail ... >{}
        ),
        std::get< index >( std::forward< tuple_type >( t ) )
    );
}

template< typename tuple_type, typename ftor, std::size_t ... index_tail >
decltype(auto) tuple_fold_impl( tuple_type && t, ftor f, std::index_sequence< 0, index_tail ... > ) {
    return tuple_fold_impl(
        std::forward< tuple_type >( t ),
        f,
        std::get< 0 >( std::forward< tuple_type >( t ) ),
        std::index_sequence< index_tail ... >{}
    );
}

template< typename tuple_type, typename ftor,
    typename seq = std::make_index_sequence< std::tuple_size< tuple_type >::value > >
decltype(auto) tuple_fold( tuple_type && t, ftor f )
    { return tuple_fold_impl( std::forward< tuple_type >( t ), f, seq{} ); }

template< typename tuple_type, typename ftor, typename acc_type,
    typename seq = std::make_index_sequence< std::tuple_size< tuple_type >::value > >
decltype(auto) tuple_fold( tuple_type && t, ftor f, acc_type && acc )
    { return tuple_fold_impl( std::forward< tuple_type >( t ), f, std::forward< acc_type >( acc ), seq{} ); }

#include <iostream>

int main() {
tuple_fold( std::make_tuple( 1, " hello ", false, std::boolalpha, '\n' ), [] ( std::ostream & s, auto && o ) -> std::ostream & { return s << o; }, std::cout );
}


--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

--Apple-Mail=_2EEC6574-5FA2-41CE-A331-A38446865730
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dwindows-1252"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-=
mode: space; -webkit-line-break: after-white-space;"><br><div><div>On 2014&=
ndash;10&ndash;17, at 7:50 PM, Markus Grech &lt;<a href=3D"mailto:markus.gr=
ech@gmail.com">markus.grech@gmail.com</a>&gt; wrote:</div><br class=3D"Appl=
e-interchange-newline"><blockquote type=3D"cite"><div dir=3D"ltr">In functi=
onal languages, the initial value of a fold can be specified by the user. W=
hy does the proposal mandate a specific value? Is there anything that preve=
nts the following syntax:<div><div class=3D"prettyprint" style=3D"border: 1=
px solid rgb(187, 187, 187); word-wrap: break-word; background-color: rgb(2=
50, 250, 250);"><code class=3D"prettyprint"><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">(</span>args <span style=3D"color: #660;" class=
=3D"styled-by-prettify">+</span> <span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">...</span> <span style=3D"color: #066;" class=3D"styled-by-=
prettify">0</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>)</span> <span style=3D"color: #800;" class=3D"styled-by-prettify">// fold=
l args (+) 0</span><br><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">(</span><span style=3D"color: #066;" class=3D"styled-by-prettify">0</=
span> <span style=3D"color: #660;" class=3D"styled-by-prettify">...</span> =
<span style=3D"color: #660;" class=3D"styled-by-prettify">+</span> args<spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">)</span> <span style=
=3D"color: #800;" class=3D"styled-by-prettify">// foldr args (+) 0</span><b=
r></code></div>Would this work?</div></div></blockquote><div><br></div><div=
>It would be easy to do, with a <font face=3D"Courier">tuple_fold</font> fu=
nction.</div><div><br></div><div>What worries me about this proposal is tha=
t its functionality is easily done with a generic template, yet no such tem=
plate is in popular use, nor does a library proposal exist. We&rsquo;re jum=
ping straight to the core language with no usage experience.</div><div><br>=
</div><div>Here&rsquo;s some sort-of working library code. Only problem is,=
 I wanted a left-fold but I got a right-fold because I&rsquo;m a little dru=
nk :P</div><div><br></div><div><br></div><div><font face=3D"Courier">#inclu=
de &lt;tuple&gt;<br>#include &lt;utility&gt;<br><br>template&lt; typename t=
uple_type, typename ftor, typename acc_type &gt;<br>auto &amp;&amp; tuple_f=
old_impl( tuple_type &amp;&amp; t, ftor f, acc_type &amp;&amp; acc,<br>&nbs=
p; &nbsp;&nbsp;std::index_sequence&lt;&gt; )<br>&nbsp; &nbsp;&nbsp;{ return=
 std::forward&lt; acc_type &gt;( acc ); }<br><br>template&lt; typename tupl=
e_type, typename ftor, typename acc_type,<br>&nbsp; &nbsp;&nbsp;std::size_t=
 index, std::size_t ... index_tail &gt;<br>decltype(auto) tuple_fold_impl( =
tuple_type &amp;&amp; t, ftor f, acc_type &amp;&amp; acc,<br>&nbsp; &nbsp;&=
nbsp;std::index_sequence&lt; index, index_tail ... &gt; ) {<br>&nbsp; &nbsp=
;&nbsp;return f(<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;tuple_fold_impl(<br>&n=
bsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;std::forward&lt; tuple_type &g=
t;( t ),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;f,<br>&nbsp; &nb=
sp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;std::forward&lt; acc_type &gt;( acc ),=
<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;std::index_sequence&lt; =
index_tail ... &gt;{}<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;),<br>&nbsp; &nbs=
p; &nbsp; &nbsp;&nbsp;std::get&lt; index &gt;( std::forward&lt; tuple_type =
&gt;( t ) )<br>&nbsp; &nbsp;&nbsp;);<br>}<br><br>template&lt; typename tupl=
e_type, typename ftor, std::size_t ... index_tail &gt;<br>decltype(auto) tu=
ple_fold_impl( tuple_type &amp;&amp; t, ftor f, std::index_sequence&lt; 0, =
index_tail ... &gt; ) {<br>&nbsp; &nbsp;&nbsp;return tuple_fold_impl(<br>&n=
bsp; &nbsp; &nbsp; &nbsp;&nbsp;std::forward&lt; tuple_type &gt;( t ),<br>&n=
bsp; &nbsp; &nbsp; &nbsp;&nbsp;f,<br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;std::=
get&lt; 0 &gt;( std::forward&lt; tuple_type &gt;( t ) ),<br>&nbsp; &nbsp; &=
nbsp; &nbsp;&nbsp;std::index_sequence&lt; index_tail ... &gt;{}<br>&nbsp; &=
nbsp;&nbsp;);<br>}<br><br>template&lt; typename tuple_type, typename ftor,<=
br>&nbsp; &nbsp;&nbsp;typename seq =3D std::make_index_sequence&lt; std::tu=
ple_size&lt; tuple_type &gt;::value &gt; &gt;<br>decltype(auto) tuple_fold(=
 tuple_type &amp;&amp; t, ftor f )<br>&nbsp; &nbsp;&nbsp;{ return tuple_fol=
d_impl( std::forward&lt; tuple_type &gt;( t ), f, seq{} ); }<br><br>templat=
e&lt; typename tuple_type, typename ftor, typename acc_type,<br>&nbsp; &nbs=
p;&nbsp;typename seq =3D std::make_index_sequence&lt; std::tuple_size&lt; t=
uple_type &gt;::value &gt; &gt;<br>decltype(auto) tuple_fold( tuple_type &a=
mp;&amp; t, ftor f, acc_type &amp;&amp; acc )<br>&nbsp; &nbsp;&nbsp;{ retur=
n tuple_fold_impl( std::forward&lt; tuple_type &gt;( t ), f, std::forward&l=
t; acc_type &gt;( acc ), seq{} ); }<br><br>#include &lt;iostream&gt;<br><br=
>int main() {<br>tuple_fold( std::make_tuple( 1, " hello ", false</font><sp=
an style=3D"font-family: Courier;">, std::boolalpha</span><span style=3D"fo=
nt-family: Courier;">, '\n' ), [] ( std::ostream &amp; s, auto &amp;&amp; o=
 ) -&gt;</span><span style=3D"font-family: Courier;">&nbsp;</span><span sty=
le=3D"font-family: Courier;">std::ostream &amp; { return s &lt;&lt; o; }, s=
td::cout );</span></div><div><font face=3D"Courier">}<br><br></font><br></d=
iv></div></body></html>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--Apple-Mail=_2EEC6574-5FA2-41CE-A331-A38446865730--

.


Author: Richard Smith <richard@metafoo.co.uk>
Date: Fri, 17 Oct 2014 09:30:44 -0700
Raw View
--089e01293f8a04763f0505a0e37c
Content-Type: text/plain; charset=UTF-8

On 17 Oct 2014 04:50, "Markus Grech" <markus.grech@gmail.com> wrote:
>
> In functional languages, the initial value of a fold can be specified by
the user. Why does the proposal mandate a specific value? Is there anything
that prevents the following syntax:
> (args + ... 0) // foldl args (+) 0
> (0 ... + args) // foldr args (+) 0
> Would this work?

That is already in the proposal (with a very slightly different syntax).

> --
>
> ---
> You received this message because you are subscribed to the Google Groups
"ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this group and stop receiving emails from it, send an
email to std-proposals+unsubscribe@isocpp.org.
> To post to this group, send email to std-proposals@isocpp.org.
> Visit this group at
http://groups.google.com/a/isocpp.org/group/std-proposals/.

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

--089e01293f8a04763f0505a0e37c
Content-Type: text/html; charset=UTF-8

<p dir="ltr"><br>
On 17 Oct 2014 04:50, &quot;Markus Grech&quot; &lt;<a href="mailto:markus.grech@gmail.com">markus.grech@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; In functional languages, the initial value of a fold can be specified by the user. Why does the proposal mandate a specific value? Is there anything that prevents the following syntax:<br>
&gt; (args + ... 0) // foldl args (+) 0<br>
&gt; (0 ... + args) // foldr args (+) 0<br>
&gt; Would this work?</p>
<p dir="ltr">That is already in the proposal (with a very slightly different syntax).</p>
<p dir="ltr">&gt; -- <br>
&gt;<br>
&gt; --- <br>
&gt; You received this message because you are subscribed to the Google Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br>
&gt; To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals%2Bunsubscribe@isocpp.org">std-proposals+unsubscribe@isocpp.org</a>.<br>
&gt; To post to this group, send email to <a href="mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>.<br>
&gt; Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br>
</p>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an email to <a href="mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href="mailto:std-proposals@isocpp.org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href="http://groups.google.com/a/isocpp.org/group/std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/</a>.<br />

--089e01293f8a04763f0505a0e37c--

.