[Xtend] apply a function with multiple arguments on all elements of a list [message #527519] |
Thu, 15 April 2010 14:00 |
MaximeLecourt Messages: 108 Registered: February 2010 Location: France |
Senior Member |
|
|
Having a list and another argument, I want to modify the elements of that list calling an extension, but that extension has other arguments.
I can do it using a recursive Xtend extension which would (kind of) look like that
List[Type] function(List[Type] l,Property p) :
if l.isEmpty then {}
else
whateverTreatment(l.first(),p).addAll(function(l.withoutfirst(),p));
Type whateverTreatment(Type t, Property p)
Is there a way to do it without the recursion (and without create extensions)?
One day I shall master M2T, but that day has yet to come...
[Updated on: Thu, 15 April 2010 14:00] Report message to a moderator
|
|
|
|
|
Re: [Xtend] apply a function with multiple arguments on all elements of a list [message #527712 is a reply to message #527519] |
Fri, 16 April 2010 07:55 |
MaximeLecourt Messages: 108 Registered: February 2010 Location: France |
Senior Member |
|
|
Thanks
The eclipse documentation states, about the collect extension :
Quote: | collect
As shown in the previous section, the select and reject operations always result in a sub-collection of the original collection. Sometimes one wants to specify a collection which is derived from another collection, but which contains objects that are not in the original collection (it is not a sub-collection). In such cases, we can use a collect operation. The collect operation uses the same syntax as the select and reject and is written like this:
collection.collect(v | expression-with-v)
collect again iterates over the target collection and evaluates the given expression on each element. In contrast to select, the evaluation result is collected in a list. When an iteration is finished the list with all results is returned. Example:
namedElements.collect(ne | ne.name) // returns a list of strings
|
Reading this, I did not really understand all that this extension could be used for, so I'd like to suggest a modification of this paragraph :
Quote: | collect
As shown in the previous section, the select and reject operations always result in a sub-collection of the original collection. Sometimes one wants to apply an operation on all elements of the collection. In such cases, we can use a collect operation. In contrast to select (and reject), the evaluation result is collected in a list. The collect operation uses the same syntax as the select and reject and is written like this:
collection.collect(v | expression-with-v)
collect again iterates over the target collection and evaluates the given expression on each element. When an iteration is finished the list with all results is returned. Example:
namedElements.collect(ne | ne.name) // returns a list of strings
|
What are your thoughts about this ?
One day I shall master M2T, but that day has yet to come...
|
|
|
Re: [Xtend] apply a function with multiple arguments on all elements of a list [message #527735 is a reply to message #527712] |
Fri, 16 April 2010 09:57 |
Sven Efftinge Messages: 1823 Registered: July 2009 |
Senior Member |
|
|
Looks good. Please file a bugzilla.
Sven
Maxime Lecourt schrieb:
> Thanks
>
> The eclipse documentation states, about the collect extension :
> Quote:
>> collect
>>
>> As shown in the previous section, the select and reject operations
>> always result in a sub-collection of the original collection.
>> Sometimes one wants to specify a collection which is derived from
>> another collection, but which contains objects that are not in the
>> original collection (it is not a sub-collection). In such cases, we
>> can use a collect operation. The collect operation uses the same
>> syntax as the select and reject and is written like this:
>>
>> collection.collect(v | expression-with-v)
>>
>> collect again iterates over the target collection and evaluates the
>> given expression on each element. In contrast to select, the
>> evaluation result is collected in a list. When an iteration is
>> finished the list with all results is returned. Example:
>>
>> namedElements.collect(ne | ne.name) // returns a list of strings
>
>
>
> Reading this, I did not really understand all that this extension could
> be used for, so I'd like to suggest a modification of this paragraph :
>
> Quote:
>> collect
>>
>> As shown in the previous section, the select and reject operations
>> always result in a sub-collection of the original collection.
>> Sometimes one wants to apply an operation on all elements of the
>> collection. In such cases, we can use a collect operation. In contrast
>> to select (and reject), the evaluation result is collected in a list.
>> The collect operation uses the same syntax as the select and reject
>> and is written like this:
>>
>> collection.collect(v | expression-with-v)
>>
>> collect again iterates over the target collection and evaluates the
>> given expression on each element. When an iteration is finished the
>> list with all results is returned. Example:
>>
>> namedElements.collect(ne | ne.name) // returns a list of strings
>
>
> What are your thoughts about this ?
--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.03162 seconds