bharal
bharal

Reputation: 16164

Scala - return a function from a map

In scala, how would i declare & instantiate a map that returns a function (for the sake of argument? A function that accepts two variables, one a String, one an Int)?

I am envisioning:

val myMap = Map[String, (String,Int)=>Boolean](
    WHAT GOES HERE???
)

Let's just map the string "a" to this cool function. I don't care much about what the function does - returns true, perhaps?

Upvotes: 2

Views: 132

Answers (2)

user1980875
user1980875

Reputation:

you can do something like this:

val map = Map("key" -> { (str: String, n: Int) =>
  str.indexOf(n) == -1
})

result:

> map: scala.collection.immutable.Map[String,(String, Int) => Boolean] = Map(key - <function2>)

Upvotes: 0

Noah
Noah

Reputation: 13959

Try this:

  val myMap = Map[String, (String, Int) => Boolean](
    "Test" -> ((s, i)  => true)
  )

Upvotes: 4

Related Questions