Javascript在每个函数调用时添加到字符串

时间:2018-01-10 09:52:43

标签: javascript recursion

我有以下情况,我有一个函数f,它接受​​一个参数input

我希望能够f满足以下输出:

f('l') --> fl

f() --> fo

f()('l') --> fol

f()()('l') --> fool

f()()()('l') --> foool

我认为这可以通过以下方式实现:

function f(input) {
  let str = 'f'
  if (input) {
    return `${str}${input}`
  }
  str = `${str}o`
  return f()
}

然而,这最终会陷入无限循环。我也尝试让f返回一个函数,但这也不起作用。

如何在保持函数无状态的同时编写f以获得所需的输出?

5 个答案:

答案 0 :(得分:1)

在JS中,你甚至可以实现一个函数的“双重”,也就是“成为”一个字符串。然而,这不是你应该在生产代码中使用的。

const f = function _f(prefix){
  const toString = () => prefix;
  return Object.assign(function(suffix="o"){ 
    return _f(prefix + suffix);
  }, {
    valueOf: toString,
    toString
  });
}("f");

console.log(""+f('l'))
console.log(""+f())
console.log(""+f()('l'))
console.log(""+f()()('l'))
console.log(""+f()()()('l'))

let foo = f()();
let fool = foo("l");
console.log(""+foo("l"));
console.log(""+fool);
console.log(""+foo("bar"));
console.log(""+fool("bar"));

答案 1 :(得分:0)

试试这个



function f(input) {
  let str = 'f'
  var o = function(suffix){
    if(suffix)
      return str + suffix;
    str += "o";
    return o;
  }
  return o(input);
}

console.log(f('l'))
console.log(f()('l'))
console.log(f()()('l'))
console.log(f()()()('l'))




答案 2 :(得分:0)

public BackendResponse callBackend(BackendRequest request) throws URISyntaxException, IOException { String body = null; ResponseEntity<String> responseEntity = null; URI uri = new URI("http", null, "localhost", 8080, request.getRequestURL(), request.getQueryString(), null); MultiValueMap<String, String> requestHeaders = getHeadersInfo(request.getHttpRequest()); if (HttpMethod.POST.equals(request.getMethod())) { body = request.getHttpRequest().getReader().lines().collect(Collectors.joining(System.lineSeparator())); responseEntity = restTemplate.exchange(uri, request.getMethod(), new HttpEntity<String>(body, requestHeaders), String.class); } else if (HttpMethod.GET.equals(request.getMethod())) { responseEntity = restTemplate.exchange(uri, request.getMethod(), new HttpEntity<String>(body, requestHeaders), String.class); } else { LOG.warn("Method:{} not supported yet", request.getMethod()); } BackendResponse response = new BackendResponse(); response.setResponse(responseEntity); return response; } 无法返回f(),它必须返回一个函数。要获得fo,您需要像fo一样调用f。

试试这个:

&#13;
&#13;
f()('')
&#13;
&#13;
&#13;

答案 3 :(得分:0)

image: tetraweb/php:7.1  ## this image must have drush available

script:
  - drush config-import -y
  - drush updatedb
...

如果f() --> fo 的返回值也可以作为函数调用,那是不可能的。我认为您一定会误会您从何处获得的详细信息(因为其他信息很熟悉,不是这个问题所独有的)。同样,在其他一些示例中,f()的数量通常是错误的公式。

  

如何编写o以便在保持函数无状态的同时获得所需的输出?

我认为您不能,但是您可以这样做,以使所涉及的任何特定功能都没有更改状态:

f

在这种情况下,function f(prefix, letter) { if (prefix && letter) { return prefix + letter; } prefix = prefix ? prefix + "o" : "f"; return function(letter) { return f(prefix, letter); }; } console.log(f()("l")); // "fl" console.log(f()()("l")); // "fol" console.log(f()()()("d")); // "food"是无状态的,并且函数f返回的状态关闭(当返回函数时)永远不会改变。

答案 4 :(得分:-1)

正如我在评论中建议的那样,你应该在每次通话后更好地使用发电机来获得每个值。

function * f(input) {
    for(const character of input) {
        yield character;
    }
}
test = f('hello world');
test.next(); // {value: "h", done: false}
test.next(); // {value: "e", done: false}
test.next(); // {value: "l", done: false}
test.next(); // {value: "l", done: false}
test.next(); // {value: "o", done: false}
test.next(); // {value: " ", done: false}
test.next(); // {value: "w", done: false}
test.next(); // {value: "o", done: false}
test.next(); // {value: "r", done: false}
test.next(); // {value: "l", done: false}
test.next(); // {value: "d", done: false}
test.next(); // {value: undefined, done: true}

因此,您只需在每次需要时调用下一个函数,并每次连接result.value。

这里有更多关于生成器的文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function