scala function map
Here’s a cool way to use anonomous function blocks as Scala Map
values.
Use case: I need a library of transformations for user input living in memory and I want to match on User Input.
case object Add
case object Subtract
val functionLibrary = Map(
Add -> { (x: Int, y: Int) => x + y },
Subtract -> { (x: Int, y: Int) => x - y }
)
functionLibrary(Add)(10,20)
functionLibrary(Subtract)(10,20)
I like using case objects
in this way as it leverages the Scala type system. case objects
also have a handy toString
built in.