gigobyte / purify

Functional programming library for TypeScript - https://gigobyte.github.io/purify/
ISC License
1.5k stars 59 forks source link

Mistake in Maybe documentation example? #570

Closed filidorwiese closed 2 years ago

filidorwiese commented 2 years ago

On https://gigobyte.github.io/purify/adts/Maybe/ the following example is given:

const port = getConfig()
    .chain(x => x.port)
    .map(parseInt)
    .orDefault(8080)

Although it's not fully clear what the hypothetical getConfig() method returns, the non-Maybe example suggests that it is probably a plain object with an optional port property:

const port = parseInt(getConfig()?.port ?? '8080')

Should the Maybe-equivalent example then not be:

const port = Maybe.of(getConfig())
    .chainNullable(x => x.port)
    .map(parseInt)
    .orDefault(8080)

instead?

gigobyte commented 2 years ago

The With Maybe example implies that Maybe is used within the whole project, so getConfig would return a Maybe<Confg>. It could be made more clear but the example is for demonstration purposes only.

I don't know, I'll close this issue but if anyone else is reading this and feels the same way please leave a comment, I might reconsider the examples.

filidorwiese commented 2 years ago

Thanks for the super quick reply. I can understand getConfig() returning a Maybe making the Maybe.of() superfluous. But even then the .chainNullable addition would be more accurate. It seems unlikely to me that each property in the getConfig() Maybe is wrapped in a Maybe itself.

Anyways, it's your call, it just confused me today. Keep up the good work :+1: