TransWikia.com

What do you call a function that returns a function?

Software Engineering Asked by Splox on October 29, 2021

Let’s say, for example, that I have a function makeFoo that makes another function foo:

function makeFoo(string) {
    return () => string
}

const foo = makeFoo('bar');

I know that makeFoo is a higher-order function because it returns a function, but is makeFoo the best name? Would something like createFoo or fooFactory or provideFoo be better? Is there any sort of standard at all?

3 Answers

Pattern-like names are very much an OOP convention. In FP, higher-order functions aren't a pattern, they're just syntax. You typically use names that go with the domain.

Functions that return a function aren't usually thought of as creating a function, but as supplying parts of the arguments to a function at different times. In languages that support a curried function argument syntax, you almost always use that instead of the way you wrote it. This is usually to meet the signature requirements of some callback function.

For example:

const logEvent = (string) => () => console.log(string);

document.getElementById("myButton").onclick = logEvent("myButton clicked!");
document.getElementById("another").onclick = logEvent("another clicked!");

  

Here, onclick requires a function, but I don't want to repeat the clutter of anonymous functions all over the place, so I create a curried function. The logEvent name describes the function that eventually happens when the onclick event occurs. Note if I had named this something like createStringClickHandler, it would be a lot more difficult to determine at the calling point what it's actually doing.

Answered by Karl Bielefeldt on October 29, 2021

As you ask specifically for what this is called in Java I'd say for your specific example its a StringSupplierSupplier or Supplier<Supplier<String>> https://docs.oracle.com/javase/8/docs/api/java/util/function/Supplier.html but I wouldn't necessarily call it that.

In general a function that returns a function is the same as a function with an extra argument (possibly with lazier evaluation) so often there will not be a special name for this. e.g. we can implement add as:

int add (int a, int b) { return a + b;}
add(2,3); //5

but we can also implement it as:

int add (int a) { return b => a + b;}
add(2)(3); //5

Answered by jk. on October 29, 2021

Create or factory or make are all wrong because from the perspective of the caller it is about the process of obtaining the function, not about the creation of it (this is a hidden detail, you do not care if it was created on the fly or waiting for you to collect, you just want to have it now). So it should be something like GetDoWhatever().

The variable you assign the result to would be doWhatever().

Answered by Martin Maat on October 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP