function

Composes multiple functions into a higher-order one. Goes right to left.

Type signature

<T, TResult>(...fs: ((x: T) => T)[]) => (x: T) => T

Examples

compose(
  (x) => x * x,
  (x) => x + 1,
)(3);
// ⇒ 16
Try in REPL

Questions

  • How to compose functions?

TypeScript sourceJavaScript source

Returns the given constant no matter the input.

Type signature

<T>(x: T) => () => T

Examples

constant(3)("anything");
// ⇒ 3
Try in REPL

Questions

  • How to create a function that always returns the same value despite given arguments?

TypeScript sourceJavaScript source

Always return the given value.

Type signature

<T>(x: T) => T

Examples

identity(5);
// ⇒ 5
Try in REPL
identity("test");
// ⇒ "test"
Try in REPL

Questions

  • How to use the identity function?
  • Where and why is identity function useful?

TypeScript sourceJavaScript source

Memoizes the function result so it is not computed for the same parameters. Uses deep equality.

Type signature

<TResult>(f: (...xs: unknown[]) => TResult) => (...args: unknown[]) => TResult

Examples

const f = (x) => {
  console.log(x);
  return x + 1;
};

const memoized = memoize(f);

memoized(5);
memoized(5);
memoized(5);
memoized(3);
Try in REPL

Questions

  • How to memoize a function?

TypeScript sourceJavaScript source

Memoizes the function result so it is not computed for the same parameters. Uses shallow equality.

Type signature

<TResult>(f: (...xs: unknown[]) => TResult) => (...args: unknown[]) => TResult

Examples

const f = ({ x }) => {
  console.log(x);
  return x + 1;
};

const memoized = memoizeShallow(f);

memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 3 });
Try in REPL

Questions

  • How to memoize a function with shallow equality?

TypeScript sourceJavaScript source

Memoizes the function result so it is not computed for the same parameters. Uses the given equality function.

Type signature

<T>(equals: (x: T[], y: T[]) => boolean) => <TResult>(f: (...xs: T[]) => TResult) => (...args: T[]) => TResult

Examples

const f = ({ x }) => {
  console.log(x);
  return x + 1;
};

const memoized = memoizeWith(
  (a, b) => a.x === b.x,
)(f);

memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 5 });
memoized({ x: 3 });
Try in REPL

Questions

  • How to memoize a function with a custom equality function?

TypeScript sourceJavaScript source

It does exactly nothing.

Type signature

() => void

Examples

noOp("anything");
// ⇒ undefined
Try in REPL

Questions

  • How to create a function that does nothing?

TypeScript sourceJavaScript source

Inverts the given function result.

Type signature

(f: (...xs: unknown[]) => unknown) => (...args: unknown[]) => boolean

Examples

not(x > 10)(15);
// ⇒ true
Try in REPL

Questions

  • How to invert a boolean function?

TypeScript sourceJavaScript source

Pipes an input through given functions.

Type signature

<T>(...fs: ((x: T) => T)[]) => (x: T) => T

Examples

pipe(
  (x) => x * x,
  (x) => x + 1,
)(3);
// ⇒ 10
Try in REPL

Questions

  • How to pipe an argument through a function?

TypeScript sourceJavaScript source

Runs the given function only when the condition is met.

Type signature

(predicate: (...xs: unknown[]) => boolean) => (action: (...xs: unknown[]) => unknown) => (...args: unknown[]) => unknown

Examples

when((x) => x > 0)((x) =>
  console.log(x),
)(5);
when((x) => x > 0)((x) =>
  console.log(x),
)(-3);
Try in REPL

Questions

  • How to run a function only when a condition is satisfied?

TypeScript sourceJavaScript source

Runs the given function only when the condition is exactly true.

Type signature

(action: (...xs: unknown[]) => unknown) => (...args: unknown[]) => unknown

Examples

whenTrue((x) => console.log(x))(
  false,
);
when((x) => x > 0)((x) =>
  console.log(x),
)(true);
Try in REPL

Questions

  • How to run a function only if its argument is true?
  • How to execute function only if a variable is true?

TypeScript sourceJavaScript source