Function1

scala.Function1
See theFunction1 companion object
trait Function1[-T1, +R] extends AnyRef

A function of 1 parameter.

In the following example, the definition of succ is shorthand, conceptually, for the anonymous class definition anonfun1, 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 between Function1 and scala.PartialFunction is that the latter can specify inputs which it will not handle.

Attributes

Companion
object
Source
Function1.scala
Graph
Supertypes
class Object
trait Matchable
class Any
Known subtypes
trait SetOps[A, CC, C]
trait SetOps[A, CC, C]
trait Set[A]
class AbstractSet[A]
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 ValueSet
trait SortedSet[A]
trait SortedSetOps[A, CC, C]
trait StrictOptimizedSortedSetOps[A, CC, C]
trait StrictOptimizedSetOps[A, CC, C]
trait SetOps[A, CC, C]
class HashSet[A]
class LinkedHashSet[A]
trait Set[A]
class AbstractSet[A]
class BitSet
class TreeSet[A]
trait SortedSet[A]
trait SortedSetOps[A, CC, C]
trait Set[A]
class AbstractSet[A]
trait SortedSet[A]
trait BitSet
trait SortedSetOps[A, CC, C]
trait BitSetOps[C]
trait SortedSetFactoryDefaults[A, CC, WithFilterCC]
trait StrictOptimizedSortedSetOps[A, CC, C]
trait StrictOptimizedSetOps[A, CC, C]
class FromJavaConsumer[T]
class FromJavaFunction[T, R]
class AbstractFunction1[T1, R]
class From <:< To
class From =:= To
class Conversion[T, U]
trait PartialFunction[A, B]
trait MapOps[K, V, CC, C]
trait MapOps[K, V, CC, C]
trait Map[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]
trait SeqMap[K, V]
trait SortedMap[K, V]
trait SortedMapOps[K, V, CC, C]
trait StrictOptimizedSortedMapOps[K, V, CC, C]
trait StrictOptimizedMapOps[K, V, CC, C]
trait MapOps[K, V, CC, C]
class TrieMap[K, V]
class AnyRefMap[K, V]
class CollisionProofHashMap[K, V]
class HashMap[K, V]
class LinkedHashMap[K, V]
class ListMap[K, V]
class LongMap[V]
trait Map[K, V]
trait Map[K, V]
class AbstractMap[K, V]
class WithDefault[K, V]
class WithDefault[K, V]
class OpenHashMap[Key, Value]
class TreeMap[K, V]
trait MultiMap[K, V]
trait SeqMap[K, V]
trait SortedMap[K, V]
trait SortedMapOps[K, V, CC, C]
trait Map[K, V]
class AbstractMap[K, V]
trait DefaultMap[K, V]
trait SeqMap[K, V]
trait SortedMap[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 StrictOptimizedMapOps[K, V, CC, C]
trait Seq[A]
trait Seq[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
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
class Vector[A]
trait IndexedSeq[A]
trait LinearSeq[A]
trait Seq[A]
class AbstractSeq[A]
class AbstractBuffer[A]
class ArrayBuffer[A]
class ArrayDeque[A]
class Queue[A]
class Stack[A]
class ListBuffer[A]
class UnrolledBuffer[T]
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 Buffer[A]
trait IndexedBuffer[A]
trait IndexedSeq[T]
class Accumulator[A, CC, C]
class AnyAccumulator[A]
class AbstractSeq[A]
trait IndexedSeq[A]
trait LinearSeq[A]
Show all
Self type
T1 => R

Members list

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.

Attributes

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

Attributes

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

Attributes

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.

Attributes

Returns

a string representation of the object.

Definition Classes
Any
Source
Function1.scala
def unlift: PartialFunction[A, B]
Implicitly added by UnliftOps

Converts an optional function to a partial function.

Converts an optional function to a partial function.

Attributes

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