Raj
Raj

Reputation: 4452

Function literal - Need help in understanding a code snippet

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)

Upvotes: 1

Views: 141

Answers (3)

0__
0__

Reputation: 67280

f is a value denoting a function literal.

In the statement, the right-hand-side is a function literal. The left-hand-side binds it to a name which is then called value (the val keyword is similar to let in LISP). Now the function is associated with the symbol f, so you can refer to that function by using this symbol f.

I disagree with the other answers which suggest that f should be called a variable. f is a value, because it is fixed to the right-hand-side term which is determined only once and cannot change. On the contrary a variable, introduced with var, allows you to re-assign values to a symbol:

var f = (i: Int) => i + 1

Where var begins a variable definition, f is the name or symbol of the variable, there could be an optional : ... defining type of the variable (if you leave that out, the type is automatically inferred from the assignment), and = ... defines the value initially assigned to the variable.

So when one says value, don't confuse this with a numeric constant, it is simply an entity that doesn't change. A function can be a value too, because f then always denotes this same identical function, even if you can feed that function with different arguments which yield different results.

Now with the var you can re-assign its right-hand-side:

f(2) // --> 3
f = (i: Int) => i * 2 // assign a new function to the variable f.
f(2) // --> 4

Functional programming is all about avoiding variables (re-assignments).


It is also possible to define a function without assigning it at all (to a value or a variable). The following defines such a function and immediately calls it with argument 4:

{ i: Int => i + 1 } apply 4 // --> 5

although that is seldom useful as a statement per se, but you will see 'plain' functions often when calling a method that expects a function argument. For instance

val s = List(1, 2, 3)
s.map { (i: Int) => i + 1 } // --> List(2, 3, 4)
s.map { _ + 1 }             // equivalent
s.map( _ + 1 )              // equivalent

Upvotes: 0

dhg
dhg

Reputation: 52681

Here, f is a variable that stores a function. It's really no different from saying any of the following:

val a = 4               // `a` is a variable storing an Int
val b = "hi"            // `b` is a variable storing a String
val f = (a:Int) => a+1  // `f` is a variable storing a function

You can also confirm this with the REPL:

scala> val f = (a:Int) => a+1
f: Int => Int = <function1>

So this is telling you that f has the type Int => Int. Or in other words, f is a function that takes one argument, an Int, and returns an Int.

Since f is a variable, you can call methods on it or pass it as an argument to functions that expect its type:

a + 3  // here I'm calling the `+` method on `a`, which is an Int
f(3)   // here I'm calling the `apply` method on `f`, which is a function `Int => Int`
f(a)   // the function `f` expects an `Int`, which `a` is
(1 to 3).map(f)  // the `map` method expects a function from Int to Int, like `f`

Upvotes: 5

Andrew Conner
Andrew Conner

Reputation: 1436

Yes, like dhg said, f is a variable (that can't be changed) that stores a function.

However, there's a subtlety here:

... the first thought that occurs is that f is a variable that stores the return value of the anonymous function

f actually stores the function, not the result. So you can give it different inputs, and get different outputs. So, you can use it like f(7) and then f(5). Functions in Scala are objects, so can be assigned to variables, passed as parameters, etc.

I recently posted about function literals, which may be helpful to you.

Upvotes: 1

Related Questions