How to call a function that returns another function in JavaScript?
You could call the inner function immediately after calling the first one, because the first call returns a function and the second call gives the result.
function getFunc() { var a = 7; return function(b) { console.log(a + b); }}
getFunc()(5);
How do I write a function that returns another function?
Try this, using Python:
import math
def make_cylinder_volume_func(r):
def volume(h):
return math.pi * r * r * h
return volume
Use it like this, for example with radius=10
and height=5
:
volume_radius_10 = make_cylinder_volume_func(10)
volume_radius_10(5)
=> 1570.7963267948967
Notice that returning a function was a simple matter of defining a new function inside the function, and returning it at the end - being careful to pass the appropriate parameters for each function. FYI, the technique of returning a function from another function is known as currying.
JavaScript Functions that return function
Let's decompose the statements in its constituents:
var xx =(function(){var e = 0; return function(){return e++}})();
var e = 0;
assign 0 to ereturn function(){return e++;}
return a function f which:
2.1 return the value of e
2.2 increment e by 1
var xx = function(){var e = 0; return function(){return e++}})();
assign to xx the function
f(){ return e++}
with scope[e=0]
xx();
Exec the function f:
4.1 return the value of e // 0
4.2 increment e by one // e = 1
4.3
xx
is now the functionf(){ return e++;
} with scope[e=1]
So, xx
is the function which return the internal value of e
(starting from 0) and increment e
by one.
IF you call xx();
another time, you'll get:
xx(); // returns 1
xx = f(){ return e++;}[e=2] // returns 2 and increment e by one
What is the return type for a function that returns another function
If I understood you correctly, your solution will depend on the type that the "second" function returns.
In a nutshell, there are at least 2 ways to do it:
- Lambda syntax
- Interfaces (normal and generic interfaces)
I've tried to explain all these in the code below, please, check it:
module main
{
export class TestClass
{
// Use lamba syntax as an interface for a return function
protected returnSpecificFunctionWhichReturnsNumber(): () => number
{
return this.specificFunctionWhichReturnsNumber;
}
protected specificFunctionWhichReturnsNumber(): number
{
return 0;
}
// Use an interface to describe a return function
protected returnSpecificInterfaceFunction(): INumberFunction
{
return this.specificFunctionWhichReturnsNumber;
}
// Use a generic interface to describe a return function
protected returnSpecificGenericInterfaceFunction(): IReturnFunction<number>
{
return this.specificFunctionWhichReturnsNumber;
}
}
// An interface for a function, which returns a number
export interface INumberFunction
{
(): number;
}
// A generic interface for a function, which returns something
export interface IReturnFunction<ValueType>
{
(): ValueType;
}
}
Functions can return another function use in Python
A couple more comments should make it clear:
# Functions can return another function
def create_adder(x):
def adder(y):
return x+y
return adder
add_15 = create_adder(15)
# def create_adder(15):
# def adder(y):
# return 15+y
# return adder
print (add_15(10))
# add_15(10) = adder(10)
# adder(10) # returns 15 + 10 = 25
Creating a new function as return in python function?
You should be able to do this by defining a new function inline:
def fourier_series(f, N):
def F(x):
...
return F
You are not limited to the arguments you pass in to fourier_series
:
def f(a):
def F(b):
return b + 5
return F
>>> fun = f(10)
>>> fun(3)
8
What does return function do in the functions below?
If you take a step back and substitute in different functions, this will make more sense.
Instead of return fn(fn(n))
, look at simpler examples.
return int(n)
This returns the result of int(n)
. n
is passed to int
, then whatever int
returns is returned.
return str(int(n))
This returns the result of str(int(n))
. n
is passed to int
, then whatever int
returns is passed to str
, then whatever str
returns is returned.
The difficult part in that example is that fn
are functions that were passed in as arguments to call
and squared_call
.
When you write call(mult_by_five, 1)
, the return line in call
is equivalent to:
return mult_by_five(1)
When you write squared_call(mult_by_five, 1)
, the return line in squared_call
is equivalent to:
return mult_by_five(mult_by_five(1))
Returning a function from inside another function in Java
The closest Java equivalent is this:
public static void main(String[] args) {
UnaryOperator<Integer> answer = magic();
System.out.println(magic().apply(1337)); // 56154
System.out.println(answer.apply(1337)); // 56154
}
static UnaryOperator<Integer> magic() {
return x -> x * 42;
}
Related Topics
How to Convert Date to Timestamp
Jquery (Or Pure Js) Simulate Enter Key Pressed for Testing
Differencebetween State and Props in React
How to Insert Text into the Textarea at the Current Cursor Position
How to Calculate Md5 Hash of a File Using JavaScript
Remove Items from Array with Splice in for Loop
Jquery Checkbox Checked State Changed Event
How Does Garbage Collection Work in JavaScript
Javascript: Class.Method VS. Class.Prototype.Method
Serializing Object That Contains Cyclic Object Value
How to Destructure Object Properties with Key Names That Are Invalid Variable Names
Functions That Return a Function
Convert '1' to '0001' in JavaScript
Document.All VS. Document.Getelementbyid
How to Get a Url Parameter in Express
How to Perform Case-Insensitive Sorting Array of String in JavaScript