A function of 1 parameter.
In the following example, the definition ofsucc is shorthand, conceptually, for the anonymous class definitionanonfun1, although the implementation details of how the function value is constructed may differ:
object Main extends App { val succ = (x: Int) => x + 1 val anonfun1 = new Function1[Int, Int] { def apply(x: Int): Int = x + 1 } assert(succ(0) == anonfun1(0))}Note that the difference betweenFunction1 andscala.PartialFunction is that the latter can specify inputs which it will not handle.
Apply the body of this function to the argument.
Apply the body of this function to the argument.
the result of function application.
Composes two instances ofFunction1 in a newFunction1, with this function applied first.
Composes two instances ofFunction1 in a newFunction1, with this function applied first.
the result type of functiong
a function R => A
a new functionf such thatf(x) == g(apply(x))
Composes two instances ofFunction1 in a newFunction1, with this function applied last.
Composes two instances ofFunction1 in a newFunction1, with this function applied last.
the type to which functiong can be applied
a function A => T1
a new functionf such thatf(x) == apply(g(x))
Returns a string representation of the object.
Returns a string representation of the object.
The default representation is platform dependent.
a string representation of the object.
Converts an optional function to a partial function.
Converts an optional function to a partial function.
UnlikeFunction.unlift, thisUnliftOps.unlift method can be used in extractors.
val of: Int => Option[String] = { i => if (i == 2) { Some("matched by an optional function") } else { None }}util.Random.nextInt(4) match { case of.unlift(m) => // Convert an optional function to a pattern println(m) case _ => println("Not matched")}