0

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?

7
  • return [left, right]; and let [leftHalf, rightHalf] = divide(arr);. See Destructuring assignment Commented Mar 30, 2022 at 20:39
  • It's not quite clear what you are trying here. In your ECMAScript example, you are just ignoring left and leftHalf completely. Is that what you are trying to do? Commented Mar 30, 2022 at 20:41
  • @Ouroborus: Thank you. I found, though, that putting square brackets around the return statement produced the desired result; let [leftHalf, rightHalf] = divide(arr); gives me the result I outlined. Commented Mar 30, 2022 at 20:43
  • @Den I believe I did say exactly that. Commented Mar 30, 2022 at 20:44
  • Can you clarify why you cannot use Destructuring? And what does it mean that Destructuring requires "equality of variable to value"? A variable can never be equal to a value because only values can be equal to values and variables are not values. Commented Mar 30, 2022 at 20:45

1 Answer 1

2

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 consts 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. Admittedly, I am diving off at the deep end, so I am giving myself a little bit of grief when it comes to translating Python to JavaScript, as I am not familiar with either language. The brevity of the numbers function is very interesting. I think the idea of having many steps is for one (me) to get a better idea of how the merge-sort algorithm works at various stages. As for Destructuring: I will have to do quite a few exercises with it to get a handle on its utility.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.