I'm very new to functional programming and specially fp-ts
library.
My question includes two parts:
- I'm seeing a pattern of turning Monads from one type to another like from
Task
toIO
or vice versa, how do we manage this, should we stay always on one or should we change as the chain continues? - How to simply make typescript follow of these type changes going from one to another?
For example let's say we have a couple of functions and we wanna compose them together as below, I know that probably this example is not very practical but it serves the purpose.
declare function getRnd(min: number, max: number): IO<number>; // Returns a random number within the range
declare function getPage(pageNo: number): TaskEither<Error, string>; // Make an Http request
declare function getLinks(pageContent: string): Option<string[]>; // Returns some links
// Let's say we wanna get a random page number and then return the links on it
// How do we compose these functions?
const getPageLinks = pipe(
getRnd(2, 4),
IO.chain(getPage), // I'm pretty sure TS will yells at me here
TaskEither.chain(getLinks),
log, // ?
)