In Scala, you can use anonymous functions with map method. And here we will discuss the usage of multiple line anonymous functions with map. Therefore, whenever you see that your algorithm is becoming lengthy then instead of utilizing an anonymous function you can firstly define the method and then you can pass it into the map or use it with the map function.
Now, lets discuss some examples below.
In this example the method addTwo has a parameter of type Char as a string is a collection of Characters and when we call this addTwo method with the map function on the stated String then this map will work on one Char at a time.
List(2, 6, 8)
This is same as above example but here we have defined an anonymous function for multiplication and it multiplies all the integers of the list with the stated integer.
- Anonymous Functions in Scala
- Scala | Nested Functions
- Partial Functions in Scala
- Scala | Functions - Basics
- Scala | Functions Call-by-Name
- Currying Functions in Scala with Examples
- Scala | Partially Applied functions
- Higher Order Functions in Scala
- Scala Map last() method with example
- Scala Set max() method with example
- Scala Int max() method with example
- Scala | map() method
- Scala Int &(x: Int) method with example
- Scala Int min() method with example
- Scala Int >>(x: Int) method with example
- Scala Set &~() method with example
- Scala Int to(end: Int) method with example
- Scala Set +() method with example
- Scala Int +(x: Int) method with example
- Scala Set contains() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.