Skip to content

True Myth / maybe / inspect

Function: inspect()

Call Signature

inspect<T>(fn, maybe): Maybe<T>

Run a side effect with the wrapped value without modifying the Maybe.

This is useful for performing actions like logging, debugging, or other “side effects” external to the wrapped value. (Note: You should never mutate the value in the callback. Doing so will be extremely surprising to callers.) The function is only called if the Maybe is a Just, and the original Maybe is returned unchanged for further operations.

ts
import * as maybe from 'true-myth/maybe';

const log = (value: unknown) => console.log(value);
const double = (value: number) => value * 2;

// Logs `42` then `84`, and returns `Just(84)`.
const aJust = maybe.just(42);
maybe.inspect(
  maybe.map(
    double,
    maybe.inspect(
      log,
      aJust
    )
  )
);

// Does not log anything, and returns `Nothing`.
const aNothing = maybe.nothing<number>();
maybe.inspect(
  maybe.map(
    double,
    maybe.inspect(
      log,
      aNothing
    )
  )
);

Type Parameters

T

T extends object

The type of the wrapped value

Parameters

fn

(value) => void

The function to call with the wrapped value (only called for Just)

maybe

Maybe<T>

The Maybe to inspect

Returns

Maybe<T>

The original Maybe, unchanged

Call Signature

inspect<T>(fn): (maybe) => Maybe<T>

Run a side effect with the wrapped value without modifying the Maybe.

This is useful for performing actions like logging, debugging, or other “side effects” external to the wrapped value. (Note: You should never mutate the value in the callback. Doing so will be extremely surprising to callers.) The function is only called if the Maybe is a Just, and the original Maybe is returned unchanged for further operations.

ts
import * as maybe from 'true-myth/maybe';

const log = (value: unknown) => console.log(value);
const double = (value: number) => value * 2;

// Logs `42` then `84`, and returns `Just(84)`.
const aJust = maybe.just(42);
maybe.inspect(
  maybe.map(
    double,
    maybe.inspect(
      log,
      aJust
    )
  )
);

// Does not log anything, and returns `Nothing`.
const aNothing = maybe.nothing<number>();
maybe.inspect(
  maybe.map(
    double,
    maybe.inspect(
      log,
      aNothing
    )
  )
);

Type Parameters

T

T extends object

The type of the wrapped value

Parameters

fn

(value) => void

The function to call with the wrapped value (only called for Just)

Returns

The original Maybe, unchanged

(maybe): Maybe<T>

Parameters

maybe

Maybe<T>

Returns

Maybe<T>