Some useful types for JavaScript. Emphasizes practicality rather than safety.
- Maybe a
- Either a b
- Tuple a b
Represents the absence or presence of a value.
Returns a new Just
which represents an existent value.
Maybe.of(42).equals(Just(42)) // true
Returns a new Just
which represents an existent value.
Just(42).isJust // true
Returns a new Nothing
which represents the absence of a value.
Nothing().isJust // false
Returns Nothing
if given undefined
, otherwise a new Just
wrapping the
given value.
Maybe.fromUndefinable(42) // Just(42)
Maybe.fromUndefinable(false) // Just(false)
Maybe.fromUndefinable(undefined) // Nothing
Transforms a list of Maybe
values into Maybe
a list of values. If any
Nothing
values are found, returns Nothing
.
Maybe.flatten([]) // Just([])
Maybe.flatten([Just(1), Just(2), Just(3)]) // Just([1, 2, 3])
Maybe.flatten([Just(1), Nothing(), Just(2)]) // Nothing()
Returns a new Maybe
with its inner value modified, if it has one.
Just(42).map(a => a + 1) // Just(43)
Nothing().map(a => a + 1) // Nothing
Applies a modification to the inner value, if it has one.
function add5IfGreaterThan0(v) {
return v > 0 ? Just(v + 5) : Nothing();
}
Just(42).chain(add5IfGreaterThan0) // Just(47)
Nothing().map(add5IfGreaterThan0) // Nothing
Applies a wrapped function to a wrapped argument, if it has both.
Just(a => a + 5).ap(Just(42)) // Just(47)
Nothing().ap(Just(42)) // Nothing
Checks whether the Maybe
is equal to something else. Dispatches equal
if the
passed argument has one.
Just(42).equals(Just(42)) // true
Nothing().equals(Just('something')) // false
Unwraps the inner value, if it has one. If it is Nothing
, return the given
default.
Just(42).withDefault(35) // 42
Nothing().withDefault('some default') // 'some default'
Like withDefault
except it applies a function to the unwrapped value.
Just(42).maybe(35, a => a + 1) // 43
Nothing().maybe('some default', a => `some value ${a}`) // 'some default'
Unwraps a Just
, will throw an error of it's called on Nothing
Just(42).fromJust() // 42
Nothing().fromJust() // throws Error('fromJust called on Nothing')
No-op.
Just(42).toMaybe() // Just(42)
Nothing().toMaybe() // Nothing
Just
results in a Right
of the same value. Nothing
yields a Left
of the
passed value.
Just(42).toEither('val') // Right(42)
Nothing().toEither('val') // Left('val')
true
if on a Just
, false
if Nothing
.
Just(42).isJust // true
Nothing().isJust // false
true
if on a Nothing
, false
if Just
.
Just(42).isNothing // false
Nothing().isNothing // true
Represents one of two potential types of values. Generally, the left value
a
is an error and b
is the result of a successful computation.
Returns a new Right
which generally represents a success value.
Either.of(42).equals(Right(42)) // true
Returns a new Right
which generally represents a success value.
Right(42).isRight // true
Returns a new Left
which generally represents a failure or error value.
Left('hmmmm').isLeft // true
Transforms a list of Either
values into Either
a list of values. If any
Left
values are found, returns Left
of the first one.
Either.flatten([]) // Right([])
Either.flatten([Right(1), Right(2), Right(3)]) // Right([1, 2, 3])
Either.flatten([Right(1), Left('first'), Right(2), Left('snd')]) // Left('first')
Returns a new Either
with its Right
value modified. Keeps
Left
unchanged.
Right(42).map(a => a + 1) // Right(43)
Left('error text').map(a => a + 1) // Left('error text')
Returns a new Either
with the left function applied, given a Left, or the
Right function applied, given a Right
.
Right(42).bimap(a => a + 1, a => a + 2) // Right(44)
Left(42).bimap(a => a + 1, a => a + 2) // Right(43)
Applies a modification to the Right
value. Keeps Left
unchanged.
function add5IfGreaterThan0(v) {
return v > 0 ? Just(v + 5) : Left('I should be ignored')
}
Right(42).chain(add5IfGreaterThan0) // Right(47)
Left('err text').map(add5IfGreaterThan0) // Left('err text')
Applies a wrapped function to a wrapped argument, if both are Right
.
Right(a => a + 5).ap(Right(42)) // Right(47)
Left('hmmm').ap(Right(42)) // Left('hmmm')
Checks whether the Either
is equal to something else. Dispatches equal
if the
passed argument has one.
Right(42).equals(Right(42)) // true
Left('err').equals(Left('other err')) // false
Unwraps the Right
value, if it has one. If it is Left
, return the given
default.
Right(42).withDefault(35) // 42
Left('foo').withDefault('some default') // 'some default'
Like withDefault
except it applies a function to the unwrapped value. The
left function is applied if the value is a Left
, likewise, right to Right
.
Right(42).either(a => a * 2423, a => a + 1) // 43
Left(72).either(a => a - 30, a => a + 1) // 42
Unwraps a Right
, will throw an error of it's called on Left
Right(42).fromRight() // 42
Left().fromRight() // throws Error('fromRight called on a Left')
Unwraps a Left
, will throw an error of it's called on Right
Right().fromLeft() // throws Error('fromLeft called on a Right')
Left(42).fromLeft() // 42
Converts Left
to Nothing
, Right(a)
to Just(a)
.
Right(42).toMaybe() // Just(42)
Left(99).toMaybe() // Nothing
No-op.
Right(42).toEither() // Right(42)
Left(99).toEither() // Left(99)
true
if on a Right
, false
if Left
.
Right(42).isRight // true
Left(99).isRight // false
true
if on a Left
, false
if Right
.
Right(42).isLeft // false
Left(99).isLeft // true
Wraps a pair of values.
Returns a new Tuple
.
Tuple(1, 2).equals(Tuple(1, 1 + 1)) // true
Tuple(1)(2).equals(Tuple(1, 2)) // true
Returns a new Tuple
with its second, inner value modified.
Tuple(12, 42).map(a => a + 1) // Tuple(12, 43)
Returns a new Tuple
with the first function applied to the first value, and
the second applied to the second
Tuple(99, 'foo').bimap(a => a + 1, b => b + 'bar') // Tuple(100, 'foobar')
Is the first value in a Tuple
.
Tuple(42, 99).fst // 42
Is the second value in a Tuple
.
Tuple(42, 99).snd // 99