Skip to content

Latest commit

 

History

History
117 lines (94 loc) · 3.59 KB

File metadata and controls

117 lines (94 loc) · 3.59 KB

Function

The @perfective/common/function package provides types and functions for the functional programming style. As functions with more than three arguments are considered a code smell, this package only declares the Nullary, Unary, Binary, and Ternary types. The functions of higher arity is unlikely to be added to the package.

Type Guards

  • isFunction<T>(value: Function | T): value is Function — returns true if a given value is a Function. Otherwise, returns false.

  • isNotFunction<T>(value: Function | T): value is T — returns true if a given value is not a Function. Otherwise, returns false.

Nullary functions

  • Nullary<T> — a function without arguments:

    • isNullary<F extends Function>(f: F): boolean — returns true if a given function f has length 0 (excluding a variadic argument). Otherwise, returns false.

    • constant<T>(value: T): Nullary<T> — creates a nullary function that returns a given value.

  • Void — a procedure without arguments:

    • naught(): void — an empty function to be passed as a callback when a no-op behavior is required.

  • Value<T> = T | Nullary<T> — a value itself or a nullary function that returns a value. (e.g. for lazy evaluation).

    • valueOf<T>(value: Value<T>): T — When given a nullary function, evaluates the function and returns the result. Otherwise, returns the given value.

Unary functions

  • Unary<X, V> — a function with one argument:

    • isUnary<F extends Function>(f: F): boolean — returns true if a given function f has length 1 (excluding a variadic argument).

    • same<T>(value: T): T — the identity function. Returns a given value.

  • UnaryVoid<T> = (value: T) ⇒ void — a procedure with one argument.

Binary functions

  • Binary<X, Y, V> — a function with two arguments.

    • isBinary<F extends Function>(f: F): boolean — Returns true if a given function f has length 2 (excluding a variadic argument). Otherwise, returns false.

Ternary functions

  • Ternary<X, Y, Z, V> — a function with three arguments.

    • isTernary<F extends Function>(f: F): boolean — returns true if a given function f has length 3 (excluding a variadic argument). Otherwise, returns false.

Length

Length type defines a kind of objects that have "length" (arrays, strings, etc).

interface Length {
    length: number;
}
  • Functions

    • length<L extends Length>(value: L): number — returns the length property of a given value.

  • Predicates

    • hasLength<L extends Length>(length: number): Predicate<L> — returns a predicate that checks if the input value has a given length.

    • isNotEmpty<L extends Length>(value: L): boolean — returns true if a given value has non-positive length.

    • isEmpty<L extends Length>(value: L): boolean — returns true if a given value has length greater than 0.

  • Reducers

    These functions can be used as a callback for Array.prototype.reduce.

    • toShortest<T extends Length>(shortest: T, value: T): T — returns the shortest of two given values.

    • toLongest<T extends Length>(longest: T, array: T): T — returns the longest of two given values.

Arguments

  • BiMap<T1, U1, T2, U2> = [Unary<T1, U1>, Unary<T2, U2>] — a pair of callbacks to map both arguments of a given function at the same time.

  • BiFold<T1, T2, U> = [Unary<T1, U>, Unary<T2, U>] — a pair of unary callbacks to map both arguments T1 and T2 of a function into the same type U.