Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 14:00 Go to next message
MaximeLecourt  is currently offline MaximeLecourt Friend
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 #527566 is a reply to message #527519] Thu, 15 April 2010 15:51 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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]



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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 07:17 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1823
Registered: July 2009
Senior Member
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 07:55 Go to previous messageGo to next message
MaximeLecourt  is currently offline MaximeLecourt Friend
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 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
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
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 07:29 Go to previous message
MaximeLecourt  is currently offline MaximeLecourt Friend
Messages: 108
Registered: February 2010
Location: France
Senior Member
It was my first time filing a bugzilla, I hope I filled it right.

https://bugs.eclipse.org/bugs/show_bug.cgi?id=309627


One day I shall master M2T, but that day has yet to come...
Previous Topic:XPAND FILE directive - is it possible to write to an InputStream
Next Topic:XPand for modifying arbitrary textfiles!
Goto Forum:
  


Current Time: Thu Apr 25 11:00:45 GMT 2024

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

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

Back to the top