-
-
Notifications
You must be signed in to change notification settings - Fork 503
Home
Giulio Canti edited this page Sep 16, 2022
·
13 revisions
How to choose an effect for my app?
Here's a table containing the basic effects
Type constructor | Effect (interpretation) |
---|---|
ReadonlyArray<A> |
a non deterministic computation |
Option<A> |
a computation that may fail |
Either<E, A> |
a computation that may fail and yield an error |
IO<A> |
a synchronous computation that never fails |
Task<A> |
an asynchronous computation that never fails |
Reader<R, A> |
reading from an environment (dependency injection) |
State<S, A> |
stateful computation |
These basic effects are like ingredients for a recipe.
In order to get your recipe (the effect of your app) you can add all the ingredients you need by nesting the corresponding type constructors.
Example
Let's say your app
- will make use of dependency injection
- will run in an asynchronous context
- will handle operations which may fail
Here's the corresponding ingredients
Reader
Task
Either
in order to mix the ingredients you just nest the type constructors
import { Reader } from 'fp-ts/Reader'
import { Task } from 'fp-ts/Task'
import { Either } from 'fp-ts/Either'
type MyEffect<R, E, A> = Reader<R, Task<Either<E, A>>>
These three ingredients are so common that fp-ts
exports a ready-to-use module, the ReaderTaskEither
module.
What is the difference between
ReadonlyArray
andArray
modules?
The Array
module predates the ReadonlyArray
module and will be deprecated in the near future. ReadonlyArray
is Array
minus all the mutating functions.