Sergey Ovchinnik
Sergey Ovchinnik

Reputation: 502

Call a function which is a result of another function call

I have a function which takes two arguments and returns a function with arity 1:

make_fun(A, B) -> 
    fun(C) -> 
        A + B + C
    end.

I use the function above to create a function and then apply it to an argument like this:

Fun = make_fun(1,2),
Result = Fun(3).

So that Result = 6 after this.

The question is: Is there a way to do the same thing without storing the function in Fun?

Something like this would be ideal but doesn't seem to work:

Result = make_fun(1,2)(3).

Upvotes: 2

Views: 105

Answers (2)

Pouriya
Pouriya

Reputation: 1626

make_fun(A, B) ->
      fun(C) ->
          A + B + C
      end.

Just put make_fun in parentheses :

(make_fun(1, 2))(3).

Upvotes: 3

Anjali Gupta
Anjali Gupta

Reputation: 83

How about This ?

Result = Fun(make_fun(1,2)).

Upvotes: -2

Related Questions