[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[RCR?] Using 'when' as a statement modifier

Tom Agnew

12/18/2007 4:32:00 AM

Hello all,

I have a suggestion for an enhancement that could add to the readability
of my programs.

I'm a frequent user of 'if' and 'unless' statement modifiers. It's a
beautiful abbreviation that enhances the readability of my code.

Here is a pattern that I use from time to time:


result = func(x) unless func(x)==y


This has the potential to invoke func(x) twice in some implementations.

It occurred to me that the 'case' semantics could be borrowed to create
a useful new modifier. Here's a suggestion that might be more efficient
and possibly more readable:

result = func(x) when result==y


The modifier would act as a mini-continuation, so that the result would
not be updated (i.e., it would be rolled back) if the condition is not
satisfied.

Then 'when' modifier could invoke the case equality operator by default.
For example:


result = func1(x) when 200..499
result = func2(x) when 500..999


or even:

result = translate(x) when Class1,Class2
result = translate(x) when value1,range2,Class3


Anyone else have a use for this? Any thoughts??


Best regards,
Tom Agnew
5 Answers

MonkeeSage

12/18/2007 11:15:00 PM

0

On Dec 17, 10:31 pm, Tom Agnew <agne...@nospam.net> wrote:
> Hello all,
>
> I have a suggestion for an enhancement that could add to the readability
> of my programs.
>
> I'm a frequent user of 'if' and 'unless' statement modifiers. It's a
> beautiful abbreviation that enhances the readability of my code.
>
> Here is a pattern that I use from time to time:
>
> result = func(x) unless func(x)==y
>
> This has the potential to invoke func(x) twice in some implementations.
>
> It occurred to me that the 'case' semantics could be borrowed to create
> a useful new modifier. Here's a suggestion that might be more efficient
> and possibly more readable:
>
> result = func(x) when result==y

I'm not sure how easy it would be to implement or what other
drawbacks / benefits it might entail, but just for clarification, this
looks like it would be sugar for something like this (I'd never write
this, just trying to illustrate what I think is happening)...

result = tmp = func(x) == y ? tmp : nil; tmp = nil

> The modifier would act as a mini-continuation, so that the result would
> not be updated (i.e., it would be rolled back) if the condition is not
> satisfied.
>
> Then 'when' modifier could invoke the case equality operator by default.
> For example:
>
> result = func1(x) when 200..499
> result = func2(x) when 500..999
>
> or even:
>
> result = translate(x) when Class1,Class2
> result = translate(x) when value1,range2,Class3
>
> Anyone else have a use for this? Any thoughts??

In these examples (besides allowing multiple conditions), what would
be the difference between 'when' and 'if'?

> Best regards,
> Tom Agnew

Regards,
Jordan

Trans

12/19/2007 1:41:00 AM

0



On Dec 18, 6:20 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 17, 10:31 pm, Tom Agnew <agne...@nospam.net> wrote:
>
>
>
> > Hello all,
>
> > I have a suggestion for an enhancement that could add to the readability
> > of my programs.
>
> > I'm a frequent user of 'if' and 'unless' statement modifiers. It's a
> > beautiful abbreviation that enhances the readability of my code.
>
> > Here is a pattern that I use from time to time:
>
> > result = func(x) unless func(x)==y

result = z unless (z = func(x))==y

However I sort of like the idea, and even better:

result = func(x) when y

T.

MonkeeSage

12/19/2007 1:54:00 AM

0

On Dec 18, 7:41 pm, Trans <transf...@gmail.com> wrote:
> On Dec 18, 6:20 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
>
> > On Dec 17, 10:31 pm, Tom Agnew <agne...@nospam.net> wrote:
>
> > > Hello all,
>
> > > I have a suggestion for an enhancement that could add to the readability
> > > of my programs.
>
> > > I'm a frequent user of 'if' and 'unless' statement modifiers. It's a
> > > beautiful abbreviation that enhances the readability of my code.
>
> > > Here is a pattern that I use from time to time:
>
> > > result = func(x) unless func(x)==y
>
> result = z unless (z = func(x))==y
>
> However I sort of like the idea, and even better:
>
> result = func(x) when y
>
> T.

Nice. Slight correction...

result = z unless (z = func(x))==y

result = z if (z = func(x))==y
^^

Regards,
Jordan

Trans

12/19/2007 4:17:00 AM

0



On Dec 18, 8:55 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
> On Dec 18, 7:41 pm, Trans <transf...@gmail.com> wrote:
>
>
>
> > On Dec 18, 6:20 pm, MonkeeSage <MonkeeS...@gmail.com> wrote:
>
> > > On Dec 17, 10:31 pm, Tom Agnew <agne...@nospam.net> wrote:
>
> > > > Hello all,
>
> > > > I have a suggestion for an enhancement that could add to the readability
> > > > of my programs.
>
> > > > I'm a frequent user of 'if' and 'unless' statement modifiers. It's a
> > > > beautiful abbreviation that enhances the readability of my code.
>
> > > > Here is a pattern that I use from time to time:
>
> > > > result = func(x) unless func(x)==y
>
> > result = z unless (z = func(x))==y
>
> > However I sort of like the idea, and even better:
>
> > result = func(x) when y
>
> > T.
>
> Nice. Slight correction...
>
> result = z unless (z = func(x))==y
>
> result = z if (z = func(x))==y
> ^^

Ah, right.

Hmm... if is to unless as when is to ?

T.

Robert Klemme

12/21/2007 7:51:00 AM

0

2007/12/19, MonkeeSage <MonkeeSage@gmail.com>:
> On Dec 17, 10:31 pm, Tom Agnew <agne...@nospam.net> wrote:
> > Hello all,
> >
> > I have a suggestion for an enhancement that could add to the readability
> > of my programs.
> >
> > I'm a frequent user of 'if' and 'unless' statement modifiers. It's a
> > beautiful abbreviation that enhances the readability of my code.
> >
> > Here is a pattern that I use from time to time:
> >
> > result = func(x) unless func(x)==y
> >
> > This has the potential to invoke func(x) twice in some implementations.
> >
> > It occurred to me that the 'case' semantics could be borrowed to create
> > a useful new modifier. Here's a suggestion that might be more efficient
> > and possibly more readable:
> >
> > result = func(x) when result==y

Note, that this does not seem to be the same as above and can be replaced by

result = y if func(x) == y

This has the negative effect that it will change semantics of
assignment which is bad IMHO. If I understand you properly then the
code above with "when" should do something like

__tmp_1 = func(x)
result = __tmp_1 if __tmp_1 == y

i.e. assignment is done only after a successful test.

Personally I don't mind to write it exactly like that (with different
name for __tmp_1 probably). Because it's totally obvious what happens
here.

You could even write a method to do that for you

def choose(val, test, fallback)
val == test ? val : fallback
end
def except(val, test, fallback)
val == test ? fallback : val
end
# ...
result = except func(x), y, result

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end