Skip to main content

Function: map()

Call Signature

map<T, U>(option, fn): Option<U>

Defined in: option/map/index.ts:35

Maps over the Some value of an Option.

If the Option is Some, applies the function to its value and returns a new Some. If the Option is None, returns None unchanged.

This is the functor map operation for Option.

Type Parameters

T

T

U

U

Parameters

option

Option<T>

The Option to map over

fn

(value) => U

Function to transform the Some value

Returns

Option<U>

A new Option with the transformed value

Example

// Data-first
map(some(5), x => x * 2) // => Some(10)
map(none(), x => x * 2) // => None

// Data-last (in pipe)
pipe(
some(5),
map(x => x * 2),
map(x => x + 1)
) // => Some(11)

See

  • flatMap - for chaining Option-returning functions
  • filter - for conditionally keeping values

Call Signature

map<T, U>(fn): (option) => Option<U>

Defined in: option/map/index.ts:36

Maps over the Some value of an Option.

If the Option is Some, applies the function to its value and returns a new Some. If the Option is None, returns None unchanged.

This is the functor map operation for Option.

Type Parameters

T

T

U

U

Parameters

fn

(value) => U

Function to transform the Some value

Returns

A new Option with the transformed value

(option): Option<U>

Parameters

option

Option<T>

Returns

Option<U>

Example

// Data-first
map(some(5), x => x * 2) // => Some(10)
map(none(), x => x * 2) // => None

// Data-last (in pipe)
pipe(
some(5),
map(x => x * 2),
map(x => x + 1)
) // => Some(11)

See

  • flatMap - for chaining Option-returning functions
  • filter - for conditionally keeping values