I’m basically new to functional programming and scala, and the following question might possibly look stupid.
val f = (a:Int) => a+1
In the above snippet, should I consider f to be a function or a variable? Coming from a C/C++ background, the first thought that occurs is that f is a variable that stores the return value of the anonymous function, but I think that’s not the correct way to interpret it Any explanation would be really helpful.
(Some of the terminologies I used above might be wrong with respect to scala/functional programming, kindly bear with it)
Here,
fis a variable that stores a function. It’s really no different from saying any of the following:You can also confirm this with the REPL:
So this is telling you that
fhas the typeInt => Int. Or in other words,fis a function that takes one argument, anInt, and returns anInt.Since
fis a variable, you can call methods on it or pass it as an argument to functions that expect its type: