[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Multiple return and parallel assignement

Eric Mahurin

5/20/2005 3:32:00 PM


--- James Britt <james_b@neurogami.com> wrote:
> James Edward Gray II wrote:
> > On May 20, 2005, at 8:05 AM, jean wrote:
> >
> >> What do you think about? (Perhaps I don't know enough Ruby
> and there is
> >> a way to do the same thing in Ruby ;-) )
> >>
> >> Thinking at Ruby I'd like that if I call mymethod defined
> above with
> >> only a variable it gets the first value:
> >
> >
> > Probably not what you want, but:
> >
> > irb(main):001:0> def mymethod
> > irb(main):002:1> return 1, 2
> > irb(main):003:1> end
> > => nil
> > irb(main):004:0> one = mymethod.first
> > => 1
> >
> > Hope that helps.
>
> I think this helps point out that mymethod is not returning
> multiple
> values, but a single value (an Array object) that contains
> multiple values.

yep. return 1,2 and return [1,2] work the same. Also when the
RHS or LHS of an assign is only single item and the other side
is multiple items you can think of that single item being
splatted (*):

a = 1,2 # *a = 1,2 # a = [1,2]
a,b = [1,2] # a,b = *[1,2] # a,b = 1,2

I've never quite understood why most languages inherently
support returning only a single value (Ruby uses an array to
make it look like multiple return values). Multiple return
values could have been passed back to the caller on the stack
just like arguments were passed to it. Maybe Ruby also uses an
array to implement multiple arguments. For Ruby, it really
doesn't matter too much since we have some niceties (splatting
and multiple assign) that give you the similar functionality as
true multiple return values. You can't differentiate between
something returning a single array and something returning
multiple values, but I don't think that is a big deal.




Yahoo! Mail
Stay connected, organized, and protected. Take the tour:
http://tour.mail.yahoo.com/mai...



24 Answers

dblack

5/20/2005 3:58:00 PM

0

james_b

5/20/2005 4:30:00 PM

0

Eric Mahurin wrote:
> I've never quite understood why most languages inherently
> support returning only a single value


Conceptual elegance?

Seems cleaner to say that every expression returns an object, though
that the object may be a container for other objects, rather than assert
that expressions may return an unknown number of objects


James


Ralph \"PJPizza\" Siegler

5/20/2005 5:50:00 PM

0

On Sat, May 21, 2005 at 01:30:13AM +0900, James Britt wrote:
> Eric Mahurin wrote:
> >I've never quite understood why most languages inherently
> >support returning only a single value
>
>
> Conceptual elegance?
>
> Seems cleaner to say that every expression returns an object, though
> that the object may be a container for other objects, rather than assert
> that expressions may return an unknown number of objects
>
>
> James
>

I also think it may be from the original mathematical idea of a "function', which only has 1 output value for a given set of inputs, which 3rd generation programming languages adopted as paradigm. With multiple return values, more in the mindset of a "relation", an n to m mapping


Ralph "PJPizza" Siegler


Luca Pireddu

5/20/2005 6:12:00 PM

0

David A. Black wrote:

> Hi --
>
> On Sat, 21 May 2005, Eric Mahurin wrote:
>
>
> It's hard, I find, to come up with an exact description of what the
> unarr?ay (unary unarray) operator does that fits every case. The
> closest I've come is: *a = x means: a gets assigned that which, when
> stripped of one level of array-ness, is x. Thus:
>
> *a = 1,2 # [1,2] stripped of [] is 1,2 so a is [1,2]
> *a = *[1,2] # a stripped of [] is [1,2] stripped of [], so
> # a is [1,2]
>
> def x(*args); end
> x(1,2,3) # [1,2,3] stripped of [] is 1,2,3, so args is [1,2,3]
> x([1,2,3]) # [[1,2,3]] stripped of [] is [1,2,3], so args
> # is [[1,2,3]]
>
> Then there's
>
> a = 1,2 # automatic arraying -- the opposite of *
> a = *[1,2] # un-arraying followed by automatic arraying :-)
>
> or something like that.
>
>
> David
>

Maybe a simpler way to look at it is *a = x means a = [x], or simply enclose
the rhs in an array before assignment.

Luca

Robert Klemme

