zanza00 / learn-fp-ts

Examples, explanations and resources for fp-ts
https://zanza00.gitbook.io/learn-fp-ts/
MIT License
34 stars 7 forks source link

More general explanations could help #25

Open lxgreen opened 3 years ago

lxgreen commented 3 years ago

Hi @zanza00,

Thanks for this nice book, it's very useful.

I understand that there's a lot of data types, type classes and libs in fp-ts ecosystem, and it takes a lot of time to explain everything and provide good examples.

As a fp-ts newbie, I'm hunting for any good learning resource (like this one). There are some missing pieces I still didn't find in various guides.

E.g. in the parseJSON example, there's a line

const sequenceE = A.sequenceT(E.either);

I understand this is some initialization, something like A.traversal(TE.taskEither) I've seen in other guide. I'd appreciate if there was a general explanation of such patterns.

zanza00 commented 3 years ago

Hi @lxgreen,

Sorry for the late reply. Life got in the way.

I will try to give you pointers, I am not really sure about the terminology to use for this stuff. There will be errors (I will add a chapter for sequence when I am more confident that I can explain it correctly and in a simple way).

As for the actual answer, I am still wrapping my head around sequence and its cousin traverse. Here is what I got so far.

From what I am able to understand sequence is a way to swap two types when they are nested. The most common one is using it when you have an Array of a monad like Option and you want to get an Option of an Array.

Something like this.

// this is pseudocode
const a: Array<Option<number>> = [Some(1), None, Some(2)]

const b: Option<Array<number>> = sequenceA(Option)(a)

Since sequence acts on monads and not functors you cannot generalize for every monad, hence why it needs to know in advance on what monad to use. In the example, I need to explicitly give the option monad instance.

There are a couple of implementations of sequence based on the shape of the data. For example, the T stands for Tuple (an array with a fixed length e.g [string, number]) and S for Struct (basically an object).

For traverse, my understanding is that it's sequence + map. I am yet to find a way to use it in my code.

Bear in mind that I am still learning and so far I am able to get by only using Option, Either, and TaskEither.

Hope it helps 🙂