function

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

Type signature

(...fs: ((x: any) => any)[]) => (x: any) => any

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

(x: any) => any

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

(x: any) => any

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

(f: (...xs: any[]) => any) => (...args: any[]) => any

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

(f: (...xs: any[]) => any) => (...args: any[]) => any

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

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

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: any[]) => any
) => (...args: any[]) => 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

(...fs: ((x: any) => any)[]) => (x: any) => any

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: any[]) => boolean
) => (action: (...xs: any[]) => any) => (...args: any[]) => any

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: any[]) => any
) => (...args: any[]) => any

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