Topic: Implementation of N4244 Resumable Lambdas


Author: chris.kohlhoff@gmail.com
Date: Tue, 28 Oct 2014 06:31:45 -0700 (PDT)
Raw View
------=_Part_4097_335073156.1414503105274
Content-Type: text/plain; charset=UTF-8

Hi everyone,

I have created an implementation of N4244 resumable lambdas as a C++ to C++
preprocessor. You can find out more about it on GitHub here:

http://chriskohlhoff.github.io/resumable-pp/

Please note that there are some differences from N4244 due to the
limitations of not being part of a real compiler. These are documented at
the above site. There is also a link to the examples and an online form to
run the preprocessor for those who don't want to build it from source.

Here's one simple example (countdown4.cpp):

#include <stdio.h>

auto countdown(int n)
{
  return [=]() resumable
  {
    while (--n > 0)
      if (n == 1) return n;
      else yield n;
  };
}

int main()
{
  auto f = [&]() resumable
  {
    yield from countdown(10);
    return from countdown(5);
  };

  while (!is_terminal(f))
    printf("%d\n", f());
}

Though I haven't put in much effort to optimise the implementation,
performance is good because the compiler is able to see right into the
lambda implementation. Running this:

auto f = countdown(1000000000);
while (!f.is_terminal())
  f();

takes just 0.4 seconds to complete, or 0.4ns per iteration, as the compiler
has optimised everything away bar the test, increment and jump.

Cheers,
Chris

--

---
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_4097_335073156.1414503105274
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hi everyone,<div><br></div><div>I have created an implemen=
tation of N4244 resumable lambdas as a C++ to C++ preprocessor. You can fin=
d out more about it on GitHub here:</div><div><br></div><div><a href=3D"htt=
p://chriskohlhoff.github.io/resumable-pp/">http://chriskohlhoff.github.io/r=
esumable-pp/</a><br></div><div><br></div><div>Please note that there are so=
me differences from N4244 due to the limitations of not being part of a rea=
l compiler. These are documented at the above site. There is also a link to=
 the examples and an online form to run the preprocessor for those who don'=
t want to build it from source.</div><br>Here's one simple example (countdo=
wn4.cpp):<br><br>#include &lt;stdio.h&gt;<br><br>auto countdown(int n)<br>{=
<br>&nbsp; return [=3D]() resumable<br>&nbsp; {<br>&nbsp; &nbsp; while (--n=
 &gt; 0)<br>&nbsp; &nbsp; &nbsp; if (n =3D=3D 1) return n;<br>&nbsp; &nbsp;=
 &nbsp; else yield n;<br>&nbsp; };  <br>} <br>  <br>int main()<br>{   <br>&=
nbsp; auto f =3D [&amp;]() resumable<br>&nbsp; {   <br>&nbsp; &nbsp; yield =
from countdown(10);<br>&nbsp; &nbsp; return from countdown(5);<br>&nbsp; };=
<br>    <br>&nbsp; while (!is_terminal(f))<br>&nbsp; &nbsp; printf("%d\n", =
f());<br>} <br><br>Though I haven't put in much effort to optimise the impl=
ementation, performance is good because the compiler is able to see right i=
nto the lambda implementation. Running this:<br><br>auto f =3D countdown(10=
00000000);<br>while (!f.is_terminal())<br>&nbsp; f();<br><br>takes just 0.4=
 seconds to complete, or 0.4ns per iteration, as the compiler has optimised=
 everything away bar the test, increment and jump.<div><div><br></div><div>=
Cheers,</div><div>Chris</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 />

------=_Part_4097_335073156.1414503105274--

.