Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

coffeescript how to unpack like "tuples" in python

Tags:

coffeescript

I'm new to coffeescript. Is there a way to take these three lines setting the rotation and accomplish the same thing, like you would do in python by unpacking a tuple?

@cosines = [0,1,0]
@branch.rotation.x = Math.asin(@cosines.x)
@branch.rotation.y = Math.asin(@cosines.y)
@branch.rotation.z = Math.asin(@cosines.z)
like image 255
nino Avatar asked Jul 03 '14 15:07

nino


People also ask

Can you unpack a tuple in Python?

In python tuples can be unpacked using a function in function tuple is passed and in function values are unpacked into normal variable.

How do I unpack a tuple from a list in Python?

Python uses the commas ( , ) to define a tuple, not parentheses. Unpacking tuples means assigning individual elements of a tuple to multiple variables. Use the * operator to assign remaining elements of an unpacking assignment into a list and assign it to a variable.

How does tuple unpacking work in Python?

When we are unpacking values into variables using tuple unpacking, the number of variables on the left side tuple must exactly match the number of values on the right side tuple . Otherwise, we'll get a ValueError .

How do I unpack a tuple in a for loop?

Unpack a Tuple in a for Loop in PythonThe number of variables on the left side or before the equals sign should equal the length of the tuple or the list. For example, if a tuple has 5 elements, then the code to unpack it would be as follows. We can use the same syntax to unpack values within a for loop.


1 Answers

This is the best code I could come up with.

@cosines = [0,1,0]
rot = @branch.rotation
[rot.x, rot.y, rot.z] = [Math.asin(c) for c in @cosines]

The unpacking destructuring is the same as in Python, but with square brackets.

like image 169
Oleh Prypin Avatar answered Sep 22 '22 21:09

Oleh Prypin