Skip to content

Instantly share code, notes, and snippets.

@nolawnchairs
Created May 17, 2020 07:07
Show Gist options
  • Save nolawnchairs/27c05799ededf37dfd1daf4fd83ddec0 to your computer and use it in GitHub Desktop.
Save nolawnchairs/27c05799ededf37dfd1daf4fd83ddec0 to your computer and use it in GitHub Desktop.
Typescript analogs to java.util.function interfaces
/**
* Represents a function that accepts one argument and produces a result.
* @template T the parameter type
* @template U the return type
*/
export type Function<T, U> = (value: T) => U
/**
* Represents a function that accepts two arguments and produces a result.
* @template T the first argument type
* @template U the second argument type
* @template R the return type
*/
export type BiFunction<T, U, R> = (value1: T, value2: U) => R
/**
* Represents a supplier of results.
* @template T the return type
*/
export type Supplier<T> = () => T
/**
* Represents an operation that accepts a single input argument
* and returns no result.
* @template T the argument type
*/
export type Consumer<T> = (value: T) => void
/**
* Represents an operation that accepts two input arguments and returns no result.
* @template T the first argument type
* @template U the second argument type
*/
export type BiConsumer<T, U> = (value1: T, value2: U) => void
/**
* Represents an operation on a single operand that produces a
* result of the same type as its operand.
* @template T the type of the parameter and return type
*/
export type UnaryOperator<T> = (value: T) => T
/**
* Represents an operation upon two operands of the same type,
* producing a result of the same type as the operands.
* @template T the type of the parameter and return type
*/
export type BinaryOperator<T> = (value1: T, value2: T) => T
/**
* Represents a predicate (boolean-valued function) of one argument.
* @template T the argument type
*/
export type Predicate<T> = (value: T) => boolean
/**
* Represents a predicate (boolean-valued function) of two arguments.
* @template T the first argument type
* @template U the second argument type
*/
export type BiPredicate<T, U> = (value1: T, value2: U) => boolean
/**
* Represents an operation that compares two operands of the same type
* and produces a number (integer) for comparison purposes
* @template T the argument type
*/
export type Comparator<T> = (value1: T, value2: T) => number
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment