First
First is a Monoid that will always return the first, non-empty value when
two First instances are combined. First is able to be a Monoid because
it implements a Maybe under the hood. The use of
the Maybe allows for an empty First to be represented
with a Nothing.
First can be constructed with either a value or a Maybe instance.
Any value passed to the constructor will be wrapped in a Just to
represent a non-empty instance of First. Any Maybe passed to the
constructor will be lifted as is, allowing the ability to "choose" a value based
on some disjunction.
While most Monoids only provide a valueOf function used for
extraction, First takes advantage of its underlying Maybe to
provide an additional option method.
Using valueOf will extract the underlying Maybe,
while option will extract the underlying value in
the Maybe, using the provided default value when the
underlying Maybe is a Nothing instance.
- Source
- Runkit
Implements#
Setoid, Semigroup, Monoid
Construction#
A First instance can be constructed by passing either a direct (unwrapped)
value a or a Maybe a to the constructor. When a direct value is passed, the
constructor will always wrap the value in a Just and return a
new First Just a instance.
Alternatively, by passing a Maybe a, the user can programmatically provide an
empty case to a given First by passing a Nothing.
- Source
- Runkit
Constructor Methods#
empty#
empty provides the identity for the Monoid in that when the value it
provides is concated to any other value, it will return the other value. In
the case of First the result of empty is Nothing. empty is
available on both the Constructor and the Instance for convenience.
- Source
- Runkit
Instance Methods#
equals#
Used to compare the underlying values of two First instances for equality by
value, equals takes any given argument and returns true if the passed
arguments is a First with an underlying value equal to the underlying value
of the First the method is being called on. If the passed argument is not
a First or the underlying values are not equal, equals will return false.
- Source
- Runkit
concat#
concat is used to combine two Semigroups of the same type under an operation
specified by the Semigroup. In the case of First, it will always provide the
first non-empty value. Any subsequent non-empty values will be thrown away and
will always result in the first non-empty value.
- Source
- Runkit
option#
First wraps an underlying Maybe which provides the ability to
option out a value in the case of an empty instance. Just like
option on a Maybe instance, it takes a value as its
argument. When run on an empty instance, the provided default
will be returned. If option is run on a non-empty instance however, the
wrapped value will be extracted not only from the First but also from
the underlying Just.
If the underlying Maybe is desired, the valueOf
method can be used and will return the Maybe instead.
- Source
- Runkit
valueOf#
valueOf is used on all crocks Monoids as a means of extraction. While the
extraction is available, types that implement valueOf are not necessarily
a Comonad. This function is used primarily for convenience for some of the
helper functions that ship with crocks. Calling valueOf on
a First instance will result in the underlying Maybe.
- Source
- Runkit
Transformation Functions#
eitherToFirst#
crocks/First/eitherToFirst
Used to transform a given Either instance to
a First instance, eitherToFirst will turn a Right instance into a
non-empty First, wrapping the original value contained in
the Right. All Left instances will map to
an empty First, mapping the originally contained value to
a Unit. Values on the Left will be lost and as such this
transformation is considered lossy in that regard.
Like all crocks transformation functions, eitherToFirst has two possible
signatures and will behave differently when passed either
an Either instance or a function that returns an instance
of Either. When passed the instance, a transformed First is
returned. When passed an Either returning function, a function will
be returned that takes a given value and returns a First.
- Source
- Runkit
lastToFirst#
crocks/First/lastToFirst
Used to transform a given Last instance to a First instance,
lastToFirst will turn a non-empty instance into a non-empty First wrapping
the original value contained within the Last.
All empty instances will map to an empty First.
Like all crocks transformation functions, lastToFirst has two possible
signatures and will behave differently when passed either
a Last instance or a function that returns an instance
of Last. When passed the instance, a transformed First is returned.
When passed a Last returning function, a function will be returned
that takes a given value and returns a First.
- Source
- Runkit
maybeToFirst#
crocks/First/maybeToFirst
Used to transform a given Maybe instance to
a First instance, maybeToFirst will turn a Just into a
non-empty First instance, wrapping the original value contained within
the First. All Nothing instances will map to
an empty First instance.
This function is available mostly for completion sake, as First can always
take a Maybe as its argument during construction. So while there is not a
real need for this to be used for transforming instances, it can come in
handy for lifting Maybe returning functions.
Like all crocks transformation functions, maybeToFirst has two possible
signatures and will behave differently when passed either a Maybe
instance or a function that returns an instance of Maybe. When
passed the instance, a transformed First is returned. When passed
a Maybe returning function, a function will be returned that
takes a given value and returns a First.
- Source
- Runkit
resultToFirst#
crocks/First/resultToFirst
Used to transform a given Result instance to a First instance,
resultToFirst will turn an Ok instance into a non-empty First,
wrapping the original value contained in the Ok. All Err instances will map
to an empty First, mapping the originally contained value to
a Unit. Values on the Err will be lost and as such this transformation is
considered lossy in that regard.
Like all crocks transformation functions, resultToFirst has two possible
signatures and will behave differently when passed either an Result instance
or a function that returns an instance of Result. When passed the instance,
a transformed First is returned. When passed a Result returning function,
a function will be returned that takes a given value and returns a First.
- Source
- Runkit