Skip to content
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

So what the hell is gather/take #2388

Open
AlexDaniel opened this issue Oct 17, 2018 · 4 comments
Open

So what the hell is gather/take #2388

AlexDaniel opened this issue Oct 17, 2018 · 4 comments
Labels
docs Documentation issue (primary issue type) RFE Request for enhancement. Try to use alternative wording that explain the concept more clearly

Comments

@AlexDaniel
Copy link
Member

AlexDaniel commented Oct 17, 2018

Inspired by #2135 which says:

Note: Please feel free to reword the title. I'm just frustrated with how little our docs say about such an important feature of perl 6.

This is another case when an amazing feature is not explained enough.

Docs: https://docs.perl6.org/syntax/gather%20take

It says:

gather/take can generate values lazily, depending on context. If you want to force lazy evaluation use the lazy subroutine or method. Binding to a scalar or sigilless container will also force laziness.

(let's ignore questionable “force laziness” stuff for now)

In some way it is right, but to me this is not enough. Surely it can generate values lazily, but how? What does it mean exactly? What should I write in order to get lazy behavior?

See my mumblings here: http://colabti.org/irclogger/irclogger_log/perl6?date=2018-10-17#l34

If I get it right (please correct me if I'm wrong), then if you have code like this:

say before;
take 42;
say after;
…

Then say ‘after’ will not be executed unless you pull another value from the Seq. So your code just magically stops executing before returning from take. This means that you can write any kind of code, like infinite recursion and the like, and it won't run more than you ask it to. That's freaking awesome.

@AlexDaniel AlexDaniel added the docs Documentation issue (primary issue type) label Oct 17, 2018
@lichtkind
Copy link
Contributor

not only that what i like about that feature you can have normal loop but in it you not take every iteration a value or sometimes two and so press irregular ebehaviour into seemingly regular frame

@zoffixznet
Copy link
Contributor

So your code just magically stops executing before returning from take

I believe the technical term is "continuation"

@AlexDaniel AlexDaniel added the RFE Request for enhancement. Try to use alternative wording that explain the concept more clearly label Jan 25, 2019
@Xliff
Copy link
Collaborator

Xliff commented Oct 7, 2019

Moreover, you are not limited to the type of the value of the original list when forming the results,
consider.

my @a = gather for %results {
   if <some condion> {
      take <do somehing complex with $_ and return the result>
   }
}

Please note that this is different than:

%results.values.grep(<some condition>).map(<complex function>)

Because no mater what you are doing, you are still looping twice, and the data you have in .grep() may no longer be available in .map. Why do in two loops what can be done in one?

I'll look through some of my code and see if I can give a more concrete example.

@FCO
Copy link

FCO commented Oct 7, 2019

But that would be the same as:

%results.values.map: { <complex function> if <some condition> }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation issue (primary issue type) RFE Request for enhancement. Try to use alternative wording that explain the concept more clearly
Projects
None yet
Development

No branches or pull requests

5 participants