Javascript add to string with each function call

后端 未结 5 2370
执念已碎
执念已碎 2020-12-21 05:32

I have the following situation where I have a function f which takes an argument input.

I want to be able to have f such that

相关标签:
5条回答
  • 2020-12-21 05:58

    In JS you can even achieve this dualitiy of a function also "being" a String. Yet this is nothing you should ever use in production code.

    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"));

    0 讨论(0)
  • 2020-12-21 05:59

    f() can not return fo, it has to return a function. To get fo you need to call f like f()('').

    Try this:

    function f(input, acc) {
        acc = acc || 'f'
        if (input === undefined) {
            return function(a) {
                return f(a, acc + 'o')
            }
        }
        return acc + input
    }
    
    console.log(f('l'))
    console.log(f()(''))
    console.log(f()('l'))
    console.log(f()()('foo'))

    0 讨论(0)
  • 2020-12-21 06:00

    Try this

    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'))

    0 讨论(0)
  • 2020-12-21 06:07
    f() --> fo
    

    That is impossible if f()'s return value is also callable as a function. I think you must be misremembering the detail of where you got this (since the rest if familiar, not unique to this question). Similarly, some of the other examples have the wrong number of os for how this is normally formulated.

    How can I write f in order to get my desired output while keeping the function stateless?

    I don't think you can, but you can do it such that any particular function involved has no changing state:

    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"

    In that, f is stateless, and the state that the function f returns closes over (when it returns a function) never changes.

    0 讨论(0)
  • 2020-12-21 06:08

    As I suggest you in comments, you should better use generators to get each value after each call.

    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}
    

    As this, you just have to call the next function each time you need and concatenate the result.value each time.

    Here more docs about generators : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function

    0 讨论(0)
提交回复
热议问题