I want to create a simple Map from math operators to the relevant functions:
var ops = Map("+" -> +, "-" -> -)
How would I do this in Scala?
If you want the functions to be curried, the following is probably the most concise way to do it.
scala> val ops: Map[String, Int => Int => Int] = Map(
| "+" -> (x => y => x + y),
| "-" -> (x => y => x - y)
| )
ops: Map[String,Int => Int => Int] = Map(+ -> <function1>, - -> <function1>)
This map however is only limited to Int
s. If you want generic operations, you will have to use Numeric
context bound.
scala> def ops[N : Numeric]: Map[String, N => N => N] = {
| import Numeric.Implicits._
| Map(
| "+" -> (x => y => x + y),
| "-" -> (x => y => x - y)
| )
| }
ops: [N](implicit evidence$1: Numeric[N])Map[String,N => N => N]
A major caveat with this approach is that a map gets created every time you call ops
.
val ops = Map("+" -> ((_: Int) + (_: Int)), "-" -> ((_: Int) - (_:Int)))
or
val ops = Map[String, (Int, Int) => Int]("+" -> (_+_), "-" -> (_-_))
or even, for actual currying,
val ops = Map("+" -> ((_: Int) + (_: Int)).curried, "-" -> ((_: Int) - (_:Int)).curried)
These functions are all bind to Int
. Well, Scala is not a point-free programming language, it's an object oriented one, and one in which there's no superclass common to all numeric types. Anyway, if you object to that, then you have an entirely different problem, which was asked and answered many times here on Stack Overflow (in fact, it was my first Scala question, iirc).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With