5/20/2005 6:19:00 PM

0

Ralph "PJPizza" Siegler wrote:
> On Sat, May 21, 2005 at 01:30:13AM +0900, James Britt wrote:
>> Eric Mahurin wrote:
>>> I've never quite understood why most languages inherently
>>> support returning only a single value
>>
>>
>> Conceptual elegance?
>>
>> Seems cleaner to say that every expression returns an object, though
>> that the object may be a container for other objects, rather than
>> assert that expressions may return an unknown number of objects
>>
>>
>> James
>>
>
> I also think it may be from the original mathematical idea of a
> "function', which only has 1 output value for a given set of inputs,

Mathematical functions are in no way limited to yielding a single output
value.

> which 3rd generation programming languages adopted as paradigm. With
> multiple return values, more in the mindset of a "relation", an n to
> m mapping

That's what a mathematical function is basically. Don't let sin, cos,
max, min etc. fool you on this one. :-)

Kind regards

robert

dblack

5/20/2005 7:33:00 PM

0

Luca Pireddu

5/21/2005 6:35:00 AM

0

David A. Black wrote:

> Hi --
>
> On Sat, 21 May 2005, Luca Pireddu wrote:
>
>> David A. Black wrote:
>>
>>> Hi --
>>>
>>> On Sat, 21 May 2005, Eric Mahurin wrote:
>>>
>>>
>>> It's hard, I find, to come up with an exact description of what the
>>> unarr?ay (unary unarray) operator does that fits every case. The
>>> closest I've come is: *a = x means: a gets assigned that which, when
>>> stripped of one level of array-ness, is x. Thus:
>>>
>>> *a = 1,2 # [1,2] stripped of [] is 1,2 so a is [1,2]
>>> *a = *[1,2] # a stripped of [] is [1,2] stripped of [], so
>>> # a is [1,2]
>>>
>>> def x(*args); end
>>> x(1,2,3) # [1,2,3] stripped of [] is 1,2,3, so args is [1,2,3]
>>> x([1,2,3]) # [[1,2,3]] stripped of [] is [1,2,3], so args
>>> # is [[1,2,3]]
>>>
>>> Then there's
>>>
>>> a = 1,2 # automatic arraying -- the opposite of *
>>> a = *[1,2] # un-arraying followed by automatic arraying :-)
>>>
>>> or something like that.
>>>
>>>
>>> David
>>>
>>
>> Maybe a simpler way to look at it is *a = x means a = [x], or simply
>> enclose the rhs in an array before assignment.
>
> I'm not sure how that's different from what I was saying above. Or
> did you mean a simpler way to say it? :-)
>
>
> David
>

Hello. Yes, I wasn't trying to correct you. Just suggesting a simpler way of
saying the same thing :-)

Luca

Martin DeMello

5/21/2005 9:34:00 PM

0

David A. Black <dblack@wobblini.net> wrote:
>
> It's hard, I find, to come up with an exact description of what the
> unarr?ay (unary unarray) operator does that fits every case. The
> closest I've come is: *a = x means: a gets assigned that which, when
> stripped of one level of array-ness, is x. Thus:

And a simple rule of thumb to 'implement' it is: every time you remove a
* on one side of the =, add a [] to the other side.

*a = 1,2 --> a = [1,2]
*a = *[1,2] --> [a] = [[1,2]] --> a = [1,2]

etc.

martin

Gary Wright

5/21/2005 9:49:00 PM

0


On May 21, 2005, at 5:35 PM, Martin DeMello wrote:
> And a simple rule of thumb to 'implement' it is: every time you
> remove a
> * on one side of the =, add a [] to the other side.
>
> *a = 1,2 --> a = [1,2]
> *a = *[1,2] --> [a] = [[1,2]] --> a = [1,2]
------------------------^
I think you dropped a '*':

*a = *[1,2] --> [a] = [*[1,2]] --> a = [1,2]

Is there a single good reference page that summarizes the rules for

multiple assignment
actual to formal argument assignment (for methods/lambdas)
actual to formal argument assignment (for blocks)
return expression to lhs assignment

I've seen it mentioned that the rules aren't quite identical for
all these scenarios.



Gary Wright



dblack

5/21/2005 10:00:00 PM

0