In this lab I'm going to explore some of the other functions in fp-ts that I
don't use as often. Some of these are just concepts that I don't understamd
(yet) and others are just functions that I've never had a need for.
Apply
Apply is a type class that extends Functor. It is used to apply a function
contained in a context to a value contained in a context. The sequenceS and
sequenceT functions are used to apply a function to a struct or tuple of
values.
This seems very useful when needing to do validation on a struct or tuple of
values. Especially when applying multiple validations to a struct or tuple of
values.
Do
The Do function is used to chain computations in a Monad. In combination
with the bind function, it can be used create computed objects which are
chained together. The resulting right value is the final computation, can be
used with map to apply a function to the final value.
I think this is a very useful function, and I'm going to start using it more
often. Especially when used as TaskEither or IO to chain together
computations that rely on multiple successful steps.
Reader
The Reader type is used to maniplulate the object passed to a function. It is
used create a function that takes a config object and returns a value. The ask
function is used to get the config object from the context.
I have a little harder time finding good examples of when this would be useful.
I think it would be useful when you have a function that needs to access a
configuration object, but you don't want to pass the configuration object to the
function every time it is called.
Covariant
The Covariant type class is the group of types that support the map
function. Similar to the map funtions that iterates over the right value of
an Either, or the some value of an Option. The bimap function is used to
apply a function to the left or right value of an Either.
This can be useful when consolidating functions that might return an Either or
an Option. Such as when editing error messages, or when you want to apply a
function to the right value of an Either.
Contravariant
The Contravariant type class is the group of types that support the
contramap function. This function is used to apply a function to the input of
a function.
This can be useful when you want to sort a list of objects by a property of the
object. The contramap function can be used to apply a function to the input of
the Ord function.
Profunctor
The Profunctor type class is the group of types that support the promap
function. This function is used to apply a function to the input and output of a
function.
This was a little harder for me to wrap my head around. I understand that it can
be used to apply a function to the input and output of a function, but I'm not
sure when I would use this.
Moniod
The Monoid type class is the group of types that support the concatAll
function. This function is used to combine multiple Ord functions into a
single Ord function.
This can be useful when you want to sort a list of objects by multiple
properties of the object. I've done this through the use of M.concatAll and by
A.sortBy as well as trying out custom ordinal sorting.
ReadonlyArray
The ReadonlyArray type class is the group of types can not be mutated. This
helps to prevent accidental mutation of an array. When using ReadonlyArray I I
explored how to use it group and aggregate values across an array of objects.