Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Scala, how to pass function with multiple parameter lists as an argument?

def multi_fun(i:Int, s:String)(d:Double) […]

How to pass this function now to another function as an argument, i.e. which parameter type needs to be indicated?

def fun_that_likes_multi_fun(mf:(Int, String)(Double) ⇒ Unit) […]

This would not work, fails even to parse.

like image 244
Majakovskij Avatar asked Apr 05 '16 04:04

Majakovskij


People also ask

How do you provide a function multiple arguments?

The * symbol is used to pass a variable number of arguments to a function. Typically, this syntax is used to avoid the code failing when we don't know how many arguments will be sent to the function.

What does => mean in Scala?

=> is the "function arrow". It is used both in function type signatures as well as anonymous function terms. () => Unit is a shorthand for Function0[Unit] , which is the type of functions which take no arguments and return nothing useful (like void in other languages).

Can a method accept multiple parameters?

Luckily, you can write functions that take in more than one parameter by defining as many parameters as needed, for example: def function_name(data_1, data_2):

What is function currying in Scala?

Currying in Scala is simply a technique or a process of transforming a function. This function takes multiple arguments into a function that takes single argument. It is applied widely in multiple functional languages. Syntax. def function name(argument1, argument2) = operation.


1 Answers

def fun_that_likes_multi_fun(mf:(Int, String)=>Double => Unit)={}

The function you have mentioned is a curried function. Which means the result of applying the first argument list is another function that takes the second argument list to process in entirety. So it is represented as such.

like image 55
Som Bhattacharyya Avatar answered Sep 21 '22 13:09

Som Bhattacharyya