2

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?

2 Answers 2

4

Try this:

  val myMap = Map[String, (String, Int) => Boolean](
    "Test" -> ((s, i)  => true)
  )
Sign up to request clarification or add additional context in comments.

1 Comment

presto! thanks. I was putting curly brackets and defining the inputs
0

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>)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.