Function1

trait Function1[-T1, +R] extends AnyRef

A function of 1 parameter.

In the following example, the definition of succ is a shorthand for the anonymous class definition anonfun1:

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 between Function1 and scala.PartialFunction is that the latter can specify inputs which it will not handle.

Companion:
object
Source:
Function1.scala
class Object
trait Matchable
class Any
class From <:< To
class From =:= To
class Conversion[T, U]
trait PartialFunction[A, B]
trait MapOps[K, V, CC, C]
trait Map[K, V]
class AbstractMap[K, V]
class AbstractMap[K, V]
class HashMap[K, V]
class IntMap[T]
class ListMap[K, V]
class LongMap[T]
class Map1[K, V]
class Map2[K, V]
class Map3[K, V]
class Map4[K, V]
class WithDefault[K, V]
class WithDefault[K, V]
class TreeMap[K, V]
class TreeSeqMap[K, V]
class VectorMap[K, V]
class AbstractMap[K, V]
class TrieMap[K, V]
class AnyRefMap[K, V]
class HashMap[K, V]
class LinkedHashMap[K, V]
class ListMap[K, V]
class LongMap[V]
class WithDefault[K, V]
class WithDefault[K, V]
class OpenHashMap[Key, Value]
class TreeMap[K, V]
trait DefaultMap[K, V]
trait SeqMap[K, V]
trait SeqMap[K, V]
trait SeqMap[K, V]
trait SortedMap[K, V]
trait SortedMap[K, V]
trait SortedMap[K, V]
trait Map[K, V]
trait Map[K, V]
trait Map[K, V]
trait MultiMap[K, V]
trait MapFactoryDefaults[K, V, CC, WithFilterCC]
class WeakHashMap[K, V]
trait MapView[K, V]
class AbstractMapView[K, V]
class Filter[K, V]
class FilterKeys[K, V]
class Id[K, V]
class MapValues[K, V, W]
class TapEach[K, V, U]
trait SortedMapFactoryDefaults[K, V, CC, WithFilterCC, UnsortedCC]
trait SortedMapOps[K, V, CC, C]
trait StrictOptimizedSortedMapOps[K, V, CC, C]
trait StrictOptimizedSortedMapOps[K, V, CC, C]
trait SortedMapOps[K, V, CC, C]
trait SortedMapOps[K, V, CC, C]
trait StrictOptimizedMapOps[K, V, CC, C]
trait StrictOptimizedMapOps[K, V, CC, C]
trait MapOps[K, V, CC, C]
trait MapOps[K, V, CC, C]
trait Seq[A]
class AbstractSeq[A]
class AbstractSeq[A]
class ArraySeq[A]
class ofBoolean
class ofByte
class ofChar
class ofDouble
class ofFloat
class ofInt
class ofLong
class ofRef[T]
class ofShort
class ofUnit
class LazyList[A]
class List[A]
class ::[A]
object Nil.type
class NumericRange[T]
class Exclusive[T]
class Inclusive[T]
class Queue[A]
class Range
class Exclusive
class Inclusive
class Stream[A]
class Cons[A]
object Empty.type
class Vector[A]
class AbstractSeq[A]
class ArrayBuffer[A]
class ArrayDeque[A]
class Queue[A]
class Stack[A]
class ListBuffer[A]
class ArraySeq[T]
class ofBoolean
class ofByte
class ofChar
class ofDouble
class ofFloat
class ofInt
class ofLong
class ofRef[T]
class ofShort
class ofUnit
trait IndexedSeq[A]
trait IndexedSeq[A]
trait IndexedSeq[T]
trait IndexedBuffer[A]
trait LinearSeq[A]
trait LinearSeq[A]
trait Seq[A]
trait Seq[A]
trait Buffer[A]
class Accumulator[A, CC, C]
trait SetOps[A, CC, C]
trait Set[A]
class AbstractSet[A]
class AbstractSet[A]
class ValueSet
class BitSet
class BitSet1
class BitSet2
class BitSetN
class HashSet[A]
class ListSet[A]
class Set1[A]
class Set2[A]
class Set3[A]
class Set4[A]
class TreeSet[A]
class AbstractSet[A]
class BitSet
class HashSet[A]
class LinkedHashSet[A]
class TreeSet[A]
trait SortedSet[A]
trait BitSet
trait SortedSet[A]
trait SortedSet[A]
trait Set[A]
trait Set[A]
trait SortedSetOps[A, CC, C]
trait BitSetOps[C]
trait SortedSetFactoryDefaults[A, CC, WithFilterCC]
trait SortedSetOps[A, CC, C]
trait SortedSetOps[A, CC, C]
trait StrictOptimizedSetOps[A, CC, C]
trait StrictOptimizedSetOps[A, CC, C]
trait SetOps[A, CC, C]
trait SetOps[A, CC, C]
class FromJavaFunction[T, R]
class AbstractFunction1[T1, R]
T1 => R

Value members

Abstract methods

def apply(v1: T1): R

Apply the body of this function to the argument.

Apply the body of this function to the argument.

Returns:

the result of function application.

Source:
Function1.scala

Concrete methods

def andThen[A](g: R => A): T1 => A

Composes two instances of Function1 in a new Function1, with this function applied first.

Composes two instances of Function1 in a new Function1, with this function applied first.

Type parameters:
A

the result type of function g

Value parameters:
g

a function R => A

Returns:

a new function f such that f(x) == g(apply(x))

Source:
Function1.scala
def compose[A](g: A => T1): A => R

Composes two instances of Function1 in a new Function1, with this function applied last.

Composes two instances of Function1 in a new Function1, with this function applied last.

Type parameters:
A

the type to which function g can be applied

Value parameters:
g

a function A => T1

Returns:

a new function f such that f(x) == apply(g(x))

Source:
Function1.scala
override def toString(): String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Returns:

a string representation of the object.

Definition Classes
Source:
Function1.scala
Implicitly added by UnliftOps

Converts an optional function to a partial function.

Converts an optional function to a partial function.

Example:

Unlike Function.unlift, this UnliftOps.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")
}
Source:
Function1.scala