gcanti / fp-ts

Functional programming in TypeScript
https://gcanti.github.io/fp-ts/
MIT License
10.63k stars 503 forks source link

Document how to make working with abstractions ergonomic #1936

Open aloussase opened 1 month ago

aloussase commented 1 month ago

When I want to abstract over the type of Monad (for example when using tagless final) I end up having to write code like this:

export const register = <F extends URIS>(F: UserSymmantics<F> & MonadTask1<F>) => (username: string, password: string) => {
  const flatMap = <A, B>(f: (a: A) => Kind<F, B>) => (ma: Kind<F, A>): Kind<F, B> => F.chain(ma, f)
  return pipe(
    F.fromTask(() => hash(password, 10)),
    flatMap(hashedPassword => F.createUser(username, hashedPassword)),
  )
}

Which is not too bad, but I'd prefer not to write the flatMap function. I'm basing this on the fp to the max example from the repository. Isn't there a more ergonomic way of writing code like this?

Thank you very much.