You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Paul King (Jira)" <ji...@apache.org> on 2025/04/09 02:37:00 UTC
[jira] [Comment Edited] (GROOVY-11606) Lazy findAll, collect, collectMany
[ https://issues.apache.org/jira/browse/GROOVY-11606?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17942063#comment-17942063 ]
Paul King edited comment on GROOVY-11606 at 4/9/25 2:36 AM:
------------------------------------------------------------
For the draft implementation, I have followed the Groovy-stream approach of setting the delegate.
It means we can have code like this \(*):
{code:groovy}
var letters = Iterators.iterate('a', String::next)
var numbers = Iterators.iterate(0, Integer::next)
assert Iterators.combineLazy(true, x:letters, y:numbers)
.collectLazy{ x + y } // <<<===== (*)
.take(8)
.join(' ') == 'a0 a1 b0 b1 a2 b2 c0 c1'
{code}
Instead of this:
{code:groovy}
var letters = Iterators.iterate('a', String::next)
var numbers = Iterators.iterate(0, Integer::next)
assert Iterators.combineLazy(true, x:letters, y:numbers)
.map{ it.x + it.y } // <<<===== (*)
.take(8)
.join(' ') == 'a0 a1 b0 b1 a2 b2 c0 c1'
{code}
Another example:
{code:groovy}
assert [
[a:3, b:4, c:5],
[a:5, b:12, c:13],
[a:8, b:15, c:17],
[a:7, b:24, c:25]
].iterator().collectLazy {
c == Math.sqrt(a ** 2 + b ** 2)
}.join(' ') == 'true true true true'
{code}
An alternative using "with":
{code:groovy}
assert [
[a:3, b:4, c:5],
[a:5, b:12, c:13],
[a:8, b:15, c:17],
[a:7, b:24, c:25]
].iterator().map {
it.with {
c == Math.sqrt(a ** 2 + b ** 2)
}
}.join(' ') == 'true true true true'
{code}
An example with "findAllLazy":
{code:groovy}
assert [
[a:1, b:2, c:3],
[a:5, b:15, c:10],
[a:10, b:20, c:30]
].iterator().findAllLazy {
a + b == c
}.join(' ') == '[a:1, b:2, c:3] [a:10, b:20, c:30]'
{code}
Using "filter":
{code:groovy}
assert [
[a:1, b:2, c:3],
[a:5, b:15, c:10],
[a:10, b:20, c:30]
].iterator().filter {
it.a + it.b == it.c
}.join(' ') == '[a:1, b:2, c:3] [a:10, b:20, c:30]'
{code}
My summary:
* The code when the delegate is set is definitely nicer
* It does have some performance impacts since, so as not to pollute the closure state, we clone it before setting the delegate
* Also, if we do this for some of the methods, folks might expect for consistency, that we do it for all of them (which we could do but we'd have to analyse whether that would break existing code)
was (Author: paulk):
For the draft implementation, I have followed the Groovy-stream approach of setting the delegate.
It means we can have code like this \(*):
{code:groovy}
var letters = Iterators.iterate('a', String::next)
var numbers = Iterators.iterate(0, Integer::next)
assert Iterators.combineLazy(true, x:letters, y:numbers)
.collectLazy{ x + y } // <<<===== (*)
.take(8)
.join(' ') == 'a0 a1 b0 b1 a2 b2 c0 c1'
{code}
Instead of this:
{code:groovy}
var letters = Iterators.iterate('a', String::next)
var numbers = Iterators.iterate(0, Integer::next)
assert Iterators.combineLazy(true, x:letters, y:numbers)
.map{ it.x + it.y } // <<<===== (*)
.take(8)
.join(' ') == 'a0 a1 b0 b1 a2 b2 c0 c1'
{code}
Another example:
{code:groovy}
assert [
[a:3, b:4, c:5],
[a:5, b:12, c:13],
[a:8, b:15, c:17],
[a:7, b:24, c:25]
].iterator().collectLazy {
c == Math.sqrt(a ** 2 + b ** 2)
}.join(' ') == 'true true true true'
{code}
An alternative using "with":
{code:groovy}
assert [
[a:3, b:4, c:5],
[a:5, b:12, c:13],
[a:8, b:15, c:17],
[a:7, b:24, c:25]
].iterator().map {
it.with {
c == Math.sqrt(a ** 2 + b ** 2)
}
}.join(' ') == 'true true true true'
{code}
An example with "findAllLazy":
{code:groovy}
assert [
[a:1, b:2, c:3],
[a:5, b:15, c:10],
[a:10, b:20, c:30]
].iterator().findAllLazy {
a + b == c
}.join(' ') == '[a:1, b:2, c:3] [a:10, b:20, c:30]'
{code}
Using "filter":
{code:groovy}
assert [
[a:1, b:2, c:3],
[a:5, b:15, c:10],
[a:10, b:20, c:30]
].iterator().filter {
it.a + it.b == it.c
}.join(' ') == '[a:1, b:2, c:3] [a:10, b:20, c:30]'
{code}
> Lazy findAll, collect, collectMany
> ----------------------------------
>
> Key: GROOVY-11606
> URL: https://issues.apache.org/jira/browse/GROOVY-11606
> Project: Groovy
> Issue Type: Improvement
> Reporter: Paul King
> Assignee: Paul King
> Priority: Major
>
> See:
> https://lists.apache.org/thread/xv1bpgc7xp3rwp7qt627xfyd10ljcwbc
--
This message was sent by Atlassian Jira
(v8.20.10#820010)