Topic: when() loop
Author: Sophie Schmidt <cosmocortney@outlook.de>
Date: Tue, 12 Mar 2019 03:05:57 -0700 (PDT)
Raw View
------=_Part_2527_1621137401.1552385157157
Content-Type: multipart/alternative;
boundary="----=_Part_2528_2095031283.1552385157157"
------=_Part_2528_2095031283.1552385157157
Content-Type: text/plain; charset="UTF-8"
Hello,
I've been writing some code to process vectors of classes by using for and
for_each loops. Instead of having long checks in the loop definition and/or
the body section of a loop, I was thinking of a kind of loop that only
jumps into its body, when the condition is true.
normal:
when(def; con; amount; iter)
{}
def: value definition
con: condition when the body is to be executed
amount: loop is executed until n true conditions have occurred. if amount =
0, then execute the loop until it hits break
ite: iterator
Example: jumps into the body part once the condition is true
when(int i = 0; vectorList[i].get_pid() == myPid; 3; i++)
{
cout << "occurrence found at: " << i;
}
for each of vector/array:
when_each(vectorList.begin().get_pid() == myPid)
{
cout << "occurrence found";
}
--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/4fd6b5bd-f2af-4e8a-9259-6e6aafc6e173%40isocpp.org.
------=_Part_2528_2095031283.1552385157157
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Hello,<div><br></div><div>I've been writing some code =
to process vectors of classes by using for and for_each loops. Instead of h=
aving long checks in the loop definition and/or the body section of a loop,=
I was thinking of a kind of loop that only jumps into its body, when the c=
ondition is true.<br><br></div><div><br></div><div>normal:</div><div><br></=
div><div><font face=3D"courier new, monospace">when(def; con; amount; iter)=
<br>{}</font></div><div><br></div><div>def: value definition</div><div>con:=
condition when the body is to be executed</div><div>amount: loop is execut=
ed until n true conditions have occurred. if amount =3D 0, then execute the=
loop until it hits break</div><div>ite: iterator</div><div><br></div><div>=
Example: jumps into the body part once the condition is true<br><font face=
=3D"courier new, monospace">when(int i =3D 0; vectorList[i].get_pid() =3D=
=3D myPid; 3; i++)</font></div><div><font face=3D"courier new, monospace">{=
</font></div><div><font face=3D"courier new, monospace">=C2=A0 =C2=A0 cout =
<< "occurrence found at: " << i;</font></div><div><fo=
nt face=3D"courier new, monospace">}</font></div><div><br></div><div><br></=
div><div><br></div><div>for each of vector/array:</div><div><br></div><div>=
<font face=3D"courier new, monospace">when_each(vectorList.begin().get_pid(=
) =3D=3D myPid)</font></div><div><font face=3D"courier new, monospace">{</f=
ont></div><div><font face=3D"courier new, monospace">=C2=A0 =C2=A0 cout <=
;< "occurrence found";</font></div><div><font face=3D"courier =
new, monospace">}</font></div></div>
<p></p>
-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/4fd6b5bd-f2af-4e8a-9259-6e6aafc6e173%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/4fd6b5bd-f2af-4e8a-9259-6e6aafc6e173=
%40isocpp.org</a>.<br />
------=_Part_2528_2095031283.1552385157157--
------=_Part_2527_1621137401.1552385157157--
.
Author: Magnus Fromreide <magfr@lysator.liu.se>
Date: Wed, 13 Mar 2019 19:23:43 +0100
Raw View
On Tue, Mar 12, 2019 at 03:05:57AM -0700, Sophie Schmidt wrote:
> Hello,
>
> I've been writing some code to process vectors of classes by using for and
> for_each loops. Instead of having long checks in the loop definition and/or
> the body section of a loop, I was thinking of a kind of loop that only
> jumps into its body, when the condition is true.
>
>
> normal:
>
> when(def; con; amount; iter)
> {}
>
> def: value definition
> con: condition when the body is to be executed
> amount: loop is executed until n true conditions have occurred. if amount =
> 0, then execute the loop until it hits break
> ite: iterator
>
> Example: jumps into the body part once the condition is true
> when(int i = 0; vectorList[i].get_pid() == myPid; 3; i++)
> {
> cout << "occurrence found at: " << i;
> }
>
Could you please express your idea using current C++.
when (def; con; amount; iter)
statement;
is a simplification, it is equivalent to what?
Remember that all of for, while, do-while and so on can be written using
goto (or while) so this is not an academic question but a way to find bugs
in your proposal.
One example of a bug is that you say nothing about what the purpose of
iter is.
/MF
--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/20190313182343.6xvqvaevvkr4vtcf%40fukushima.lysator.liu.se.
.
Author: Jake Arkinstall <jake.arkinstall@gmail.com>
Date: Wed, 13 Mar 2019 19:03:20 +0000
Raw View
--0000000000000314ae0583fe76a1
Content-Type: text/plain; charset="UTF-8"
Hi Sophie,
With Ranges being accepted into C++20, we will effectively have this:
https://en.cppreference.com/w/cpp/ranges
It's more verbose (you're explicitly piping the container through a filter
and iterating over the values that pass through it), but it's also
incredibly powerful and makes more complex loops much easier to work with.
I think it will be a long time before we see much work in the direction of
loop helpers that don't involve Ranges.
So what are your options if you want this functionality now?
One thing that you can do is use std::find (or std::find_if) for
initialising the iterator, and for incrementing it. You can then either
have that in a for loop (though it would be ugly, but see below) or define
a set of iterators that increment in a custom way, provide the begin and
end functions, and loop through it in a range based for. The downside is
that these iterators need to carry a reference to the container to be able
to know where container.end() is (otherwise the last matching value will
keep trying to find the next match, far beyond the bounds of the
container). Or you can do as many others before you have done and use a
macro.
#define when(variable, container, condition) \
for( \
auto variable = std::find_if(container.begin(), container.end(),
condition); \
variable != container.end(); \
variable = std::find_if(variable + 1, container.end(), condition) \
)
usage:
#include<algorithm>
#include<vector>
#include<iostream>
int main() {
std::vector<int> test{1,2,3,4,5,6,7,8,9};
when(it, test, [](int const& x){ return (x % 2 == 1)){
std::cout << *it << '\n';
}
}
But all of this is inflexible and doesn't give you the option of using it
as a view for something else to use. For that you either want to write your
own view over the container, or wait for C++20.
Theres also a trick you can use that's already in the language. It doesn't
require all that silly messing around with iterators or std algorithms
either.
for(int const& x : test) if(x % 2 == 1){
std::cout << x << '\n';
}
On Tue, 12 Mar 2019, 10:06 Sophie Schmidt, <cosmocortney@outlook.de> wrote:
> Hello,
>
> I've been writing some code to process vectors of classes by using for and
> for_each loops. Instead of having long checks in the loop definition and/or
> the body section of a loop, I was thinking of a kind of loop that only
> jumps into its body, when the condition is true.
>
>
> normal:
>
> when(def; con; amount; iter)
> {}
>
> def: value definition
> con: condition when the body is to be executed
> amount: loop is executed until n true conditions have occurred. if amount
> = 0, then execute the loop until it hits break
> ite: iterator
>
> Example: jumps into the body part once the condition is true
> when(int i = 0; vectorList[i].get_pid() == myPid; 3; i++)
> {
> cout << "occurrence found at: " << i;
> }
>
>
>
> for each of vector/array:
>
> when_each(vectorList.begin().get_pid() == myPid)
> {
> cout << "occurrence found";
> }
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/4fd6b5bd-f2af-4e8a-9259-6e6aafc6e173%40isocpp.org
> <https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/4fd6b5bd-f2af-4e8a-9259-6e6aafc6e173%40isocpp.org?utm_medium=email&utm_source=footer>
> .
>
--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CCPQ1XfpWBDK9j6Wj95X-M%2BegC1zUMHFxCK-8x2Xxw22OQ%40mail.gmail.com.
--0000000000000314ae0583fe76a1
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"auto">Hi Sophie,<div dir=3D"auto"><br></div><div dir=3D"auto">W=
ith Ranges being accepted into C++20, we will effectively have this: <a hre=
f=3D"https://en.cppreference.com/w/cpp/ranges" target=3D"_blank" rel=3D"nor=
eferrer">https://en.cppreference.com/w/cpp/ranges</a></div><div dir=3D"auto=
"><br></div><div dir=3D"auto">It's more verbose (you're explicitly =
piping the container through a filter and iterating over the values that pa=
ss through it), but it's also incredibly powerful and makes more comple=
x loops much easier to work with. I think it will be a long time before we =
see much work in the direction of loop helpers that don't involve Range=
s.</div><div dir=3D"auto"><br></div><div dir=3D"auto">So what are your opti=
ons if you want this functionality now?</div><div dir=3D"auto"><br></div><d=
iv dir=3D"auto">One thing that you can do is use std::find (or std::find_if=
) for initialising the iterator, and for incrementing it. You can then eith=
er have that in a for loop (though it would be ugly, but see below) or defi=
ne a set of iterators that increment in a custom way, provide the begin and=
end functions, and loop through it in a range based for. The downside is t=
hat these iterators need to carry a reference to the container to be able t=
o know where container.end() is (otherwise the last matching value will kee=
p trying to find the next match, far beyond the bounds of the container). O=
r you can do as many others before you have done and use a macro.</div><div=
dir=3D"auto"><br></div><div dir=3D"auto">#define when(variable, container,=
condition) \</div><div dir=3D"auto">=C2=A0 =C2=A0 for( \</div><div dir=3D"=
auto">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0auto variable =3D std::find_if(cont=
ainer.begin(), container.end(), condition); \</div><div dir=3D"auto">=C2=A0=
=C2=A0 =C2=A0 =C2=A0 =C2=A0variable !=3D container.end(); \</div><div dir=
=3D"auto">=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0variable =3D std::find_if(varia=
ble + 1, container.end(), condition) \</div><div dir=3D"auto">=C2=A0 =C2=A0=
)</div><div dir=3D"auto"><br></div><div dir=3D"auto">usage:</div><div dir=
=3D"auto"><br></div><div dir=3D"auto">#include<algorithm></div><div d=
ir=3D"auto">#include<vector></div><div dir=3D"auto">#include<iostr=
eam></div><div dir=3D"auto">int main() {</div><div dir=3D"auto">=C2=A0 =
=C2=A0 std::vector<int> test{1,2,3,4,5,6,7,8,9};</div><div dir=3D"aut=
o">=C2=A0 =C2=A0 when(it, test, [](int const& x){ return (x % 2 =3D=3D =
1)){</div><div dir=3D"auto">=C2=A0 =C2=A0 =C2=A0 =C2=A0 std::cout << =
*it << '\n';</div><div dir=3D"auto">=C2=A0 =C2=A0 }</div><div=
dir=3D"auto">}</div><div dir=3D"auto"><br></div><div dir=3D"auto">But all =
of this is inflexible and doesn't give you the option of using it as a =
view for something else to use. For that you either want to write your own =
view over the container, or wait for C++20.</div><div dir=3D"auto"><br></di=
v><div dir=3D"auto"><br></div><div dir=3D"auto">Theres also a trick you can=
use that's already in the language. It doesn't require all that si=
lly messing around with iterators or std algorithms either.</div><div dir=
=3D"auto"><br></div><div dir=3D"auto">for(int const& x : test) if(x % 2=
=3D=3D 1){</div><div dir=3D"auto">=C2=A0 =C2=A0 std::cout << x <&=
lt; '\n';</div><div dir=3D"auto">}</div><br><div class=3D"gmail_quo=
te" dir=3D"auto"><div dir=3D"ltr" class=3D"gmail_attr">On Tue, 12 Mar 2019,=
10:06 Sophie Schmidt, <<a href=3D"mailto:cosmocortney@outlook.de" rel=
=3D"noreferrer noreferrer noreferrer" target=3D"_blank">cosmocortney@outloo=
k.de</a>> wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"mar=
gin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr=
">Hello,<div><br></div><div>I've been writing some code to process vect=
ors of classes by using for and for_each loops. Instead of having long chec=
ks in the loop definition and/or the body section of a loop, I was thinking=
of a kind of loop that only jumps into its body, when the condition is tru=
e.<br><br></div><div><br></div><div>normal:</div><div><br></div><div><font =
face=3D"courier new, monospace">when(def; con; amount; iter)<br>{}</font></=
div><div><br></div><div>def: value definition</div><div>con: condition when=
the body is to be executed</div><div>amount: loop is executed until n true=
conditions have occurred. if amount =3D 0, then execute the loop until it =
hits break</div><div>ite: iterator</div><div><br></div><div>Example: jumps =
into the body part once the condition is true<br><font face=3D"courier new,=
monospace">when(int i =3D 0; vectorList[i].get_pid() =3D=3D myPid; 3; i++)=
</font></div><div><font face=3D"courier new, monospace">{</font></div><div>=
<font face=3D"courier new, monospace">=C2=A0 =C2=A0 cout << "occ=
urrence found at: " << i;</font></div><div><font face=3D"courier=
new, monospace">}</font></div><div><br></div><div><br></div><div><br></div=
><div>for each of vector/array:</div><div><br></div><div><font face=3D"cour=
ier new, monospace">when_each(vectorList.begin().get_pid() =3D=3D myPid)</f=
ont></div><div><font face=3D"courier new, monospace">{</font></div><div><fo=
nt face=3D"courier new, monospace">=C2=A0 =C2=A0 cout << "occurr=
ence found";</font></div><div><font face=3D"courier new, monospace">}<=
/font></div></div>
<p></p>
-- <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" rel=3D"nore=
ferrer noreferrer noreferrer noreferrer" target=3D"_blank">std-proposals+un=
subscribe@isocpp.org</a>.<br>
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org" rel=3D"noreferrer noreferrer noreferrer noreferrer" target=3D"_blank"=
>std-proposals@isocpp.org</a>.<br>
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/4fd6b5bd-f2af-4e8a-9259-6e6aafc6e173%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter" rel=3D"noreferrer =
noreferrer noreferrer noreferrer" target=3D"_blank">https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/4fd6b5bd-f2af-4e8a-9259-6e6aafc6e173%=
40isocpp.org</a>.<br>
</blockquote></div>
</div>
<p></p>
-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CCPQ1XfpWBDK9j6Wj95X-M%2BegC1z=
UMHFxCK-8x2Xxw22OQ%40mail.gmail.com?utm_medium=3Demail&utm_source=3Dfooter"=
>https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/CAC%2B0CCPQ1X=
fpWBDK9j6Wj95X-M%2BegC1zUMHFxCK-8x2Xxw22OQ%40mail.gmail.com</a>.<br />
--0000000000000314ae0583fe76a1--
.
Author: Cleiton Santoia <cleitonsantoia@gmail.com>
Date: Wed, 13 Mar 2019 12:05:30 -0700 (PDT)
Raw View
------=_Part_15_1091910237.1552503930338
Content-Type: multipart/alternative;
boundary="----=_Part_16_615421289.1552503930338"
------=_Part_16_615421289.1552503930338
Content-Type: text/plain; charset="UTF-8"
1 Another algorithm "std::for_each_if" with an extra parameter for the
condition ?
2 range::view ?
--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9b1fb9b7-020f-46ff-b8b1-7181807d5059%40isocpp.org.
------=_Part_16_615421289.1552503930338
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><br></div><div>1 Another algorithm "std::for_eac=
h_if" with an extra parameter for the condition ?</div><div><br></div>=
<div>2 range::view ?</div></div>
<p></p>
-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/9b1fb9b7-020f-46ff-b8b1-7181807d5059%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/9b1fb9b7-020f-46ff-b8b1-7181807d5059=
%40isocpp.org</a>.<br />
------=_Part_16_615421289.1552503930338--
------=_Part_15_1091910237.1552503930338--
.