Definition of multiple variables with identical value by using a single command (Matlab's 'deal')

634 views
Skip to first unread message

Theodore Papamarkou

unread,
Mar 23, 2013, 8:43:37 AM3/23/13
to juli...@googlegroups.com
Is there a way of defining multiple variables with identical value by typing a single assignment? I am looking for sth similar to the 'deal' Matlab command. For instance,

(x, y) = deal(eye(2))

would create two variables x and y, each of which holds the 2x2 eye(2) matrix.

Although this is a trivial command, it saves some typing and perhaps makes the code look more elegant. Sorry if this suggested feature is available already and I don't know it :)

John Myles White

unread,
Mar 23, 2013, 8:52:06 AM3/23/13
to juli...@googlegroups.com
Much Julia doesn't have Matlab's ability to respond to different numbers of outputs (varargs), I think this would have to be a macro of some sort:

@deal x y eye(2)

Probably could look a lot nicer than that, though.

-- John

Theodore Papamarkou

unread,
Mar 23, 2013, 9:01:10 AM3/23/13
to juli...@googlegroups.com
Thanks John, the macro invocation you suggested looks nice. The only implication is that one may want to input a finite number of arguments to it, for example

@deal x y z eye(2)

I am not familiar yet with how Julia deals with different numbers of outputs - thanks for the explanation. From what you told me, if I understood well, this means that it is not possible to define a macro which takes a variable number of arguments...?

John Myles White

unread,
Mar 23, 2013, 9:05:03 AM3/23/13
to juli...@googlegroups.com
No, you can arbitrarily many arguments.

What you can't do is make a function return different things depending on the calling context.

You can't use the two calling contexts

x = f(a)
x, y = f(a)

and expect f(a) to have information about the calling context that allows it to modify its behavior.

 -- John

Theodore Papamarkou

unread,
Mar 23, 2013, 9:09:51 AM3/23/13
to juli...@googlegroups.com
Thanks John, I understand what you mean now.

This implies that it is not possible to implement the 'deal' macro the way I had it in mind, it is not such an important feature anyhow...

Jacob Quinn

unread,
Mar 23, 2013, 9:59:18 AM3/23/13
to juli...@googlegroups.com
Julia does support destructuring.

x, y = eye(2), eye(2)

and assignment chaining

x = y = eye(2)

and you could implement a function similar to deal:

deal(x)= x, x

But like John said, there doesn't seem to be an obvious way for auto variable-output return (probably for the best...).

A @deal macro would have to be given the number of outputs, but could then, I guess, do what you're talking about.

-Jacob

Jared Kofron

unread,
Mar 23, 2013, 10:03:33 AM3/23/13
to juli...@googlegroups.com
I personally find MATLABs inspection of output variables counterproductive from a programming standpoint. It's not nice in general to have to reason about the behavior of variable assignment. 

Theodore Papamarkou

unread,
Mar 23, 2013, 10:18:02 AM3/23/13
to juli...@googlegroups.com
I may be wrong about this Jared, yet I think that Julia prompts the programmer to reason about the behaviour of variable assignment. For example,

x, y = (1, 2)

is in a sense multiple assignment in a single line. However, I get your point - Julia's assignment mechanism is pretty neat as it stands, avoiding potential confusion (Matlab's 'deal' does not hinder confusion either I think...).

The assignment chaining you suggested Jacob is great (I really don't need the 'deal' command then), I wonder how I didn't think of it...

Jared Kofron

unread,
Mar 23, 2013, 11:13:34 AM3/23/13
to juli...@googlegroups.com
Yes, but not in the sense that the behavior of the function itself can change depending on what it's result is bound to, which IMO is very strange. 

Theodore Papamarkou

unread,
Mar 23, 2013, 11:28:10 AM3/23/13
to juli...@googlegroups.com
That's true.

Pierre-Yves Gérardy

unread,
Mar 23, 2013, 11:45:27 AM3/23/13
to juli...@googlegroups.com


On Saturday, March 23, 2013 1:43:37 PM UTC+1, Theodore Papamarkou wrote:
Is there a way of defining multiple variables with identical value by typing a single assignment? I am looking for sth similar to the 'deal' Matlab command. For instance,

(x, y) = deal(eye(2))

type Deal
     data
end

import Base.start, Base.next, Base.done

start (d::Deal) = 1
next (d::Deal, n) = (d.data, 1)
done (d::Deal, n) = false

a,b,c = Deal(eye(2))

# :-) 

Theodore Papamarkou

unread,
Mar 23, 2013, 11:59:06 AM3/23/13
to juli...@googlegroups.com
Thanks Pierre-Yves! The assignment chaining that Jacob suggested is also a good solution, so I don't know if users would like to have a 'deal' command after all (I like both a = b = c = eye(2) and a, b, c = deal(eye(2)) idioms...)

Pierre-Yves Gérardy

unread,
Mar 23, 2013, 12:07:44 PM3/23/13
to juli...@googlegroups.com
I think that assignment chaining is the way to go. This is less efficient, and non-idiomatic, but it can be done if you really want.

Douglas Bates

unread,
Mar 23, 2013, 4:54:26 PM3/23/13
to juli...@googlegroups.com
On Saturday, March 23, 2013 10:59:06 AM UTC-5, Theodore Papamarkou wrote:
Thanks Pierre-Yves! The assignment chaining that Jacob suggested is also a good solution, so I don't know if users would like to have a 'deal' command after all (I like both a = b = c = eye(2) and a, b, c = deal(eye(2)) idioms...)

Be careful of assignment chaining.  I was going to comment on Jacob's posting that

x,y =eye(2),eye(2)

 and

x=y=eye(2)

have different effects.  In the latter assignment changes to the contents of x also change the contents of y
julia> x, y = eye(2), eye(2)
(
2x2 Float64 Array:
 1.0  0.0
 0.0  1.0,

2x2 Float64 Array:
 1.0  0.0
 0.0  1.0)

julia> x[1,2] = -1.
-1.0

julia> x
2x2 Float64 Array:
 1.0  -1.0
 0.0   1.0

julia> y
2x2 Float64 Array:
 1.0  0.0
 0.0  1.0

julia> x = y = eye(2)
2x2 Float64 Array:
 1.0  0.0
 0.0  1.0

julia> x[1,2] = -1.
-1.0

julia> y
2x2 Float64 Array:
 1.0  -1.0
 0.0   1.0



Jacob Quinn

unread,
Mar 23, 2013, 5:06:17 PM3/23/13
to juli...@googlegroups.com
Good call Doug. I should have clarified. That's 'working with arrays in Julia' 101. Assignment chaining is more appropriate with non-array values or when the arrays won't be tampered with.

-Jacob

Patrick O'Leary

unread,
Mar 24, 2013, 10:19:04 PM3/24/13
to juli...@googlegroups.com
I'm surprised no one has posted an implementation yet. So just for kicks...

macro deal(xprs...)
    assignments = {}
    for x in xprs[1:end-1]
        push!(assignments, :($x = $(xprs[end])))
    end
    esc(quote
        $(assignments...)
    end)
end
Reply all
Reply to author
Forward
0 new messages