DEV Community

Discussion on: Getting started with fp-ts: Reader

Collapse
 
gcanti profile image
Giulio Canti

I have small concern if the whole typing of that has/or not a big sense

Well, you are just writing a bunch of functions returning Reader, whether or not you add an explicit type. The point is making the dependencies as implicit as possibile, the key is to put them as a last argument. Note that often people put the dependencies as a first argument

const f = (deps: Dependencies) => (b: boolean) => (b ? deps.i18n.true : deps.i18n.false)
Enter fullscreen mode Exit fullscreen mode

making composition more difficult.

For the second...

all the following gs are equivalent (so feel free to choose the style you prefer)

const g = (n: number) => (deps: Dependencies) => pipe(deps, f(n > deps.lowerBound))

const g = (n: number): Reader<Dependencies, string> => deps => pipe(deps, f(n > deps.lowerBound))

const g = (n: number): Reader<Dependencies, string> => deps => f(n > deps.lowerBound)(deps)

const g = (n: number): Reader<Dependencies, string> =>
  pipe(
    ask<Dependencies>(),
    chain(deps => f(n > deps.lowerBound))
  )
Enter fullscreen mode Exit fullscreen mode
Collapse
 
macsikora profile image
Pragmatic Maciej

Yes the whole idea has a lot of sense, and I mean adding argument after not before. Thanks for clarifying!

Collapse
 
gunzip profile image
Danilo Spinelli

I'm used to put deps as the first argument since this lets you partially apply f:

const f = (deps: Dependencies) => (b: boolean): string => (...)