Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore some members of a returned tuple in Julia

Tags:

julia

Julia does not support multiple return, per se. However, Julia performs similar functionality by returning a tuple of values, which can then be assigned to a tuple of variables. For instance:

function mult_return()
    (3,2)
end

returns the tuple (3,2). We can then assign these two return values to different variables, as follows:

(a,b) = mult_return() (or a,b = mult_return() because the parenthesis are not necessary.)

My question is this: Is there a way to ignore one of the return values? For example, in Matlab syntax, a user could write:

[~, b] = mult_return()

so that only the second value is assigned to a variable.

What is the proper way to approach this problem in Julia?

like image 517
Glenn Avatar asked May 17 '14 11:05

Glenn


2 Answers

I think you can do the same thing that is common in python, namely use underscores for skipped values. Example:

a, _ = mult_return()

It works multiple times as well

_, _ = mult_return()
like image 173
nemo Avatar answered Nov 12 '22 03:11

nemo


Rather than assigning the dummy variable _, you can just do

a, = mult_return()

in order to ignore the second return value, and similarly for larger tuples.

like image 39
SGJ Avatar answered Nov 12 '22 02:11

SGJ