Last
Last
is a Monoid
that will always return the last, non-empty value when
two Last
instances are combined. Last
is able to be a Monoid
because
it implements a Maybe
under the hood. The use of the Maybe
allows for an
empty
Last
to be represented with a Nothing
.
Last
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 Last
. Any Maybe
passed to the constructor will be lifted as
is, allowing the ability to "choose" a value based on some disjunction.
While most Monoid
s only provide a valueOf
function used for
extraction, Last
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
#
ImplementsSetoid
, Semigroup
, Monoid
#
ConstructionA Last
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 Last Just a
instance.
Alternatively, by passing a Maybe a
, the user can programmatically provide an
empty case to a given Last
by passing a Nothing
.
- Source
- Runkit
#
Constructor Methods#
emptyempty
provides the identity for the Monoid
in that when the value it
provides is concat
ed to any other value, it will return the other value. In
the case of Last
the result of empty
is Nothing
. empty
is available
on both the Constructor and the Instance for convenience.
- Source
- Runkit
#
Instance Methods#
equalsUsed to compare the underlying values of two Last
instances for equality by
value, equals
takes any given argument and returns true
if the passed argument
is a Last
with an underlying value equal to the underlying value of
the Last
the method is being called on. If the passed argument is not
a Last
or the underlying values are not equal, equals
will return false
.
- Source
- Runkit
#
concatconcat
is used to combine two Semigroup
s of the same type under an operation
specified by the Semigroup
. In the case of Last
, it will always provide the
last non-empty value. All previous non-empty values will be thrown away and
will always result in the last non-empty value.
- Source
- Runkit
#
optionLast
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 Last
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
#
valueOfvalueOf
is used on all crocks
Monoid
s 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 Last
instance
will result in the underlying Maybe
.
- Source
- Runkit
#
Transformation Functions#
eitherToLastcrocks/Last/eitherToLast
Used to transform a given Either
instance to
a Last
instance, eitherToLast
will turn a Right
instance into a
non-empty Last
, wrapping the original value contained in the Right
.
All Left
instances will map to an empty
Last
, 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, eitherToLast
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 Last
is returned. When passed an Either
returning function,
a function will be returned that takes a given value and returns a Last
.
- Source
- Runkit
#
firstToLastcrocks/Last/firstToLast
Used to transform a given First
instance to
a Last
instance, firstToLast
will turn a non-empty instance into a
non-empty Last
wrapping the original value contained within
the First
. All empty
instances will map to
an empty
Last
.
Like all crocks
transformation functions, firstToLast
has two possible
signatures and will behave differently when passed either
a First
instance or a function that returns an instance
of First
. When passed the instance, a transformed Last
is returned.
When passed a First
returning function, a function will be returned
that takes a given value and returns a Last
.
- Source
- Runkit
#
maybeToLastcrocks/Last/maybeToLast
Used to transform a given Maybe
instance to
a Last
instance, maybeToLast
will turn a Just
into a
non-empty Last
instance, wrapping the original value contained within
the Last
. All Nothing
instances will map to
an empty
Last
instance.
This function is available mostly for completion sake, as Last
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, maybeToLast
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 Last
is returned. When passed a Maybe
returning function,
a function will be returned that takes a given value and returns a Last
.
- Source
- Runkit
#
resultToLastcrocks/Last/resultToLast
Used to transform a given Result
instance to a Last
instance,
resultToLast
will turn an Ok
instance into a non-empty Last
,
wrapping the original value contained in the Ok
. All Err
instances will map
to an empty
Last
, 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, resultToLast
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 Last
is returned. When passed a Result
returning function,
a function will be returned that takes a given value and returns a Last
.
- Source
- Runkit