MOCKSTACKS
EN
Questions And Answers

More Tutorials









Scala Functions

Examples

Anonymous Functions


Anonymous functions are functions that are defined but not assigned a name.

The following is an anonymous function that takes in two integers and returns the sum.

(x: Int, y: Int) => x + y

The resultant expression can be assigned to a val:

val sum = (x: Int, y: Int) => x + y

Anonymous functions are primarily used as arguments to other functions. For instance, the map function on a collection expects another function as its argument:

// Returns Seq("FOO", "BAR", "QUX")
Seq("Foo", "Bar", "Qux").map((x: String) => x.toUpperCase)

The types of the arguments of the anonymous function can be omitted: the types are inferred automatically:

Seq("Foo", "Bar", "Qux").map((x) => x.toUpperCase)

If there is just one argument, the parentheses around that argument can be omitted:

Seq("Foo", "Bar", "Qux").map(x => x.toUpperCase)

Underscores shorthand


There is an even shorter syntax that doesn't require names for the arguments. The above snippet can be written:

Seq("Foo", "Bar", "Qux").map(_.toUpperCase)

_ represents the anonymous function arguments positionally. With an anonymous function that has multiple parameters, each occurrence of _ will refer to a different argument. For instance, the two following expressions are equivalent:

// Returns "FooBarQux" in both cases
Seq("Foo", "Bar", "Qux").reduce((s1, s2) => s1 + s2)
Seq("Foo", "Bar", "Qux").reduce(_ + _)

When using this shorthand, any argument represented by the positional _ can only be referenced a single time and in the same order.

Conclusion

In this page (written and validated by ) you learned about Scala Functions . What's Next? If you are interested in completing Scala tutorial, your next topic will be learning about: Scala Futures.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.