Extracting data from a function chain without arrays

后端 未结 5 919
臣服心动
臣服心动 2021-01-14 01:36

This is an advanced topic of

How to store data of a functional chain of Monoidal List?

I am pretty sure we can somehow extract data from a function chain wit

5条回答
  •  借酒劲吻你
    2021-01-14 01:51

    Work in progress

    Thanks to the stunning contribution by @user3297291 , I somehow could refactor the code to fit my specification, but not working because I am lost the concept during the implementation :(

    The point is whole thing must be curried, and no object.method is involved.

    Can anyone "debug" please :)

    The initial value is set to the first element, in this example as 1

    I think this is almost done.

    const isFunction = f => (typeof f === 'function');
    
    const Empty = Symbol();
    
    const L = (x = Empty) => (y = Empty) => z => isFunction(z)
        ? (() => {
            const fold = f => seed => f(x)(y) === Empty
                ? seed
                : (L)(y)(f);
            return fold(z)(x);
        })()
        : L(z)(L(x)(y));
    
    
    const sum = a => b => a + b;
    
    
    console.log(
        L(1)(2)(3)(4)(5)(6)(sum)
    );

    Output

     z => isFunction(z)
        ? (() => {
            const fold = f => seed => f(x)(y) === Empty
                ? seed
                : (L)(y)(f);
            return fold(z)(x);
        })()
        : L(z)(L(x)(y))
    

提交回复
热议问题