I've been using fp-ts for a little over three years now and has been a
fundemental part of my development process. I've been using it in production for
the last two years and have been very happy with it. I've been using it in a
variety of projects, from small personal projects to large enterprise
applications. I've also been using it in a variety of languages, from TypeScript
to Dart.
While the majority of what I do with fp-ts is pretty standard, there are a few
things that I would like to improve upon. This series of posts will be a
collection of things that I've learned about fp-ts that I think are worth
sharing or exploring further.
The Array type is a primative that is used to represent a list of values. It
is a very common type in functional programming and is generally used to replace
for loops.
Option
The Option type is a primative that is used to represent a value that may or
may not exist. It is a very common type in functional programming and is
generally used to replace null or undefined values.
Either
The Either type is a primative that is used to represent a value that may be
one of two types. It is a very common type in functional programming and is
generally used to replace throw statements.
Task
The Task type is a primative that is used to represent a value that may be
computed asynchronously. It is a very common type in functional programming and
is generally used to replace Promise objects.
TaskEither
The TaskEither type is a primative that is used to represent a value that may
be computed asynchronously and may be one of two types. It is a very common type
in functional programming and is generally used to replace Promise objects
that may throw errors.
Composition
pipe vs flow
The pipe and flow functions are used to compose functions together. They are
very similar, but have a few key differences. The pipe function starts with a
value and then applies a series of functions to it. The flow function composes
a series of functions together and then returns a new function that can be
applied to a value.
chain
The chain function is used to flatten nested primatives. When you need to
flatten left or right values, you can use the chain function. It is very
similar to the flatMap function in other languages.
match or fold
The match function is used to extract values from primatives. When you need to
extract a value to be used in the application, match forces you to think about
both the left and right values.
sometimes its needed to return multiple types from a match. In this case, you
can use the foldW function.