Local Functions


If you understand the Scala function, the local function should be the most simple idea among all other Scala functional concepts.
When we use an Object Oriented approach, it is quite common to create private methods. They are a kind of helper methods, and we make them private because we do not want them to expose to the external world.
Scala offers the same notion using the local functions. You can define functions inside other functions. That's what we call as a Local Function. The local functions are visible only in their enclosing block.
Here is an example.


The getErrors function takes a file name as input, and it returns the error messages. We are using isError as a local Function. Sometimes, people also refer them as Nested Function. The choice is yours, call them Nested or Local, but they are the same thing.
We learned about the function literals in the earlier post. The notion of a local function and the function literals works hand in hand in functional programming. They are the key element of a Higher Order functions that return a function.
Continue reading, and I will cover Higher Order functions in great depth.

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

By Prashant Pandey -



You will also like: