Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Maybe<A>

A Maybe represents an optional value (A) with a convenient chaining syntax and strong type inference. To create one, use the top-level constructors Just, or Nothing.

import {Just, Maybe, Nothing, exists} from 'monadism'

import Cache from '@my/app/Cache'

const getUser = (userId: string): Maybe<string> => {
  const val = Cache.get(userId)

  return exists(val) ? Just(val) : Nothing()
}

Or, use the maybe convenience function:

import {maybe} from 'monadism'

import {User} from '@my/app/Types'

const getUser = (userId: string): Maybe<User> => maybe(Cache.get(userId))

Type parameters

  • A

    The type of optional value the Maybe represents.

Hierarchy

  • Maybe

Implements

Index

Properties

empty

empty: A & object = empty<A>()

Static fromNullable

fromNullable: maybe = Maybe.maybe

An alias for maybe.

Methods

alt

  • The first Maybe that is Just something is returned, otherwise Nothing is returned.

    Just(1).alt(Just(2))   // Just(1)
    Just(2).alt(Nothing()) // Just(2)

    Parameters

    Returns Maybe<A>

apply

  • Unpacks a Maybe for a function from A to B into a function from Maybe<A> to Maybe<B>. Allows functions contained within a Just to transform a value contained within a Just.

    const f = (n: number) => n * 2
    
    Just(2).apply(Just(f)) // Just(4)

    Type parameters

    • B

    Parameters

    Returns Maybe<B>

equals

  • equals(m: Maybe<A>): boolean
  • If the value is Nothing, returns false. If the value is Just something, returns the boolean value of something.

    Just(2).equals(Just(2)) // true

    Parameters

    Returns boolean

extend

  • extend<B>(func: function): Maybe<B>
  • Allows sequencing of Maybe values and functions that accept a Maybe and return a non-Maybe value.

    const f = (m: Maybe<number>) => m.getOr(0)
    
    Just(2).extend(f)           // Just(2)
    Nothing<number>().extend(f) // Nothing()

    Type parameters

    • B

    Parameters

    • func: function
        • Parameters

          Returns B

    Returns Maybe<B>

fmap

  • fmap<B>(func: function): Maybe<B>
  • Our name for flatMap. Allows sequencing of Maybe values and functions that return a Maybe.

    For example, if you have a Maybe for a display name you might want to do something with it, like split a display name into first and last names:

    const squareIfEven = (a: number): Maybe<number> => a % 2 === 0 ? Just(a * a) : Just(a)
    
    Just(6).fmap(squareIfEven) // Just(36)

    The fmap method allows you to transform the value with a function that accepts it and returns another Maybe.

    Type parameters

    • B

      The type of the resulting value.

    Parameters

    • func: function
        • Parameters

          • value: A

          Returns Maybe<B>

    Returns Maybe<B>

fold

  • fold<B>(b: B, func: function): B
  • Apply a function to each case in the data structure.

    const f = 'none'
    const g = (s: string) => `some${s.length}`
    
    Nothing<string>().fold(f, g)) // 'none'
    Just('abc').fold(f, g))       // 'some3

    Type parameters

    • B

    Parameters

    • b: B
    • func: function
        • (value: A): B
        • Parameters

          • value: A

          Returns B

    Returns B

getOr

  • getOr(def: A): A
  • If the value of the current Maybe is Nothing, return a default value instaed.

    Just(2).getOr(1) // 2
    Nothing().getOr(1) // 1

    Parameters

    • def: A

    Returns A

getOrThrow

  • getOrThrow(): A
  • If the value of the current Maybe is Nothing, throw a default error message. WARNING: Unsafe - could throw an exception.

    Nothing().getOrThrow() // Error: Maybe was Nothing

    Returns A

getOrThrowMessage

  • getOrThrowMessage(message: string): A
  • If the value of the current Maybe is Nothing, throw the given error message. WARNING: Unsafe - could throw an exception.

    Nothing().getOrThrowMessage("It didn't work!") // Error: It didn't work!

    Parameters

    • message: string

    Returns A

instead

  • instead(def: A): Maybe<A>
  • Replaces the return value with what is provided if there is Nothing.

    Parameters

    • def: A

    Returns Maybe<A>

isNothing

  • isNothing(): boolean
  • Return true or false depending on whether there is Just something or Nothing.

    Returns boolean

map

  • map<B>(func: function): Maybe<B>
  • Take a function that maps one type to another and lift it to work with Maybes.

    const square = (a: number) => a * a
    
    Just(6).map(square) // Just(36)

    Type parameters

    • B

      The type of the resulting value.

    Parameters

    • func: function
        • (value: A): B
        • Parameters

          • value: A

          Returns B

    Returns Maybe<B>

on

  • on(callback: function): Maybe<A>
  • Run a side effect if the value is Just something, as opposed to Nothing.

    For example, if we just want to console.log() our Maybe with we can use the on function to apply a function to Just values, ignoring the result:

    name.on(val => {
      console.log('Display name:', val.join(' '))
    })

    Parameters

    • callback: function
        • (value: A): void
        • Parameters

          • value: A

          Returns void

    Returns Maybe<A>

prop

  • prop<P>(key: P): Maybe<NonNullable<A[P]>>
  • Returns a Maybe for the value at the given key.

    Type parameters

    • P: keyof A

    Parameters

    • key: P

    Returns Maybe<NonNullable<A[P]>>

toBoolean

  • toBoolean(): boolean
  • If the value is Just something, return the Boolean value of it. Otherwise, return false.

    Returns boolean

toNullable

  • toNullable(): A | undefined
  • Use of this function should be discouraged. Use one of the stronger methods below in most cases.

    Returns A | undefined

unless

  • unless(callback: function): Maybe<A>
  • Run a side effect if the value is Nothing.

    Parameters

    • callback: function
        • (): void
        • Returns void

    Returns Maybe<A>

when

  • when<B>(b: B): Maybe<B>
  • Replaces the return value with what is provided if there is Just something, discarding the original value.

    Type parameters

    • B

    Parameters

    • b: B

    Returns Maybe<B>

Static Just

  • Just<A>(value: A): Maybe<A>
  • Wrap a value in a Maybe.

    Type parameters

    • A

    Parameters

    • value: A

    Returns Maybe<A>

Static Nothing

  • Return an empty Maybe.

    Type parameters

    • A

    Returns Maybe<A>

Static maybe

  • maybe<A>(value: A | Nil): Maybe<NonNullable<A>>
  • Create a Maybe from a nullable value.

    maybe(2).getOr(1)         // 2
    maybe(undefined).getOr(1) // 1

    Type parameters

    • A

    Parameters

    • value: A | Nil

    Returns Maybe<NonNullable<A>>

Generated using TypeDoc