A function that takes a function with multiple arguments and returns a function with one argument of the tuple

Is there a standard name for a higher order function that takes a function with multiple arguments and returns a function with one tuple argument:

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

This is not the same as uncurryin most programming languages, which I usually see as:

def uncurry(f):
    def f1(a, b):
        return f(a)(b)
    return f1
+4
source share
1 answer

apply()

In JavaScript, this is 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 is the context
test.apply('this', ['a', 'b', 'c'])
Run codeHide result

, , this , , null undefined, .

, , -, :

function test(a, b, c) {
  // arguments
  console.log(a)
  console.log(b)
  console.log(c)
}

// what am I? I am `apply()`
function apply(f) {
  return function (tup) {
    return f.apply(this, tup)
  }
}

var applied = apply(test)

applied(['a', 'b', 'c'])
Hide result

spread()

, , - spread(), :

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

test(...['a', 'b', 'c'])
Hide result

-, , :

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

// what am I? I am `spread()`
function spread(f) {
  return function (tup) {
    return f(...tup)
  }
}

var spreaded = spread(test)

spreaded(['a', 'b', 'c'])
Hide result
+1

Source: https://habr.com/ru/post/1682521/


All Articles