Nick Radov
Nick Radov

Reputation: 431

Proper Scala syntax to turn higher-order function into anonymous function?

I would like to eliminate the inverse function in the example below and just create an anonymous function directly in the call to bar. Can anyone suggest the correct syntax? I've tried a few variations but can't get anything to compile.

object Test {

  def foo(p: Int => Boolean): Boolean = {
    def inverse(p: Int => Boolean): Int => Boolean = {
      e: Int => !p(e)
    }

    bar(inverse(p))
  }

  def bar(p: Int => Boolean): Boolean = true

}

Upvotes: 3

Views: 952

Answers (1)

Jens Schauder
Jens Schauder

Reputation: 81988

This should work

bar(!p(_))

stackoverflow says this answer is too short.

Upvotes: 13

Related Questions