The type of optional value the Maybe represents.
An alias for maybe
.
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)
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
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()
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.
The type of the resulting value.
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
If the value of the current Maybe is Nothing, return a default value instaed.
Just(2).getOr(1) // 2
Nothing().getOr(1) // 1
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
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!
Replaces the return value with what is provided if there is Nothing.
Return true
or false
depending on whether there is Just something or Nothing.
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)
The type of the resulting value.
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(' '))
})
Returns a Maybe for the value at the given key.
If the value is Just something, return the Boolean value of it. Otherwise, return false.
Use of this function should be discouraged. Use one of the stronger methods below in most cases.
Run a side effect if the value is Nothing.
Replaces the return value with what is provided if there is Just something, discarding the original value.
Wrap a value in a Maybe.
Return an empty Maybe.
Generated using TypeDoc
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))