Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

README.md

Outline

compose

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

Questions

  • How to compose functions?

constant

Returns the given constant no matter the input.

Type signature

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

Examples

constant(3)("anything");
// ⇒ 3

Questions

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

identity

Always return the given value.

Type signature

<T>(x: T) => T

Examples

identity(5);
// ⇒ 5
identity("test");
// ⇒ "test"

Questions

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

memoize

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);

Questions

  • How to memoize a function?

memoizeShallow

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 });

Questions

  • How to memoize a function with shallow equality?

memoizeWith

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 });

Questions

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

noOp

It does exactly nothing.

Type signature

() => void

Examples

noOp("anything");
// ⇒ undefined

Questions

  • How to create a function that does nothing?

not

Inverts the given function result.

Type signature

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

Examples

not(x > 10)(15);
// ⇒ true

Questions

  • How to invert a boolean function?

pipe

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

Questions

  • How to pipe an argument through a function?

when

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);

Questions

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

whenTrue

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);

Questions

  • How to run a function only if its argument is true?
  • How to execute function only if a variable is true?
Morty Proxy This is a proxified and sanitized view of the page, visit original site.