Scala Function Literals
Let's start with the meaning of the term literal in computer science.
A literal is a notation for representing a fixed value in source code.
We have been using literals for a long time to assign fixed values to different data types. The code below shows some examples.
The first line of the above codes uses a string literal. The second line uses an
integer literal. Now, let's talk about the
functional programming paradigm.
One of the basic ideas of functional programming is that the functions are the
first
class citizens. They are like values. So, whatever you can do with values, you
should
be able to do it with the functions. Like any other value, we can assign functions
to
a variable. We can pass them as a parameter, and we can return them from a higher
order
function. We can also create values using a literal as shown in the above example.
Can we create a function using a literal?
If we can create a string value using a string literal, we must be able to
create
a function value using a function literal.
Function literal Syntax
Here is the syntax for creating a function literal.
You can compare it with the standard function syntax that we start with the keyword
def.
You will notice three differences.
- We do not have a function identifier (function name)
- A right arrow symbol replaces the equal to sign
- The return type moved to the end
It is exactly same as we created a value earlier. Instead of using a string literal, we use a function literal. In the example above, I skipped the return type for the function value because Scala automatically infers the return type. However, you can enforce it by putting it in the end as shown in the later part of the code. You might be wondering that when we created a long value, we specified the type annotation in the front. Can we do the same with function literals? Refer the below code.
The above example shows that we can specify the function type in the front. However, you must remember the difference between the front place and the tail position.
- The tail position defines a return type
- The front place defines the function type
The function type includes the input type and the return type both separated by a
=> symbol. The input type comes before the
=> symbol, and hence we have an
Int there because the function takes a single integer as input.
The return type comes after the
=> symbol, and hence we have an
Int there as well because the function returns an integer.
However, you do not need to specify the function type or the return type
because
Scala should be able to infer that automatically.
Here is another example.
The
myFun is a val. The code before the
= symbol is the function type and the code after the
= symbol is the function literal. However, the recommendation is to avoid
the
optional parts.
So we remove everything starting with the
: and go up to the
= symbol. We also removed the return type from the tail position.
We learned some other optional parts in an earlier article such as a semicolon,
return
keyword, curly braces. All of those rules are also applicable to function literals.
Hence,
we removed the curly braces as well.
That is all about function literals. Some people call them anonymous functions,
and
others refer them as lambda functions. However, these are three names for the same
thing
in Scala. You might wonder with the following question.
Where do we use function literals?
Most of the time, you will use function literal with higher order functions. I
mean,
either you will be passing it to a higher order function or returning it. I have
covered
it with suitable examples in a dedicated section about Higher Order functions.
Continue reading for more concepts.
Read More
Basics of Scala functions | Function Literals in Scala | Function values | Local Functions | Variable length argument | Default values and named arguments | Scala Placeholder syntax | Higher Order functions | Partially applied functions | Function currying