You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2022/02/08 20:03:00 UTC
[jira] [Comment Edited] (GROOVY-10473) Extend streams support to spread and subscript
[ https://issues.apache.org/jira/browse/GROOVY-10473?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17489094#comment-17489094 ]
Eric Milles edited comment on GROOVY-10473 at 2/8/22, 8:02 PM:
---------------------------------------------------------------
The spread-dot syntax is supported for dynamic groovy because {{Stream}} provides an "iterator" method and ScriptBytecodeAdapter does this:
{code:java}
public static Object getPropertySpreadSafe(Class senderClass, Object receiver, String messageName) throws Throwable {
if (receiver == null) return null;
List answer = new ArrayList();
for (Iterator it = InvokerHelper.asIterator(receiver); it.hasNext();) {
answer.add(getPropertySafe(senderClass, it.next(), messageName));
}
return answer;
}
// or
public static Object invokeMethodNSpreadSafe(Class senderClass, Object receiver, String messageName, Object[] messageArguments) throws Throwable {
if (receiver == null) return null;
List answer = new ArrayList();
for (Iterator it = InvokerHelper.asIterator(receiver); it.hasNext();) {
answer.add(invokeMethodNSafe(senderClass, it.next(), messageName, messageArguments));
}
return answer;
}
{code}
For static compilation, see GROOVY-8133
was (Author: emilles):
The spread-dot syntax is supported for dynamic groovy because {{Stream}} provides an "iterator" method and ScriptBytecodeAdapter does this:
{code:java}
public static Object invokeMethodNSpreadSafe(Class senderClass, Object receiver, String messageName, Object[] messageArguments) throws Throwable {
if (receiver == null) return null;
List answer = new ArrayList();
for (Iterator it = InvokerHelper.asIterator(receiver); it.hasNext();) {
answer.add(invokeMethodNSafe(senderClass, it.next(), messageName, messageArguments));
}
return answer;
}
{code}
For static compilation, see GROOVY-8133
> Extend streams support to spread and subscript
> ----------------------------------------------
>
> Key: GROOVY-10473
> URL: https://issues.apache.org/jira/browse/GROOVY-10473
> Project: Groovy
> Issue Type: Improvement
> Reporter: Eric Milles
> Priority: Major
>
> Groovy 3 and 4 added some {{Stream}} extension methods like {{plus}}, {{toList}} and {{toArray}}. And some default type conversions like {{String[] array = stream;}}. This could be taken further by having spread-dot and subscript support:
> {code:groovy}
> stream*.thing // --> stream.map(it -> it.thing).toList()
> stream[1] // --> stream.skip(1).findFirst().orElse(null)
> stream[1..10] // --> stream.skip(1).limit(10-1).toList()
> // consider negative indexes and reverse ranges
> {code}
> Also, {{forEachPair}} and {{zip}} extension methods would be useful for processing corresponding streams.
--
This message was sent by Atlassian Jira
(v8.20.1#820001)