New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Explain more on benefits/use-case of using is pure trait
#1231
Comments
|
Can somebody review what I just did? Does it resolve the issue? |
|
Yep, makes it very clear. |
explain why the compiler cannot reliably infer purity on its own. For issue #1231.
|
I don't pretend to reopen this issue, and what I'm about to say is not
specific to `is pure`.
Wouldn't it be better to look for actual use cases and insert them as
examples? While multiplying input by 2 is syntactically correct, it's
relatively unlikely to meet it in a real life situation; you will simply
multiply by 2 without needing to create a function. Not that examples
elsewhere are better:
https://medium.com/@jamesjefferyuk/javascript-what-are-pure-functions-4d4d5392d49c
but this
http://www.deadcoderising.com/2017-06-13-why-pure-functions-4-benefits-to-embrace-2/
for instance mentions composition, laziness, both of them having support in
perl 6, and maybe they could be mentioned here in this example.
Since I do have write permission here, would you like me to try something
here along these lines? Just reopen the issue and assign it to me if you
want.
|
|
@JJ Perl 6 can have composition, laziness and concurrency on normal functions just fine. The trait is used to change their performance characteristics, moving a runtime calculation (which could be cached at runtime too: https://docs.perl6.org/routine/is%20cached) into compilation time. |
|
@JJ feel free to provide a better example |
|
2017-09-03 10:24 GMT+02:00 rafaelschipiura <notifications@github.com>:
@JJ <https://github.com/jj> Perl 6 can have composition, laziness and
concurrency on normal functions just fine. The trait is used to change
their performance characteristics, moving a runtime calculation (which
could be cached at runtime too: https://docs.perl6.org/routine/is%20cached)
into compilation time.
I didn't mean those were *exclusive* traits of pure functions. My point was
that it would be interesting to provide meaningful use cases by providing
examples that fall in them.
|
|
@JJ I agree your idea is good, I was just trying to help find a good example. |
|
2017-09-03 10:28 GMT+02:00 rafaelschipiura <notifications@github.com>:
@JJ <https://github.com/jj> I agree your idea is good, I was just trying
to help find a good example.
Thanks. In fact, caching/memoization is another use case of pure routines.
I'll try to find examples for all of them, maybe run some measurements too.
|
|
@JJ I just want you to note that the very example provided in the documentation has side effects and Perl6 has no problem with it, OK? |
|
I've come with a nice use case that includes pre-computing a set of things; however it involves using experimental macros. Would this be suitable? Or is it better if I think about a different one? |
No. |
👎 even if they weren't experimental. There's no need to daunt the user with a whole 'nother large language feature they'd have to be familiar with just to explain
|
|
Correct me if I'm wrong, but only if you call them with constants they will
be evaluated in compile time. You can obviously call them with a variable,
but their value will not be available in compile time, so they will act as
"normal" variables.
That's what I mean when I say a "real" use case. It could be a difficult
computation performed in compile time, for instance, or pre-computing some
calls. That is also why I say we would need a macro to have an use case as
real as possible.
I was thinking, for instance, about a problem we often find in evolutionary
algorithms, royal road. It's computed with 4 bits, you can pretty much put
it in a hash or else write a macro that generates its 16 cases, which then
use the "is pure". Programatically it will not look like a sequence of 16
function calls, but a loop, and the 16 cases will be actually computed in
compile time.
|
|
For instance, this thing: four-bits() is called at compile time. However, royal-road is only called at compile time for the "1010" call. A macro could generate the code instead of the map. |
https://docs.perl6.org/routine/is%20pure
Even after reading about what constant-folding is, it's not clear why someone would need to explicitly mark this when the compiler can probably infer it on its own.
Is there a difference between using
is pureon a sub that has a return value vs on a sub that doesn't have a return value?The text was updated successfully, but these errors were encountered: