Topic: transform_while()


Author: matan.nassau@gmail.com
Date: Sat, 20 Dec 2014 08:02:56 -0800 (PST)
Raw View
------=_Part_1554_587858861.1419091376270
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

Would a std::transform_while() algorithm be useful?

It would be equivalent to

last =3D std::find_if(first, last, pred);
std::transform(first, last, out, op);
return out;

except it would do it in a single pass.

It's something I find I occasionally do, but without standard algorithms be=
cause I haven't found a way to do it in a single pass other than with a raw=
 loop.

When I write the generic algorithm it does feel clumsy because it takes bot=
h a predicate and a unary operation. But I wish there were standard algorit=
hms that would allow me to get something done with only as strong iterators=
 as it takes (here, input iterators and not forward iterators).

Someone mentioned to me that maybe this can be done nicely in the new range=
 proposal from Eric Niebler, using a filtered view on the input range, but =
I wasn't able to test that yet.

--=20

---=20
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 e=
mail 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-proposa=
ls/.

------=_Part_1554_587858861.1419091376270--

.


Author: Scott Prager <splinterofchaos@gmail.com>
Date: Mon, 22 Dec 2014 13:21:16 -0800 (PST)
Raw View
------=_Part_818_240771074.1419283276339
Content-Type: multipart/alternative;
 boundary="----=_Part_819_780249770.1419283276340"

------=_Part_819_780249770.1419283276340
Content-Type: text/plain; charset=UTF-8



On Saturday, December 20, 2014 11:02:56 AM UTC-5, Matan Nassau wrote:
>
> Would a std::transform_while() algorithm be useful?
>
> It would be equivalent to
>
> last = std::find_if(first, last, pred);
> std::transform(first, last, out, op);
> return out;
>

Shouldn't that be `last = std::find_if_not(first, last, pred);`?

I think this feature would be useful for for_each, accumulate,
inner_product, and maybe even others that don't immediately seem useful,
like copy and move. Unfortunately, until now, I've only known how to do
this with sentinel iterators, and even then, that fact that all algorithms
currently require both iterators to be the same type is a nuisance. (Why
hasn't this been proposed or accepted yet? I remember reading an Eric
Neibler blog post about it over a year ago!)


>
> except it would do it in a single pass.
>
> It's something I find I occasionally do, but without standard algorithms
> because I haven't found a way to do it in a single pass other than with a
> raw loop.
>

A little off-topic, but I have toyed around in my mind whether it would be
worth while to extent the behaviour of the range-based for loop to take
care of these cases.
for(auto x :xs  while(x<5))
 ...

for(auto x :xs  if (x % 2 == 0))  // filter-like loop
 ...




>
> When I write the generic algorithm it does feel clumsy because it takes
> both a predicate and a unary operation. But I wish there were standard
> algorithms that would allow me to get something done with only as strong
> iterators as it takes (here, input iterators and not forward iterators).
>
> Someone mentioned to me that maybe this can be done nicely in the new
> range proposal from Eric Niebler, using a filtered view on the input range,
> but I wasn't able to test that yet.


I believe that would be...
transform(view::take_while(rng, pred), f);



--

---
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/.

