4

I am looking for a name for the following function:

(f, a) => () => f(a)

Basically a function that returns a function which when called calls f with a.

Is there a common name for this function? maybe it can be described with some Ramda magic?


Edit to clarify:

What I'm looking for is similar to Ramda's partial,

partial(f, [a])

Except that partial is more like:

(f, a) => (b) => f(a, b)

I.e the b in the case of partial is unwanted.

Sam Pettersson
  • 3,049
  • 6
  • 23
  • 37
  • FWIW I can’t imagine many places where this would be better than storing `a` in a variable and writing the arrow function in. – Ry- Mar 09 '18 at 21:35
  • **Amazing how much opinions you're getting!** This is a *primarily opinion-based* question because you're going to get a lot of **opinions** based of experiences, Etc. Therefore, this question should be closed. – Ele Mar 09 '18 at 21:39
  • 2
    @Ele: Eh, it’s asking for a name that exists, not opinions on what it should be named. Just because a name probably doesn’t exist and people are answering with opinions on how to name it doesn’t necessarily mean it should be closed. – Ry- Mar 09 '18 at 21:40
  • @Ryan look the answers, everyone is kicking something! – Ele Mar 09 '18 at 21:41
  • I name this function `call` in many of my answers. `deferred_call` is also good – Mulan Mar 10 '18 at 06:15

5 Answers5

10

That's a thunk.

Essentially, it's a reified computation that you can pass around and evaluate on demand. There are all sorts of reasons one might want to delay evaluation of an expression (it may be expensive, it may have time-sensitive side effects, etc.).

Doing const enThunk = f => a => () => f(a); lets you pass around f(a) without actually evaluating it until a later point in time.

For a practical example, consider a fetch request. Now normally Promises start working as soon as they're constructed. But what if we want to retry the request if it fails? const response = fetch(someUrl); is done and on it's way but if we have const dataRequest = () => fetch(someUrl); the a retry is just calling the function again.

Jared Smith
  • 19,721
  • 5
  • 45
  • 83
  • 1
    I think this should be the accepted answer. It not only gives a name as requested by the OP, but also a link to a trustable source. – fps Mar 10 '18 at 13:08
  • A thunk is a zero argument function that does something. OP asks for a function that takes **two arguments** and returns a thunk. – Sylwester Mar 10 '18 at 19:22
  • 2
    @Sylwester: hence the name in the example, `enThunk` – Ry- Mar 11 '18 at 15:16
  • I got distracted from answering this question before. I was going to suggest `thunkify`. `enThunk` is so much more satisfying. – Scott Sauyet Mar 11 '18 at 20:53
  • @ScottSauyet Don't feel bad about getting distracted: I sure wish all library authors were as active in their tags here as you are! – Jared Smith Mar 12 '18 at 00:08
1

I'd call that a special case of binding

If you have a function of two parameters, say f(x, y), then you can define g(y) = f(5, y). In that case, you bound the parameter x in f to the fixed point 5.

corazza
  • 31,222
  • 37
  • 115
  • 186
1

I would call it prepared function with deferred calling.

The first part takes the function and argument and returns a function without calling the given function with argument. This is called later with explicit calling of the function.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

In JavaScript, this is likely referred to as bind. There might be a more specific name for this type of combinator in general (which is very similar to the apply combinator -> f => a => f(a)).

Simple to implement:

const bind = (f, a) => () => f(a)
djfdev
  • 5,747
  • 3
  • 19
  • 38
-1

it's a higher-order function.

A higher-order function is a function that takes at least one or more functions as an input or returns another function.

This definition satisfies the code:

(f, a) => () => f(a)

as it's essentially a function returning another function.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
  • 2
    "higher-order function" is a pretty bad name for the particular function `(f, a) => () => f(a)`. It belongs to the class of higher-order functions, but it's absolutely not the only one. – Paul Mar 09 '18 at 21:33
  • @Paulpro a function becomes “higher order” if it accepts or returns a function and that's what we are witnessing here. whether or not there is a more specific name I don't know. maybe you can tell me and at least i'll know from now on ;) – Ousmane D. Mar 09 '18 at 22:22