如何防止递归函数重新初始化累积变量? [英] How to prevent a recursive function from re-initializing an accumulating variable?

查看:97
本文介绍了如何防止递归函数重新初始化累积变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个函数是用 JavaScript 编写的,但我认为这个概念可以用其他一些编程语言来实现.

This function is written in JavaScript but I think that the concept can be implemented with some other programming languages.

function uniteUnique(arr) {
    let seenBefore = []; //the accumulating array
    for (let item of arguments) {
        if (typeof (item) == "object") {
            uniteUnique(...item);
        }
        else if (!seenBefore.includes(item)) {
            seenBefore.push(item);
        }
    }
    return seenBefore;
}

简而言之,该函数迭代它作为参数接收的数组,这些数组本身可能包含也可能不包含其他数组.任何这些数组的最深层包含 int 值.该函数返回一个包含所有那些 int 的数组(即出现在嵌套数组中的那些),但它只返回每个 int 一次,即使它出现了多次.

In short, the function iterates over arrays it receives as arguments, which may or may not contain other arrays themselves. the deepest level of any of those arrays contains int values. The function returns an array that contains all those ints (i.e those that appeared in nested arrays), but it returns each int only once, even if it appeared more than one time.

我的问题在于,每次递归返回到更高级别时,它都会再次初始化包含保存的ints的数组,即该函数需要返回(seenBefore),因此破坏了整个过程.一方面,我必须在函数启动时初始化数组,但另一方面,它被多次初始化并丢失其先前存储的值.

My problem lies in the fact that every time the recursion returns to a higher level, it initializes again the array that contains the saved ints, namely the array that the function needs to return (seenBefore), and therefore ruins the whole process. On one hand, I must initialize the array when the function starts, but on the other hand, it is initialized more than once and loses its previously stored values.

例如,如果我要运行函数

for example, if I would run the function

uniteUnique([1, 3, [6, 3], 2], [5, 2, 1, 4], [2, 1]);

输出应该是

[1,3,6,2,5,4]

因为该函数必须按照处理的顺序只返回一次偶然发现的数字.该函数实际上返回一个空数组,因为它在函数从递归的最顶层返回之前再次初始化.

because the function has to return the numbers it stumble upon only once by the order of processing. The function actually returns an empty array because it is initialized again right before the function returns from the top-most level of the recursion.

我怎样才能绕过这个问题?

How can I bypass this problem?

(PS:我知道这可以通过将累积数组从函数中拉出到不同的作用域来解决",但这会导致其他问题,例如每次运行函数之前都需要重新初始化累积数组如果我运行它不止一次.)

(P.S: I know this can be "solved" by pulling the accumulating array out of the function to a different scope, but that causes other problems, such as the need to reinitialize the accumulating array each time before I run the function if I run it more than once.)

推荐答案

另一种可能是将 seenBefore 定义为空数组 作为默认的第二个参数,即递归地传递给函数的每次调用:

Another possibility would be to define seenBefore as the empty array as a default second parameter, which is recursively passed to every call of the function:

function uniteUnique(arr, seenBefore = []) {
  for (const item of arr) {
    if (typeof (item) == "object") {
      uniteUnique(item, seenBefore);
    }
    else if (!seenBefore.includes(item)) {
      seenBefore.push(item);
    }
  }
  return seenBefore;
}

uniteUnique(someArr);

请注意,这里接受单个参数作为数组,而不是多个参数.

Note that this accepts a single argument as an array, rather than multiple arguments.

这篇关于如何防止递归函数重新初始化累积变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