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

[Fun] Add lazy() for doing lazy evaluations #215

Merged
merged 2 commits into from
Jul 10, 2021
Merged

Conversation

veewee
Copy link
Collaborator

@veewee veewee commented Jun 30, 2021

This Fun\lazy() helper can be used for doing lazy evaluations:
https://en.wikipedia.org/wiki/Lazy_evaluation

Examples:

Lazy proxy:

$soap = Fun\lazy(fn () => new \SoapClient('http://slowwsdl.com'));
$functions = $soap()->__getFunctions();

Lazy evaluations:

$a = Fun\lazy(fn(): int => 1);
for ($i = 1; $i<10; $i++) {
    $b = $a;
    $a = Fun\lazy(fn() => $b() + $b());
}

$aResult = $a();
// = 2**10 -> all intermediate values are calculated lazily when calling the $a() callable.

Lazy streams

$incrementalNumbersStream = Fun\lazy(function() {
    $i = 0;
    while (true) {
        yield ++$i;
    }
});

take(10, $incrementalNumbersStream()); // [1...10]
take(20, $incrementalNumbersStream()); // [11...30]

@coveralls
Copy link

coveralls commented Jun 30, 2021

Pull Request Test Coverage Report for Build 1017034780

  • 8 of 8 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.0002%) to 99.935%

Totals Coverage Status
Change from base Build 1017024431: 0.0002%
Covered Lines: 3072
Relevant Lines: 3074

💛 - Coveralls

@azjezz azjezz added this to the 1.8.0 milestone Jul 1, 2021
@azjezz azjezz added Priority: Medium This issue may be useful, and needs some attention. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Type: Enhancement Most issues will probably ask for additions or changes. labels Jul 1, 2021
@azjezz azjezz merged commit 87e0a73 into azjezz:1.8.x Jul 10, 2021
@azjezz
Copy link
Owner

azjezz commented Jul 10, 2021

Thanks @veewee 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: Medium This issue may be useful, and needs some attention. Status: Accepted It's clear what the subject of the issue is about, and what the resolution should be. Type: Enhancement Most issues will probably ask for additions or changes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants