You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS如何生成可配置嵌套深度的函数调用字符串全组合

函数调用组合生成问题

现有函数定义

我现有3个JavaScript函数,定义如下:

function f1(a,b){
return 0;//returns something
}
function f2(a,b){
return 0;//returns something
}
function f3(a){/*note that this has only 1 input: a*/
return 0;//returns something
}

需求说明

我需要找出这些函数满足单条字符串内最多包含2层函数调用的所有可能组合,最终以数组形式返回,预期返回的字符串格式示例如下:

"f1(f1($,$),$)"//used functions:2:- f1,f1
"f1($,f1($,$))"//used functions:2:- f1,f1
"f1(f2($,$),$)"//used functions:2:- f1,f2
"f1($,f2($,$))"//used functions:2:- f1,f2
"f1(f3($),$)"//used functions:2:- f1,f3
"f1($,f3($))"//used functions:2:- f1,f3
"f1($,$)"//used functions:1:- f1

"f2(f1($,$),$)"//used functions:2:- f2,f1
"f2($,f1($,$))"//used functions:2:- f2,f1
"f2(f2($,$),$)"//used functions:2:- f2,f2
"f2($,f2($,$))"//used functions:2:- f2,f2
"f2(f3($),$)"//used functions:2:- f2,f3
"f2($,f3($))"//used functions:2:- f2,f3
"f2($,$)"//used functions:1:- f2

"f3(f1($,$))"//used functions:2:- f3,f1
"f3(f2($,$))"//used functions:2:- f3,f2
"f3(f3($))"//used functions:2:- f3,f3
"f3($)"//used functions:1:- f3

注意事项

  • $代表无需额外处理的固定占位值,在生成结果中始终保留为$即可;
  • 函数集合支持扩展:我的实际业务场景共有7个JS函数,其中6个接收2个入参,1个接收1个入参;
  • 最大嵌套层数支持扩展:“最多2层函数调用”的规则可按需调整为最多5层等任意数值。

已实现代码

我目前已经掌握了定长字符组合的生成方法,已实现的代码如下:

/*
 * Generate all possible combinations from a list of characters for a given length
 */
function* charCombinations(chars, minLength, maxLength) {
  chars = typeof chars === 'string' ? chars : '';
  minLength = parseInt(minLength) || 0;
  maxLength = Math.max(parseInt(maxLength) || 0, minLength);

  //Generate for each word length
  for (i = minLength; i <= maxLength; i++) {

    //Generate the first word for the combination length by the repetition of first character.
    word = (chars[0] || '').repeat(i);
    yield word;

    //Generate other possible combinations for the word
    //Total combinations will be chars.length raised to power of word.length
    //Make iteration for all possible combinations
    for (j = 1; j < Math.pow(chars.length, i); j++) {

      //Make iteration for all indices of the word
      for (k = 0; k < i; k++) {

        //check if the current index char need to be flipped to the next char.
        if (!(j % Math.pow(chars.length, k))) {

          // Flip the current index char to the next.
          let charIndex = chars.indexOf(word[k]) + 1;
          char = chars[charIndex < chars.length ? charIndex : 0];
          word = word.substr(0, k) + char + word.substr(k + char.length);
        }
      }

      //Re-oder not neccesary but it makes the words are yeilded alphabetically on ascending order.
      yield word.split('').reverse().join('');
    }
  }
}

let combinations = charCombinations('abc', 1, 3);
let combination = 0;
var carray = [];
while (typeof combination != "undefined") {
  combination = combinations.next().value
  carray.push(combination);
}
carray.pop();

console.log(carray);

待解决问题

上述代码是我参考公开技术博客内容修改实现的,但我不知道如何将其适配为生成带括号的函数调用格式字符串的逻辑。我猜测使用XML处理树结构的思路会有帮助,我本身有在JS中使用XML处理数据与算法逻辑的经验,但仍不清楚具体实现方案。


内容的提问来源于stack exchange,提问作者roboto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 19:12:19