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–17, at 7:50 PM, Markus Grech <<a href=3D"mailto:markus.gr=
ech@gmail.com">markus.grech@gmail.com</a>> 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’re jum=
ping straight to the core language with no usage experience.</div><div><br>=
</div><div>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 dru=
nk :P</div><div><br></div><div><br></div><div><font face=3D"Courier">#inclu=
de <tuple><br>#include <utility><br><br>template< typename t=
uple_type, typename ftor, typename acc_type ><br>auto && tuple_f=
old_impl( tuple_type && t, ftor f, acc_type && acc,<br>&nbs=
p; std::index_sequence<> )<br> { return=
std::forward< acc_type >( acc ); }<br><br>template< typename tupl=
e_type, typename ftor, typename acc_type,<br> std::size_t=
index, std::size_t ... index_tail ><br>decltype(auto) tuple_fold_impl( =
tuple_type && t, ftor f, acc_type && acc,<br> &=
nbsp;std::index_sequence< index, index_tail ... > ) {<br>  =
; return f(<br> tuple_fold_impl(<br>&n=
bsp; std::forward< tuple_type &g=
t;( t ),<br> f,<br> &nb=
sp; std::forward< acc_type >( acc ),=
<br> std::index_sequence< =
index_tail ... >{}<br> ),<br> &nbs=
p; std::get< index >( std::forward< tuple_type =
>( t ) )<br> );<br>}<br><br>template< typename tupl=
e_type, typename ftor, std::size_t ... index_tail ><br>decltype(auto) tu=
ple_fold_impl( tuple_type && t, ftor f, std::index_sequence< 0, =
index_tail ... > ) {<br> return tuple_fold_impl(<br>&n=
bsp; std::forward< tuple_type >( t ),<br>&n=
bsp; f,<br> std::=
get< 0 >( std::forward< tuple_type >( t ) ),<br> &=
nbsp; std::index_sequence< index_tail ... >{}<br> &=
nbsp; );<br>}<br><br>template< typename tuple_type, typename ftor,<=
br> typename seq =3D std::make_index_sequence< std::tu=
ple_size< tuple_type >::value > ><br>decltype(auto) tuple_fold(=
tuple_type && t, ftor f )<br> { return tuple_fol=
d_impl( std::forward< tuple_type >( t ), f, seq{} ); }<br><br>templat=
e< typename tuple_type, typename ftor, typename acc_type,<br> &nbs=
p; typename seq =3D std::make_index_sequence< std::tuple_size< t=
uple_type >::value > ><br>decltype(auto) tuple_fold( tuple_type &a=
mp;& t, ftor f, acc_type && acc )<br> { retur=
n tuple_fold_impl( std::forward< tuple_type >( t ), f, std::forward&l=
t; acc_type >( acc ), seq{} ); }<br><br>#include <iostream><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 & s, auto && o=
) -></span><span style=3D"font-family: Courier;"> </span><span sty=
le=3D"font-family: Courier;">std::ostream & { return s << 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" 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, "Markus Grech" <<a href="mailto:markus.grech@gmail.com">markus.grech@gmail.com</a>> wrote:<br>
><br>
> 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>
> (args + ... 0) // foldl args (+) 0<br>
> (0 ... + args) // foldr args (+) 0<br>
> Would this work?</p>
<p dir="ltr">That is already in the proposal (with a very slightly different syntax).</p>
<p dir="ltr">> -- <br>
><br>
> --- <br>
> You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.<br>
> 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>
> 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>
</p>
<p></p>
-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" 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--
.