fp-ts
fp-ts copied to clipboard
[Documentation] Commentary on how to replace "array" instance
📖 Documentation
I'm used to using this package and monocle-ts
together to enable me to traverse over arrays. Typically I do const itemTraversal = fromTraversable(array)<Item>()
to construct the traversal, but it seems the array
instance has been deprecated. Docs now say to "Use small, specific instances instead." What does that mean? What smaller, specific instance of an array is there?
I suggest some documentation on how to do this. For what it's worth, the monocle-ts docs still use this deprecated array
in its fromTraversable
example.
"Small, specific instances" refers to the individual typeclass instances of a type constructor. For example, fromTraversable
uses the Traversable
instance, and Array
has one:
https://github.com/gcanti/fp-ts/blob/48b3786511aa3413c2f2a4d76311d3b2afa2cd64/src/Array.ts#L1908-L1920
So your code would become something like
import * as A from 'fp-ts/Array'
const itemTraversal = fromTraversable(A.Traversable)<Item>()
I believe these typeclass-specific instances are going to be used now instead of the "mega instances" for better tree-shaking.
This is awesome. Just used this for the first time. I was trying to figure out if I had to write my own applicative instance for my Item
or something :D
I have a related doubt about using TE.taskEither
, considering the import * as TE from 'fp-ts/lib/TaskEither. This seems deprecated with the same message to
Use small, specific instances instead.`. Where should that small instance be imported from?
Specifically, I'm using it to run a sequence of taskEithers like this:
sequenceT(TE.taskEither)(task1, task2)
@oana-sas Replace TE.taskEither
with either TE.ApplySeq
or TE.ApplyPar
.
The error message isn't very helpful, I only figured this out by looking at sequenceT's example in which you'll see sequenceT(O.Apply)
.