------=_Part_819_780249770.1419283276340
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Saturday, December 20, 2014 11:02:56 AM UTC-5, =
Matan Nassau wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;mar=
gin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Would a std=
::transform_while() algorithm be useful?
<br>
<br>It would be equivalent to
<br>
<br>last =3D std::find_if(first, last, pred);
<br>std::transform(first, last, out, op);
<br>return out;
<br></blockquote><div><br></div><div>Shouldn't that be `last =3D std::find_=
if_not(first, last, pred);`?</div><div><br></div><div>I think this feature =
would be useful for for_each, accumulate, inner_product, and maybe even oth=
ers that don't immediately seem useful, like copy and move. Unfortunately, =
until now, I've only known how to do this with sentinel iterators, and even=
 then, that fact that all algorithms currently require both iterators to be=
 the same type is a nuisance. (Why hasn't this been proposed or accepted ye=
t? I remember reading an Eric Neibler blog post about it over a year ago!)<=
/div><div>&nbsp;</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;=
margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">
<br>except it would do it in a single pass.
<br>
<br>It's something I find I occasionally do, but without standard algorithm=
s because I haven't found a way to do it in a single pass other than with a=
 raw loop.
<br></blockquote><div><br></div><div>A little off-topic, but I have toyed a=
round in my mind whether it would be worth while to extent the behaviour of=
 the range-based for loop to take care of these cases.</div><div><div class=
=3D"prettyprint" style=3D"border: 1px solid rgb(187, 187, 187); word-wrap: =
break-word; background-color: rgb(250, 250, 250);"><code class=3D"prettypri=
nt"><div class=3D"subprettyprint"><span style=3D"color: #008;" class=3D"sty=
led-by-prettify">for</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>auto</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> x </=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">:</span><spa=
n style=3D"color: #000;" class=3D"styled-by-prettify">xs &nbsp;</span><span=
 style=3D"color: #008;" class=3D"styled-by-prettify">while</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">x</span><span style=3D"color: #660;"=
 class=3D"styled-by-prettify">&lt;</span><span style=3D"color: #066;" class=
=3D"styled-by-prettify">5</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">))</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"><br>&nbsp;</span><span style=3D"color: #660;" class=3D"styled-by-pre=
ttify">...</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
<br><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">fo=
r</span><span style=3D"color: #660;" class=3D"styled-by-prettify">(</span><=
span style=3D"color: #008;" class=3D"styled-by-prettify">auto</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> x </span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">:</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify">xs &nbsp;</span><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">if</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify">x </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
%</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #066;" class=3D"styled-by-prettify">2</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"colo=
r: #660;" class=3D"styled-by-prettify">=3D=3D</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #066;" cla=
ss=3D"styled-by-prettify">0</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">))</span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> &nbsp;</span><span style=3D"color: #800;" class=3D"styled-by-pret=
tify">// filter-like loop</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br>&nbsp;</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">...</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"><br></span></div></code></div><br><br></div><div>&nbsp;</div><block=
quote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-le=
ft: 1px #ccc solid;padding-left: 1ex;">
<br>When I write the generic algorithm it does feel clumsy because it takes=
 both a predicate and a unary operation. But I wish there were standard alg=
orithms that would allow me to get something done with only as strong itera=
tors as it takes (here, input iterators and not forward iterators).
<br>
<br>Someone mentioned to me that maybe this can be done nicely in the new r=
ange proposal from Eric Niebler, using a filtered view on the input range, =
but I wasn't able to test that yet.</blockquote><div><br></div><div>I belie=
ve that would be...</div><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"><div class=3D"subprettyprint"><=
span style=3D"color: #000;" class=3D"styled-by-prettify">transform</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">view</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #00=
0;" class=3D"styled-by-prettify">take_while</span><span style=3D"color: #66=
0;" class=3D"styled-by-prettify">(</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">rng</span><span style=3D"color: #660;" class=3D"sty=
led-by-prettify">,</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> pred</span><span style=3D"color: #660;" class=3D"styled-by-prettif=
y">),</span><font color=3D"#000000"><span style=3D"color: #000;" class=3D"s=
tyled-by-prettify"> </span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">f</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>);</span></font><font color=3D"#000000"></font></div></code></div><br>&nbs=
p;</div></div>

<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 />

------=_Part_819_780249770.1419283276340--
------=_Part_818_240771074.1419283276339--

.


Author: Farid Mehrabi <farid.mehrabi@gmail.com>
Date: Tue, 23 Dec 2014 19:10:13 +0330
Raw View
--001a1134c69ce93e48050ae3fe5c
Content-Type: text/plain; charset=UTF-8

2014-12-20 19:32 GMT+03:30 <matan.nassau@gmail.com>:

> Would a std::transform_while() algorithm be useful?
>
> It would be equivalent to
>
> last = std::find_if(first, last, pred);
> std::transform(first, last, out, op);
> return out;
>
how about:
   pred p{out};
   last = std::find_if(first, last, p);
   return p.lastOut();

>
> except it would do it in a single pass.
>
> It's something I find I occasionally do, but without standard algorithms
> because I haven't found a way to do it in a single pass other than with a
> raw loop.
>
> When I write the generic algorithm it does feel clumsy because it takes
> both a predicate and a unary operation. But I wish there were standard
> algorithms that would allow me to get something done with only as strong
> iterators as it takes (here, input iterators and not forward iterators).
>
> Someone mentioned to me that maybe this can be done nicely in the new
> range proposal from Eric Niebler, using a filtered view on the input range,
> but I wasn't able to test that yet.
>
> --
>
> ---
> 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/.
>



--
how am I supposed to end the twisted road of  your hair in the dark night??
unless the candle of your face does turn a lamp up on my way!!!

--

---
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/.

--001a1134c69ce93e48050ae3fe5c
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"rtl"><div class=3D"gmail_extra" dir=3D"ltr"><br><div class=3D"g=
mail_quote"><div>2014-12-20 19:32 GMT+03:30  <span dir=3D"ltr">&lt;<a href=
=3D"mailto:matan.nassau@gmail.com" target=3D"_blank">matan.nassau@gmail.com=
</a>&gt;</span>:</div><blockquote class=3D"gmail_quote" style=3D"margin:0px=
 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);bor=
der-left-style:solid;padding-left:1ex">Would a std::transform_while() algor=
ithm be useful?<br>
<br>
It would be equivalent to<br>
<br>
last =3D std::find_if(first, last, pred);<br>
std::transform(first, last, out, op);<br>
return out;<br></blockquote><div>how about:</div><div>=C2=A0 =C2=A0pred p{o=
ut};</div><div>=C2=A0 =C2=A0last =3D std::find_if(first, last, p);=C2=A0</d=
iv><div>=C2=A0 =C2=A0return p.lastOut();=C2=A0</div><blockquote class=3D"gm=
ail_quote" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-l=
eft-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<br>
except it would do it in a single pass.<br>
<br>
It&#39;s something I find I occasionally do, but without standard algorithm=
s because I haven&#39;t found a way to do it in a single pass other than wi=
th a raw loop.<br>
<br>
When I write the generic algorithm it does feel clumsy because it takes bot=
h a predicate and a unary operation. But I wish there were standard algorit=
hms that would allow me to get something done with only as strong iterators=
 as it takes (here, input iterators and not forward iterators).<br>
<br>
Someone mentioned to me that maybe this can be done nicely in the new range=
 proposal from Eric Niebler, using a filtered view on the input range, but =
I wasn&#39;t able to test that yet.<br>
<span class=3D""><font color=3D"#888888"><br>
--<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%2Bunsubscribe@isocpp.org">std-propo=
sals+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/" target=3D"_blank">http://groups.google.com/a/isocpp.org/gro=
up/std-proposals/</a>.<br>
</font></span></blockquote></div><br><br clear=3D"all"><div><br></div>-- <b=
r><div class=3D"gmail_signature"><div>how am I supposed to end the twisted =
road of=C2=A0 your hair in the dark night??<br>unless the candle of your fa=
ce does turn a lamp up on my way!!!<br></div></div>
</div></div>

<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 />

--001a1134c69ce93e48050ae3fe5c--

.


Author: Dietmar Kuhl <dietmar.kuehl@me.com>
Date: Tue, 23 Dec 2014 18:05:38 +0100
Raw View
On 20 Dec 2014, at 17:02, matan.nassau@gmail.com wrote
> Would a std::transform_while() algorithm be useful?

The underlying facility, i.e., an algorithm which terminates based on a con=
dition rather than having reached a position is useful, e.g., when dealing =
with single-pass sequences. However, the way to go about it, i.e., adding a=
 new algorithm is, in my opinion, wrong. Instead, the algorithm interfaces =
taking input or forward iterators (as well as the definition for range-base=
d for) should be changed to allow different types for begin and the end ite=
rator. Of course, since the algorithm determines a position which is a prio=
ri unknown and may be useful to carry on from for further processing, it al=
so needs to return both the source and the destination iterator:

    template <typename InputIterator, typename EndPoint, typename OutputIte=
rator, typename Function>
    std::pair<InputIterator, OutputIterator>
    transform(InputIterator it, EndPoint end, OutputIterator to, Function f=
un) {
        for (; it !=3D end; ++it, ++to) {
            *to =3D fun(*it);
        }
        return std::make_pair(it, to);
    }

Although Eric Niebler has written a series of blogs on this topic (see http=
://ericniebler.com/2014/02/16/delimited-ranges/ for the first blog out of a=
 series of four) this discussion is fairly independent from the ranges prop=
osal (although ranges do benefit from this change). The same logic applies =
to all other algorithm taking input iterator (e.g., copy(), copy_if(), and =
for_each()) or forward iterators. In some cases an end iterator is supposed=
 to be produced which could be seen to imply that the end point would need =
a way to create an iterator. That's actually not the case, though: all the =
algorithms in the standard C++ library taking a forward iterator and return=
ing an end iterator under some condition do produce the an iterator compari=
ng equal to the end point anyway.

--=20

---=20
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 e=
mail 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-proposa=
ls/.

.