You are given a code snippet that defines two functions: `reduceIndexed` and `genBitString`. Your task is to implement the function `genCombinations` that generates all possible combinations of a given array.

The `genCombinations` function takes an array of numbers as input and returns an array of arrays containing all possible combinations. The order of the combinations does not matter.

For example, given the input array `[1, 2, 3]`, the `genCombinations` function should return `[[], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]]`. 

You need to complete the definition of the `genCombinations` function.
user
Sure, I'd be happy to help you implement the `genCombinations` function. One possible implementation is using recursion with backtracking.

Here's a TypeScript code snippet for a simplified version of this function, assuming that the input array `arr` contains only integers. You'll need to adjust the types and handle any additional options as needed.

```typescript
// Returns an array of arrays containing all possible combinations
function genCombinations(arr: number[]): number[][] {
  // Base case: empty array
  if (arr.length === 0) {
    return [[], []];
  }

  // Call genCombinations recursively with a sub-array and a reduced index
  const combinations = [];
  for (let I = 0; I < arr.length; i++) {
    const subset = [...arr.slice(0, i), ...arr.slice(i + 1)];
    const partialCombinations = genCombinations(subset);
    combinations.push(...partialCombinations);
  }

  // Add the current element to all partial combinations
  for (const combination of combinations) {
    combination.push(arr[0]);
  }

  return combinations;
}
```

In this function, the recursion uses two nested loops:

1. The outer loop iterates over all indices in the input array.
2. The inner loop generates all possible combinations of the sub-array ranging from index 0 to the current index.

The `...` syntax in the `push` method adds the current element `arr[0]` to all partial combinations, effectively combining them.

This implementation generates all possible combinations up to length `arr.length`, so if you need all combinations up to length `n`, you might need to pass `n` as a second argument to the function, or modify this function to compute only up to that length.

Note that this code does not check if the input array is valid, nor does it handle any errors. You'll need to add these as necessary.
assistant
You are given a code snippet that defines two functions: `reduceIndexed` and `genBitString`. Your task is to implement the function `genCombinations` that generates all possible combinations of a given array.

The `genCombinations` function takes an array of numbers as input and returns an array of arrays containing all possible combinations. The order of the combinations does not matter.

For example, given the input array `[1, 2, 3]`, the `genCombinations` function should return `[[], [3], [2], [2,3], [1], [1,3], [1,2], [1,2,3]]`. 

You need to complete the definition of the `genCombinations` function.
user
Sure, I can help you with that. Here's how you can implement the `genCombinations` function using the `reduceIndexed` and `genBitString` functions:

```javascript
function reduceIndexed(arr, f, acc) {
  for (let i = 0; i < arr.length; i++) {
    acc = f(acc, arr[i], i);
  }
  return acc;
}

function genBitString(n) {
  let result = [];
  for (let i = 0; i < (1 << n); i++) {
    result.push((i >>> 0).toString(2).padStart(n, '0'));
  }
  return result;
}

function genCombinations(arr) {
  let bitStrings = genBitString(arr.length);
  let combinations = reduceIndexed(bitStrings, (combinations, bitString) => {
    let combination = [];
    for (let i = 0; i < bitString.length; i++) {
      if (bitString[i] === '1') {
        combination.push(arr[i]);
      }
    }
    combinations.push(combination);
    return combinations;
  }, []);
  return combinations;
}
```

The `genCombinations` function works by generating all bit strings of length equal to the length of the input array. Each bit string represents a combination, where a '1' at position `i` means that the `i`th element of the array is included in the combination. The `reduceIndexed` function is used to convert each bit string into a combination, which is then added to the array of combinations.
assistant