Interface PartialFunction<T,R>

Type Parameters:
T - the type of the function input (the domain)
R - the type of the function output (the codomain)
All Superinterfaces:
Function<T,R>, Function1<T,R>, Serializable
All Known Subinterfaces:
API.Match.Case<T,R>, API.Match.Pattern<T,R>, IndexedSeq<T>, LinearSeq<T>, List<T>, Map<K,V>, Multimap<K,V>, Seq<T>, SortedMap<K,V>, SortedMultimap<K,V>, Stream<T>
All Known Implementing Classes:
AbstractMultimap, API.Match.Case0, API.Match.Case1, API.Match.Case2, API.Match.Case3, API.Match.Case4, API.Match.Case5, API.Match.Case6, API.Match.Case7, API.Match.Case8, API.Match.Pattern0, API.Match.Pattern1, API.Match.Pattern2, API.Match.Pattern3, API.Match.Pattern4, API.Match.Pattern5, API.Match.Pattern6, API.Match.Pattern7, API.Match.Pattern8, Array, CharSeq, HashMap, HashMultimap, LinkedHashMap, LinkedHashMultimap, List.Cons, List.Nil, Queue, Stream.Cons, Stream.Empty, StreamModule.AppendElements, StreamModule.ConsImpl, TreeMap, TreeMultimap, Vector

public interface PartialFunction<T,R> extends Function1<T,R>
Represents a partial function T -> R that may not be defined for all input values of type T. The caller is responsible for checking isDefinedAt(Object) before applying this function.

If the function is not defined for a given value, apply(Object) may produce an arbitrary result. There is no guarantee that an exception will be thrown in this case.

If the function is defined for a given value, apply(Object) may still throw an exception during execution.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final long
    The serial version UID for serialization.
  • Method Summary

    Modifier and Type
    Method
    Description
    apply(T t)
    Applies this function to the given argument and returns the result.
    static <T, V extends Value<T>>
    PartialFunction<V,T>
    Creates a partial function that maps a given Value to its underlying value.
    boolean
    isDefinedAt(T value)
    Tests whether a value is contained in the function's domain.
    default Function1<T, Option<R>>
    Lifts this partial function into a total function that returns an Option result.
    static <T,R> PartialFunction<T,R>
    unlift(@NonNull Function<? super T, ? extends Option<? extends R>> totalFunction)
    Converts (or "unlifts") a totalFunction that returns an Option into a partial function.
  • Field Details

    • serialVersionUID

      static final long serialVersionUID
      The serial version UID for serialization.
      See Also:
  • Method Details

    • unlift

      static <T,R> PartialFunction<T,R> unlift(@NonNull Function<? super T, ? extends Option<? extends R>> totalFunction)
      Converts (or "unlifts") a totalFunction that returns an Option into a partial function.

      The provided totalFunction should be side-effect-free, because it may be invoked twice: once when checking if the resulting partial function is defined at a value, and once when applying the partial function to that value.

      Type Parameters:
      T - the type of the function input (the domain)
      R - the type of the function output (the codomain)
      Parameters:
      totalFunction - the function returning an Option result
      Returns:
      a partial function that is defined only for inputs for which the totalFunction returns a defined Option
    • getIfDefined

      static <T, V extends Value<T>> PartialFunction<V,T> getIfDefined()
      Creates a partial function that maps a given Value to its underlying value.

      The resulting partial function is defined for an input Value if and only if the Value is not empty. For defined inputs, the partial function returns the underlying value contained in the Value.

      Type Parameters:
      T - the type of the underlying value
      V - the type of the input Value (the domain of the function)
      Returns:
      a partial function that maps a non-empty Value to its underlying value
    • apply

      R apply(T t)
      Applies this function to the given argument and returns the result.
      Specified by:
      apply in interface Function<T,R>
      Specified by:
      apply in interface Function1<T,R>
      Parameters:
      t - the input argument
      Returns:
      the result of applying this function to the input
    • isDefinedAt

      boolean isDefinedAt(T value)
      Tests whether a value is contained in the function's domain.
      Parameters:
      value - a potential input to the function
      Returns:
      true if the given value is contained in the function's domain, false otherwise
    • lift

      default Function1<T, Option<R>> lift()
      Lifts this partial function into a total function that returns an Option result.

      The resulting function applies an argument to this partial function and returns Some(result) if the function is defined for that argument, or None if it is not defined.

      Returns:
      a total function that returns an Option containing the result if defined, or None otherwise