fp-ts icon indicating copy to clipboard operation
fp-ts copied to clipboard

[Documentation] Commentary on how to replace "array" instance

Open kylegoetz opened this issue 3 years ago • 4 comments

📖 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.

kylegoetz avatar Apr 20 '21 18:04 kylegoetz

"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.

0x706b avatar Apr 21 '21 00:04 0x706b

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

kylegoetz avatar Apr 22 '21 01:04 kylegoetz

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 avatar Jul 30 '21 14:07 oana-sas

@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).

samhh avatar Jul 30 '21 15:07 samhh