Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » M2T (model-to-text transformation) » Max / Min Operation?
Max / Min Operation? [message #640087] Fri, 19 November 2010 02:03 Go to next message
Miles Parker is currently offline Miles ParkerFriend
Messages: 1340
Registered: July 2009
Senior Member
I'm wondering if anyone has thought about a good approach to finding the element with minimum or maximum value of a given expression from within a collection? The only way I can think of is to do a sort():

myMeasure.sortBy(e | e.value).last().value;

..but of course that is doing way more work than necessary. All we need to be able to do is traverse the whole list once. But there isn't anyway to communicate state between evaluations of collection operators. Perhaps someone has a more clever way of handling this? I thought about doing something lispish like dividing tails and heads and doing the compare that way but that can lead to lispish size stacks as well..
Re: Max / Min Operation? [message #640116 is a reply to message #640087] Fri, 19 November 2010 07:47 Go to previous messageGo to next message
Laurent Goubet is currently offline Laurent GoubetFriend
Messages: 1639
Registered: July 2009
Senior Member
This is a multi-part message in MIME format.
--------------080504080304090703000907
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit

Hi,

I believe that "iterate" is the closest you'll find. If "myMeasure" is
your collection of numbers :

myMeasure->iterate(current; result : Integer = myMeasure->first() |
result.min(current))

There may be better ways, but this is what I thought of first :p

Laurent Goubet
Obeo

On 19/11/2010 03:03, Miles Parker wrote:
> I'm wondering if anyone has thought about a good approach to finding the
> element with minimum or maximum value of a given expression from within
> a collection? The only way I can think of is to do a sort():
>
> myMeasure.sortBy(e | e.value).last().value;
>
> .but of course that is doing way more work than necessary. All we need
> to be able to do is traverse the whole list once. But there isn't anyway
> to communicate state between evaluations of collection operators.
> Perhaps someone has a more clever way of handling this? I thought about
> doing something lispish like dividing tails and heads and doing the
> compare that way but that can lead to lispish size stacks as well..


--------------080504080304090703000907
Content-Type: text/x-vcard; charset=utf-8;
name="laurent_goubet.vcf"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
filename="laurent_goubet.vcf"

YmVnaW46dmNhcmQNCmZuOkxhdXJlbnQgR291YmV0DQpuOkdvdWJldDtMYXVy ZW50DQpvcmc6
PGEgaHJlZj0iaHR0cDovL3d3dy5vYmVvLmZyIj5PYmVvPC9hPg0KZW1haWw7 aW50ZXJuZXQ6
bGF1cmVudC5nb3ViZXRAb2Jlby5mcg0KdXJsOmh0dHA6Ly93d3cub2Jlby5m cg0KdmVyc2lv
bjoyLjENCmVuZDp2Y2FyZA0KDQo=
--------------080504080304090703000907--
Re: Max / Min Operation? [message #640259 is a reply to message #640116] Fri, 19 November 2010 18:51 Go to previous messageGo to next message
Miles Parker is currently offline Miles ParkerFriend
Messages: 1340
Registered: July 2009
Senior Member
[quote title=Laurent Goubet wrote on Fri, 19 November 2010 02:47]
I believe that "iterate" is the closest you'll find.[quote]

Thanks Laurent. That is exactly the general functionality I'm looking for. I don't think it is in current bundled Helios docs so I didn't even know it existed.
Re: Max / Min Operation? [message #640322 is a reply to message #640116] Sat, 20 November 2010 13:35 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 4185
Registered: July 2009
Senior Member
Hi Miles

A very sensible suggestion.

I've raised an OMG issue suggesting a max and a min iteration analoguous
to isUnique. It would of course be specified using iterate.

MDT/OCL 3.1 Examples makes the OCL Standard Library model-driven so
adding this as a user extension will be easy.

For now try something like (using an existing iteration)

let measures : Sequence(Measure) = myMeasure->asSequence(),
values : Sequence(Real) = measures ->select(value),
maxValue : Real = values->max() in
measures->at(values->indexOf(maxValue))

or

myMeasure->iterate(current; result : Measure = myMeasure->any(true) |
result.value.min(current.value))

(not tested).

Regards

Ed Willink


On 19/11/2010 07:47, Laurent Goubet wrote:
> Hi,
>
> I believe that "iterate" is the closest you'll find. If "myMeasure" is
> your collection of numbers :
>
> myMeasure->iterate(current; result : Integer = myMeasure->first() |
> result.min(current))
>
> There may be better ways, but this is what I thought of first :p
>
> Laurent Goubet
> Obeo
>
> On 19/11/2010 03:03, Miles Parker wrote:
>> I'm wondering if anyone has thought about a good approach to finding the
>> element with minimum or maximum value of a given expression from within
>> a collection? The only way I can think of is to do a sort():
>>
>> myMeasure.sortBy(e | e.value).last().value;
>>
>> .but of course that is doing way more work than necessary. All we need
>> to be able to do is traverse the whole list once. But there isn't anyway
>> to communicate state between evaluations of collection operators.
>> Perhaps someone has a more clever way of handling this? I thought about
>> doing something lispish like dividing tails and heads and doing the
>> compare that way but that can lead to lispish size stacks as well..
>
Re: Max / Min Operation? [message #641077 is a reply to message #640087] Wed, 24 November 2010 08:15 Go to previous messageGo to next message
Sven Efftinge is currently offline Sven EfftingeFriend
Messages: 1771
Registered: July 2009
Senior Member
Am 11/19/10 3:03 AM, schrieb Miles Parker:
> I'm wondering if anyone has thought about a good approach to finding the
> element with minimum or maximum value of a given expression from within
> a collection? The only way I can think of is to do a sort():
>
> myMeasure.sortBy(e | e.value).last().value;

That's how I would do it as well.

>
> .but of course that is doing way more work than necessary.

"Way more" means creates a list?
Did you hit a performance issue with that expression?

Sven


--
Need professional support for Xtext or other Eclipse Modeling technologies?
Go to: http://xtext.itemis.com
Twitter : @svenefftinge
Blog : http://blog.efftinge.de


--
Need professional support on Xtext or Xtend?
Mail to: xtext (at) itemis.com
Twitter : @svenefftinge
Blog : blog.efftinge.de
Re: Max / Min Operation? [message #641273 is a reply to message #641077] Wed, 24 November 2010 17:22 Go to previous message
Miles Parker is currently offline Miles ParkerFriend
Messages: 1340
Registered: July 2009
Senior Member
Sven Efftinge wrote on Wed, 24 November 2010 03:15
Am 11/19/10 3:03 AM, schrieb Miles Parker:
> myMeasure.sortBy(e | e.value).last().value;

That's how I would do it as well.
> .but of course that is doing way more work than necessary.

"Way more" means creates a list?
Did you hit a performance issue with that expression?



Well, *way* more is relative. I mean that it is going to be (depending on how you've implemented sort) O (n log n) -> O(n^2) whereas find max is obviously worst case O(n). I have large really lists but I haven't run into issues so at this point it's more curiosity about what can be done with xtend.
Previous Topic:[Acceleo/EMF] Problem in Main.java after refactoring one meta-model into two
Next Topic:[XPand] Supporting @generated style protection
Goto Forum:
  


Current Time: Thu Dec 18 20:16:38 GMT 2014

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

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