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
scala higher-order-functions
add a comment |
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
scala higher-order-functions
What sort of explanation are you looking for? You need to be a bit more specific about the problem you are facing.
– Tim
yesterday
add a comment |
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
scala higher-order-functions
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
scala higher-order-functions
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
add a comment |
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
add a comment |
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 Int
s 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
"That is a function calledsum
" – 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
add a comment |
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 Int
s 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
"That is a function calledsum
" – 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
add a comment |
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 Int
s 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
"That is a function calledsum
" – 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
add a comment |
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 Int
s 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
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 Int
s 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
edited yesterday
answered yesterday
houcros
398418
398418
"That is a function calledsum
" – 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
add a comment |
"That is a function calledsum
" – 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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What sort of explanation are you looking for? You need to be a bit more specific about the problem you are facing.
– Tim
yesterday