Package io.vavr

Interface PartialFunction<T,R>

Type Parameters:
T - type of the function input, called domain of the function
R - type of the function output, called codomain of the function
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 is not necessarily defined for all input values of type T. The caller is responsible for calling the method isDefinedAt() before this function is applied to the value.

If the function is not defined for a specific value, apply() may produce an arbitrary result. More specifically it is not guaranteed that the function will throw an exception.

If the function is defined for a specific value, apply() may still throw an exception.

  • Field Details

  • Method Details

    • unlift

      static <T, R> PartialFunction<T,R> unlift(Function<? super T,? extends Option<? extends R>> totalFunction)
      Unlifts a totalFunction that returns an Option result into a partial function. The total function should be side effect free because it might be invoked twice: when checking if the unlifted partial function is defined at a value and when applying the partial function to a value.
      Type Parameters:
      T - type of the function input, called domain of the function
      R - type of the function output, called codomain of the function
      Parameters:
      totalFunction - the function returning an Option result.
      Returns:
      a partial function that is not necessarily defined for all input values of type T.
    • getIfDefined

      static <T, V extends Value<T>> PartialFunction<V,T> getIfDefined()
      Factory method for creating a partial function that maps a given Value to its underlying value. The partial function is defined for an input Value if and only if the input Value is not empty. If the input Value is not empty, the partial function will return the underlying value of the input Value.
      Type Parameters:
      T - type of the underlying value of the input Value.
      V - type of the function input, called domain of the function
      Returns:
      a partial function that maps a 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 argument
      Returns:
      the result of function application
    • isDefinedAt

      boolean isDefinedAt(T value)
      Tests if a value is contained in the function's domain.
      Parameters:
      value - a potential function argument
      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.
      Returns:
      a function that applies arguments to this function and returns Some(result) if the function is defined for the given arguments, and None otherwise.