Functions

The Functions module provides a collection of utility functions for functional programming in TypeScript. It includes common functional programming primitives, function composition utilities, and helper functions for common operations.

Function Evaluation

invoke

This function is a syntax sugar for IIFEs.

Immediately invokes a function and returns its return value.

import { invoke } from 'funkcia/functions';

declare const shape: Shape;

const humanReadableShape = invoke(() => {
  switch (shape.kind) {
    case 'CIRCLE':
      return 'Circle';
    case 'SQUARE':
      return 'Square';
    default:
      const invalidKind: never = shape.kind;
      throw new Error(`Invalid shape: ${invalidKind}`);
  }
});

lazyCompute

Lazily computes a value by invoking a function. The value is computed only once when first accessed.

Functional Primitives

identity

Returns the provided value unchanged.

noop

A function that does nothing and returns nothing.

Constant Functions

always

Returns a function that will always return the provided value.

alwaysNull

Returns null.

alwaysUndefined

Returns undefined.

alwaysVoid

Returns void.

ignore

Returns never.

alwaysTrue

Returns true.

alwaysFalse

Returns false.

Type Utilities

coerce

Returns the provided value coerced to the desired type.

compose

Composes two or more functions into a single function, from left to right.

pipe

Pipes a value through a series of functions, from left to right.

Last updated

Was this helpful?