Python has a succinct way of assigning a function with a parameter to multiple variables, done as a one-liner. I would like to know whether JavaScript has the same, or similar way of doing that.
I have found several examples describing multiple variable assignments for JavaScript, using multiple assignments and Destructuring. The issue with these methods is that they can, or do require equality of variable to value:
let a = b = c = some_value
, or let [a, b] = [x, y]
. This is where I have a problem with a particular part of code.
Python
#!/usr/bin/env python3
def numbers(arr):
if len(arr) <= 1:
return arr
left_half, right_half = divide(arr) # The code in question; Multi-variable assignment
return left_half, right_half
def divide(arr):
mid = len(arr) // 2
left = arr[:mid]
right = arr[mid:]
return left, right
l = [12, 45, 9, 2, 5]
print(numbers(l)) # ...([12, 45],[9, 2, 5])
JavaScript
function numbers(arr) {
if (arr.length <= 1) {
return arr;
}
let leftHalf, rightHalf = divide(arr); // The not-so-equivalent code
return leftHalf, rightHalf;
}
function divide(arr) {
let mid = Math.floor(arr.length / 2);
let left = arr.slice(0, mid);
let right = arr.slice(mid);
return left, right;
}
const l = [12, 45, 9, 2, 5];
console.log(numbers(l)); // ...[9, 2, 5]
How can the same or similar pattern as Python be achieved in the JavaScript code?
CodePudding user response:
In the question, you state that you cannot use Destructuring. However, Destructuring, more precisely an Array Destructuring Binding Pattern is exactly the right tool to use here, so you really should be using it. (Well, except for the fact that it is redundant, but more on that later.)
Here is how you would do a 1:1 translation of the Python code (Well, except for the fact that ECMAScript does not have Tuples) to ECMAScript:
function numbers(arr) {
if (arr.length <= 1) {
return arr;
}
const [leftHalf, rightHalf] = divide(arr);
return [leftHalf, rightHalf];
}
function divide(arr) {
const mid = Math.floor(arr.length / 2);
const left = arr.slice(0, mid);
const right = arr.slice(mid);
return [left, right];
}
The only difference of the ECMAScript code compared to the Python code is that in the Python code, both numbers
and divide
return a Tuple whereas ECMAScript does not have Tuples, so, in the ECMAScript code, they return Arrays.
However, note that in both the Python and the ECMAScript version, numbers
destructures the return value of divide
, only to put it back together with the exact same data structure, so you can just get rid of most of that code:
function numbers(arr) {
if (arr.length <= 1) {
return arr;
}
return divide(arr);
}
def numbers(arr):
if len(arr) <= 1:
return arr
return divide(arr)
You will also notice that I changed all your variables to const
s in the ECMAScript code, since you are not modifying their bindings.
This API is weird and hard to use, though, because the numbers
function returns two completely different things depending on the length of the argument array. It returns either an array of numbers or an array-of-arrays-of-numbers (in ECMAScript, in Python, it returns a tuple-of-arrays-of-numbers). This makes it harder for the caller to use since the different return types need to be treated differently.