![]() Scala provides annotation to check if the recursion is tail recursive or not.Īn example of tail-recursive function is demonstrated below: Conclusion Scala provides tail recursion optimizations to reduce the risk of a stack overflow error caused by recursive functions.Ī tail-recursive function an optimized recursive function whose recursive call is the last operation performed by the function, and no operations need to be saved when the function returns. The partially applied functions are the functions which are not applied on all the arguments defined by the stated function i.e., while invoking a function, we can supply some of the arguments and the left arguments are supplied when required.Īn example of partially applied function is demonstrated below: Tail Recursion Each function returns another function that consumes the following argument.Īn example of curried function is demonstrated below: Partially Applied Functions CurryingĬurrying is the process of converting a function with multiple arguments into a sequence of functions that take one argument. Thus instead of iterating through a collection and manually constructing the values, we are manipulating the existing collections through their high order functions. It can be demonstrated as: The filter methodĪ collection’s filter method takes a function as an argument and returns a new collection containing just the elements for which the function returns true. The flatMapmethod is essentially a combination of the mapmethod being run first followed by the flattenmethod. It can be demonstrated as: The flatMap methodĪ collection’s flatMap method takes a function as an argument, applies it to every element in the collection, and returns a new collection for every element. The map methodĪ collection’s map method takes a function as an argument and returns a new collection containing the applications of the function on every element. There are some classic HOFs we use a lot with collections are map, flatMap, and filter. High order function allows you to create function composition, lambda function or anonymous function, etc.Īn example of passing a function as a parameter in a function is demonstrated below:Īn example of returning a function from a function is demonstrated below: In other words, we can say a function that works with function is called high order function. High Order FunctionsĪ high order function takes other functions as a parameter or returns a function as a result. These two functionalities are provided by high order functions in Scala. Let’s dive into the details of the last two points mentioned above. We can return a function from another function.We can pass one or more functions as parameters to another function. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |