部分申请和Currying有什么区别?

时间:2017-12-21 07:34:23

标签: javascript currying partial-application

我觉得这是鸡和蛋的问题。 进行比较:

柯里:

x => y => z => u => value

部分申请:

f_ab = (z,u) => value

根据我的理解:

  1. Currying创造了一系列一元功能。
  2. 部分应用程序与任何arity的功能一起运行。
  3. 实施例

    const partial = 
        ( f, ...argList1 ) =>
        ( ...argList2 ) =>
        f( ...argList1, ...argList2 );
    
    const curry1 = f => a => (...rest) => f( a, ...rest );
    const curry2 = f => a => b => (...rest) => f( a, b, ...rest );  
    
    const curry = f => curryN( f, [] );
    const curryN =
        ( f, acc ) =>
        acc.length === f.length ?
        f( ...acc ) :
        arg => curryN( f, [ ...acc, arg ] ); 
    
     // Application sequence:
    // curry( volume )
    // curryN( volume, [] )
    // a => curryN( volume, [a] )
    // a => b => curryN( volume, [a, b] )
    // a => b => c => curryN( volume, [a, b, c] )
    // a => b => c => volume( a, b, c )
    
    
     const volume = ( a, b, c ) => a * b * c;
    
     console.log( 'partial', partial( volume, 2, 3 )( 4 ) );
     console.log( 'curry', curry( volume )( 2 )( 3 )( 4 ) );
    

0 个答案:

没有答案