220 13979 <9F045410-BC6B-492D-B2BD-03AADFA7EDE0@gmail.com> article
Path: news.gmane.org!not-for-mail
From: David Krauss <potswa@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Re: Comment on N4191 - Folding expressions
 (Sutton, Smith)
Date: Fri, 17 Oct 2014 20:43:05 +0800
Lines: 172
Approved: news@gmane.org
Message-ID: <9F045410-BC6B-492D-B2BD-03AADFA7EDE0@gmail.com>
References: <c0667403-de73-4dfb-947a-d25363d2928f@isocpp.org> <c62ae546-01ea-406f-ae1f-2eeb5825e849@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\))
Content-Type: multipart/alternative; boundary="Apple-Mail=_2EEC6574-5FA2-41CE-A331-A38446865730"
X-Trace: ger.gmane.org 1413549816 17332 80.91.229.3 (17 Oct 2014 12:43:36 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Fri, 17 Oct 2014 12:43:36 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCW25A7E3QCRB3U5QSRAKGQEDEOKSAA@isocpp.org Fri Oct 17 14:43:29 2014
Return-path: <std-proposals+bncBCW25A7E3QCRB3U5QSRAKGQEDEOKSAA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yh0-f71.google.com ([209.85.213.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCW25A7E3QCRB3U5QSRAKGQEDEOKSAA@isocpp.org>)
	id 1Xf6sS-0000yV-8t
	for gclcip-std-proposals@m.gmane.org; Fri, 17 Oct 2014 14:43:28 +0200
Original-Received: by mail-yh0-f71.google.com with SMTP id f73sf1565830yha.6
        for <gclcip-std-proposals@m.gmane.org>; Fri, 17 Oct 2014 05:43:27 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:message-id:mime-version:subject:date
         :references:to:in-reply-to:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe:content-type;
        bh=0VznHZOZfae7XiS5qFG9GnNgK6hIC7ArhpDnc2phqKg=;
        b=Bw1qrV6psJqlsT4rwV29eIWdjq0WgYAwSoAKSwTpT6zYGnfujz6GTUrPN0Y7x9HnQe
         LI+eIQEzwdEXzI40kuHdhLcXOCVqXgq1X0z6/dq1TsDJcUmgn3ObjO3MO/UsQ81hVMZ+
         lIm2wMRdWGKAsMqRK3IeLTNRcTqslBkwinPHEVOxuE4/yG+OqBs5fhLQ8fRqOXwbvFcI
         s48dqGJWjbG+yVQKZtIyCy6xfTRmDXlDG5vzVzaT1JGfLAwoBKawAastug2Msw3rTp0Q
         RsqHd/NG+i+UT/+IVna13LkqyyONXP7yYl/fjKrgFi9dollpbyTjF6sfoHbZMcc06WhF
         g+mQ==
X-Gm-Message-State: ALoCoQlC5ofp//25jkxdwFUf8283RZqcp5/aXP18XR/VrP926+DyN0c4m5PkuwnEYXexx4arsJxT
X-Received: by 10.236.39.174 with SMTP id d34mr5382179yhb.54.1413549807018;
        Fri, 17 Oct 2014 05:43:27 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.11.23 with SMTP id v23ls867970ioi.81.gmail; Fri, 17 Oct
 2014 05:43:26 -0700 (PDT)
X-Received: by 10.70.61.202 with SMTP id s10mr4925461pdr.124.1413549806228;
        Fri, 17 Oct 2014 05:43:26 -0700 (PDT)
Original-Received: from mail-pa0-x236.google.com (mail-pa0-x236.google.com. [2607:f8b0:400e:c03::236])
        by mx.google.com with ESMTPS id b2si1165942pde.9.2014.10.17.05.43.26
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Fri, 17 Oct 2014 05:43:26 -0700 (PDT)
Received-SPF: pass (google.com: domain of potswa@gmail.com designates 2607:f8b0:400e:c03::236 as permitted sender) client-ip=2607:f8b0:400e:c03::236;
Original-Received: by mail-pa0-f54.google.com with SMTP id ey11so782095pad.13
        for <std-proposals@isocpp.org>; Fri, 17 Oct 2014 05:43:25 -0700 (PDT)
X-Received: by 10.68.231.33 with SMTP id td1mr8083736pbc.133.1413549805462;
        Fri, 17 Oct 2014 05:43:25 -0700 (PDT)
Original-Received: from [172.20.10.2] ([121.54.54.144])
        by mx.google.com with ESMTPSA id oq6sm1503777pdb.45.2014.10.17.05.43.18
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Fri, 17 Oct 2014 05:43:24 -0700 (PDT)
In-Reply-To: <c62ae546-01ea-406f-ae1f-2eeb5825e849@isocpp.org>
X-Mailer: Apple Mail (2.1878.6)
X-Original-Sender: potswa@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of potswa@gmail.com designates 2607:f8b0:400e:c03::236 as permitted
 sender) smtp.mail=potswa@gmail.com;       dkim=pass header.i=@gmail.com;
       dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:13979
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/13979>

--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--

.
