parallel-assignment

Swapping array elements using parallel assignment

假如想象 提交于 2019-12-22 08:03:05
问题 Intrigued by this question, I have played a bit with parallel assignment with arrays and method calls. So here's an paradigmatic example, trying to swap two members in an array, by their value: deck = ['A', 'B', 'C'] #=> ["A", "B", "C"] deck[deck.index("A")], deck[deck.index("B")] = deck[deck.index("B")], deck[deck.index("A")] #=> ["B", "A"] deck #=> ["A", "B", "C"] The array hasn't changed. But if we change the order of arguments, it works: deck[deck.index("B")], deck[deck.index("A")] = deck

Swapping array elements using parallel assignment

让人想犯罪 __ 提交于 2019-12-05 15:00:45
Intrigued by this question , I have played a bit with parallel assignment with arrays and method calls. So here's an paradigmatic example, trying to swap two members in an array, by their value: deck = ['A', 'B', 'C'] #=> ["A", "B", "C"] deck[deck.index("A")], deck[deck.index("B")] = deck[deck.index("B")], deck[deck.index("A")] #=> ["B", "A"] deck #=> ["A", "B", "C"] The array hasn't changed. But if we change the order of arguments, it works: deck[deck.index("B")], deck[deck.index("A")] = deck[deck.index("A")], deck[deck.index("B")] #=> ["A", "B"] deck #=> ["B", "A", "C"] I guess it has to do