python - Function that takes a function with multiple arguments and returns a function with a single tuple argument -


is there standard name higher-order function takes function multiple arguments , returns function single tuple argument:

def what_am_i(f):     def f1(tup):         f(*tup)     return f1  

this not same uncurry in programming languages, see defined as:

def uncurry(f):     def f1(a, b):         return f(a)(b)     return f1 

apply()

in javascript, called apply():

'use strict'    function test(a, b, c) {    // context    console.log(this)    // arguments    console.log(a)    console.log(b)    console.log(c)  }    // first parameter context  test.apply('this', ['a', 'b', 'c'])

unfortunately, first argument tends clutter function call in lot of cases this unimportant, , therefore pass value null or undefined, in edge cases can useful.

in cases want specify array, implement wrapper function that's equivalent example so:

function test(a, b, c) {    // arguments    console.log(a)    console.log(b)    console.log(c)  }    // i? `apply()`  function apply(f) {    return function (tup) {      return f.apply(this, tup)    }  }    var applied = apply(test)    applied(['a', 'b', 'c'])

spread()

another argument make call spread(), because spread syntax same thing:

function test(a, b, c) {    console.log(a)    console.log(b)    console.log(c)  }    test(...['a', 'b', 'c'])

and can implement wrapper function that's intuitive looks this:

function test(a, b, c) {    console.log(a)    console.log(b)    console.log(c)  }    // i? `spread()`  function spread(f) {    return function (tup) {      return f(...tup)    }  }    var spreaded = spread(test)    spreaded(['a', 'b', 'c'])


Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -