Skip to main content



      Home
Home » Modeling » M2T (model-to-text transformation) » [Xtend] apply a function with multiple arguments on all elements of a list
[Xtend] apply a function with multiple arguments on all elements of a list [message #527519] Thu, 15 April 2010 10:00 Go to next message
Eclipse UserFriend
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)?

[Updated on: Thu, 15 April 2010 10:00] by Moderator

Re: [Xtend] apply a function with multiple arguments on all elements of a list [message #527566 is a reply to message #527519] Thu, 15 April 2010 11:51 Go to previous messageGo to next message
Eclipse UserFriend
Hello maxime,

do you know that you can call a xtend function that is defined for lets say Element on a List[Element] too?

here a sample

the extension
String postfix(String in, String postfix) :
	in + postfix;


delivers called like this
{"Person","Vehicle"}.postfix("Type")


the resulting list
[PersonType, VehicleType]

Re: [Xtend] apply a function with multiple arguments on all elements of a list [message #527699 is a reply to message #527566] Fri, 16 April 2010 03:17 Go to previous messageGo to next message
Eclipse UserFriend
Christian Dietrich schrieb:
> Hello maxime,
>
> do you know that you can call a xtend function that is defined for lets
> say Element on a List[Element] too?
>
> here a sample
>
> the extension
>
> String postfix(String in, String postfix) :
> in + postfix;
>
>
> delivers called like this
>
> {"Person","Vehicle"}.postfix("Type")

Which is effectively a short hand for

{'Person','Vehicle'}.collect(e|e.postfix('Type'))

Sven


--
Need professional support for Xtext and EMF?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
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 03:55 Go to previous messageGo to next message
Eclipse UserFriend
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 ?
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 05:57 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: [Xtend] apply a function with multiple arguments on all elements of a list [message #528063 is a reply to message #527519] Mon, 19 April 2010 03:29 Go to previous message
Eclipse UserFriend
It was my first time filing a bugzilla, I hope I filled it right.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=309627
Previous Topic:XPAND FILE directive - is it possible to write to an InputStream
Next Topic:XPand for modifying arbitrary textfiles!
Goto Forum:
  


Current Time: Sun Jul 13 05:00:56 EDT 2025

Powered by FUDForum. Page generated in 1.81632 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top