Can You Explain this with an example.Encountered this when studying HOF in scala









up vote
0
down vote

favorite












This is the Function.What are the arguments here?is it a function? and what is the return type?



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int = ...
sumf










share|improve this question























  • What sort of explanation are you looking for? You need to be a bit more specific about the problem you are facing.
    – Tim
    yesterday















up vote
0
down vote

favorite












This is the Function.What are the arguments here?is it a function? and what is the return type?



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int = ...
sumf










share|improve this question























  • What sort of explanation are you looking for? You need to be a bit more specific about the problem you are facing.
    – Tim
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











This is the Function.What are the arguments here?is it a function? and what is the return type?



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int = ...
sumf










share|improve this question















This is the Function.What are the arguments here?is it a function? and what is the return type?



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int = ...
sumf







scala higher-order-functions






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited yesterday

























asked yesterday









RAM SHANKER G

15




15











  • What sort of explanation are you looking for? You need to be a bit more specific about the problem you are facing.
    – Tim
    yesterday

















  • What sort of explanation are you looking for? You need to be a bit more specific about the problem you are facing.
    – Tim
    yesterday
















What sort of explanation are you looking for? You need to be a bit more specific about the problem you are facing.
– Tim
yesterday





What sort of explanation are you looking for? You need to be a bit more specific about the problem you are facing.
– Tim
yesterday













1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










That is a function called sum. It takes as parameter one function from Int to Int and returns a function that when called with a pair of Ints returns another Int.



The actual result depends on the implementation, but we could for instance define it as:



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int =
f(a) + f(b)

sumf



Or simply:



def sum(f: Int => Int): (Int, Int) => Int = 
(a: Int, b: Int) => f(a) + f(b)



In that case, you could call it like this for instance:



def f(a: Int): Int = a * a
def g(a: Int, b: Int): Int = sum(f)(a, b)
val x: Int = g(3, 4) // x = 25





share|improve this answer






















  • "That is a function called sum" – This is wrong. sum is not a function, it is a method. Functions and methods are fundamentally different in Scala: functions are objects, methods are not. (Methods belong to objects.) Methods can be generic, functions can't. Methods can have an implicit parameter list, functions can't. Methods can have optional parameters with default arguments, functions can't. Methods can have repeated parameters, functions can't.
    – Jörg W Mittag
    18 hours ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223880%2fcan-you-explain-this-with-an-example-encountered-this-when-studying-hof-in-scala%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










That is a function called sum. It takes as parameter one function from Int to Int and returns a function that when called with a pair of Ints returns another Int.



The actual result depends on the implementation, but we could for instance define it as:



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int =
f(a) + f(b)

sumf



Or simply:



def sum(f: Int => Int): (Int, Int) => Int = 
(a: Int, b: Int) => f(a) + f(b)



In that case, you could call it like this for instance:



def f(a: Int): Int = a * a
def g(a: Int, b: Int): Int = sum(f)(a, b)
val x: Int = g(3, 4) // x = 25





share|improve this answer






















  • "That is a function called sum" – This is wrong. sum is not a function, it is a method. Functions and methods are fundamentally different in Scala: functions are objects, methods are not. (Methods belong to objects.) Methods can be generic, functions can't. Methods can have an implicit parameter list, functions can't. Methods can have optional parameters with default arguments, functions can't. Methods can have repeated parameters, functions can't.
    – Jörg W Mittag
    18 hours ago














up vote
2
down vote



accepted










That is a function called sum. It takes as parameter one function from Int to Int and returns a function that when called with a pair of Ints returns another Int.



The actual result depends on the implementation, but we could for instance define it as:



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int =
f(a) + f(b)

sumf



Or simply:



def sum(f: Int => Int): (Int, Int) => Int = 
(a: Int, b: Int) => f(a) + f(b)



In that case, you could call it like this for instance:



def f(a: Int): Int = a * a
def g(a: Int, b: Int): Int = sum(f)(a, b)
val x: Int = g(3, 4) // x = 25





share|improve this answer






















  • "That is a function called sum" – This is wrong. sum is not a function, it is a method. Functions and methods are fundamentally different in Scala: functions are objects, methods are not. (Methods belong to objects.) Methods can be generic, functions can't. Methods can have an implicit parameter list, functions can't. Methods can have optional parameters with default arguments, functions can't. Methods can have repeated parameters, functions can't.
    – Jörg W Mittag
    18 hours ago












up vote
2
down vote



accepted







up vote
2
down vote



accepted






That is a function called sum. It takes as parameter one function from Int to Int and returns a function that when called with a pair of Ints returns another Int.



The actual result depends on the implementation, but we could for instance define it as:



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int =
f(a) + f(b)

sumf



Or simply:



def sum(f: Int => Int): (Int, Int) => Int = 
(a: Int, b: Int) => f(a) + f(b)



In that case, you could call it like this for instance:



def f(a: Int): Int = a * a
def g(a: Int, b: Int): Int = sum(f)(a, b)
val x: Int = g(3, 4) // x = 25





share|improve this answer














That is a function called sum. It takes as parameter one function from Int to Int and returns a function that when called with a pair of Ints returns another Int.



The actual result depends on the implementation, but we could for instance define it as:



def sum(f: Int => Int): (Int, Int) => Int = 
def sumf(a: Int, b: Int): Int =
f(a) + f(b)

sumf



Or simply:



def sum(f: Int => Int): (Int, Int) => Int = 
(a: Int, b: Int) => f(a) + f(b)



In that case, you could call it like this for instance:



def f(a: Int): Int = a * a
def g(a: Int, b: Int): Int = sum(f)(a, b)
val x: Int = g(3, 4) // x = 25






share|improve this answer














share|improve this answer



share|improve this answer








edited yesterday

























answered yesterday









houcros

398418




398418











  • "That is a function called sum" – This is wrong. sum is not a function, it is a method. Functions and methods are fundamentally different in Scala: functions are objects, methods are not. (Methods belong to objects.) Methods can be generic, functions can't. Methods can have an implicit parameter list, functions can't. Methods can have optional parameters with default arguments, functions can't. Methods can have repeated parameters, functions can't.
    – Jörg W Mittag
    18 hours ago
















  • "That is a function called sum" – This is wrong. sum is not a function, it is a method. Functions and methods are fundamentally different in Scala: functions are objects, methods are not. (Methods belong to objects.) Methods can be generic, functions can't. Methods can have an implicit parameter list, functions can't. Methods can have optional parameters with default arguments, functions can't. Methods can have repeated parameters, functions can't.
    – Jörg W Mittag
    18 hours ago















"That is a function called sum" – This is wrong. sum is not a function, it is a method. Functions and methods are fundamentally different in Scala: functions are objects, methods are not. (Methods belong to objects.) Methods can be generic, functions can't. Methods can have an implicit parameter list, functions can't. Methods can have optional parameters with default arguments, functions can't. Methods can have repeated parameters, functions can't.
– Jörg W Mittag
18 hours ago




"That is a function called sum" – This is wrong. sum is not a function, it is a method. Functions and methods are fundamentally different in Scala: functions are objects, methods are not. (Methods belong to objects.) Methods can be generic, functions can't. Methods can have an implicit parameter list, functions can't. Methods can have optional parameters with default arguments, functions can't. Methods can have repeated parameters, functions can't.
– Jörg W Mittag
18 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53223880%2fcan-you-explain-this-with-an-example-encountered-this-when-studying-hof-in-scala%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Darth Vader #20

Ondo