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)
In python tuples can be unpacked using a function in function tuple is passed and in function values are unpacked into normal variable.
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.
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 .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With