Zoltán
Zoltán

Reputation: 22176

Coffeescript disregard some of multiple return values of a function

I have a function that returns a pair of values. At some points in my code, I only need the second value. In Scala, I could do something like this:

[_, secondValue] = functionThatReturnsAPair()

which would discard the first value. Is there such a mechanism in coffeescript, or do I have to declare a variable which is ignored?

Upvotes: 0

Views: 655

Answers (2)

mu is too short
mu is too short

Reputation: 434785

You say that you have a function which returns a pair of values. That means that your function actually returns a two element array. So you can say:

a = functionThatReturnsAPair()[1];

The downside of this is that it sort of hides the [1] off at the right side.

Upvotes: 0

gunn
gunn

Reputation: 9165

Not exactly the same, but this will do it:

[..., a] = functionThatReturnsAPair()

Upvotes: 2

Related Questions