Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala, Passing an operator as argument of a function

Tags:

scala

I have this code:

for( i <- 0 to 8){
  ((numbers(i) - i)/3).abs + ((numbers(i) - i)%3).abs
}

and I would like to do, as the title says, something like this

for( i <- 0 to 8){
  by3(numbers(i), i, /) + by3(numbers(i), i, %)
}

def by3(a: Int, b: Int, op: Int => Int) = ((a - b) op 3).abs

and probably also use a partially applied function for it.. but by now this would be possible to achieve? How?

like image 413
LowFieldTheory Avatar asked Aug 31 '13 20:08

LowFieldTheory


People also ask

Can we pass operator as argument?

You can't directly. You need to pass in a representation of the operator (commonly using an enum) and the function then translates that to the right operator.

What is the meaning of => in Scala?

=> is syntactic sugar for creating instances of functions. Recall that every function in scala is an instance of a class. For example, the type Int => String , is equivalent to the type Function1[Int,String] i.e. a function that takes an argument of type Int and returns a String .

Is Scala pass by reference?

In Java and Scala, certain builtin (a/k/a primitive) types get passed-by-value (e.g. int or Int) automatically, and every user defined type is passed-by-reference (i.e. must manually copy them to pass only their value).


3 Answers

To get rid of the underscores you need to define the functions as values.

val / = (a:Int, b: Int) => a / b
val % = (a:Int, b: Int) => a % b

def by3(a: Int, b: Int, fn: (Int, Int) => Int): Int = fn(a - b, 3).abs

(0 to 8).foreach(i => by3(numbers(i), i, /) + by3(numbers(i), i, %))
like image 174
Karl Avatar answered Nov 13 '22 04:11

Karl


First, you need to correctly define op as a function (specifically, a Function2)

def operate(a: Int, b: Int, op: (Int, Int) => Int ) : Int = (op ((a - b), 3)).abs

Operators in Scala are actually methods: + is a method of Int(and Long, Double, ...) in Scala OO foundation. Then, to pass a operator (method) as a function, you can lift it using the underscore notation: operate(5, 3, _ + _)

like image 30
maasg Avatar answered Nov 13 '22 06:11

maasg


Edited/Update:

Being concise, the shortest way to do it is:

def doStuff(a: Int, b: Int, op: (Int, Int) => Int) = {op(a - b, 3).abs}

doStuff(4,1,_%_)

So you can doStuff(numbers(i), i, _ / _) + doStuff(numbers(i), i, _ % _)

like image 44
tkroman Avatar answered Nov 13 '22 04:11

tkroman