sdk-common / com.tomtom.online.sdk.common.functional / Function2

Function2

@FunctionalInterface interface Function2<A1 : Any!, A2 : Any!, R : Any!> : Serializable

Represents a function with two arguments. Due to Java semantics and how functions are represented on the JVM, using Function2 might be shorter then using Function1 that returns another Function1 as a result. Still it's possible to convert Function2 into such form.

final Function2<String, String, Integer> myFunc = new Function2<String, String, Integer>() {
  // function implementation ...
  }
 
  final Function1<String, Function1<String, Integer>> myCurriedFunc = myFunc.curried();
 
  
  
As you see in the code example above, Function2 is a compromise between notation length and usage capabilities.

See Also
Function1

Properties

serialVersionUID

static val serialVersionUID: Long

Functions

andThen

open fun <R2 : Any!> andThen(after: ((in R) -> out R2)!): ((A1, A2) -> R2)!

Returns a composed function that first applies this function to the given two inputs and then applies the function passed as after to the result.

apply

abstract fun apply(arg1: A1, arg2: A2): R

Applies this function to the given arguments.

applyFirst

open fun applyFirst(arg1: A1): ((A2) -> R)!

Applies this function partially to first argument.

applySecond

open fun applySecond(arg2: A2): ((A1) -> R)!

Applies this function partially to second argument.

constant

open static fun <T1 : Any!, T2 : Any!, R : Any!> constant(value: R): ((T1, T2) -> R)!

Returns a function that always returns the constant regardless of the passed arguments.

curried

open fun curried(): ((A1) -> ((A2) -> R)!)!

Returns a curried version of this function. In functional programming currying is a an operation that transforms a function that takes several arguments and returns a result (f: (P x Q) -> R) into a function that takes one argument and returns a function (g: P -> (Q -> R)). This allows to reason about functions with multiple arguments in simpler theoretical models which provide only one argument.

flipped

open fun flipped(): ((A2, A1) -> R)!

Returns a function which arguments are flipped. Especially useful if we have an existing API which accepts a Function2 that differs only with the order of arguments with our implementation (and we cannot change it).

liftTry

open static fun <T1 : Any!, T2 : Any!, R : Any!> liftTry(partial: ((T1, T2) -> R)!): ((T1, T2) -> Try<R>!)!

Lifts given partial into a total function that returns a Try result.