-
We have a Can we define an instance of Show for Option like that in fp-ts? If not how should I think and organize code for Show instances? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I believe you're describing |
Beta Was this translation helpful? Give feedback.
-
Amazing, thank you @samhh In case anyone else found this thread, this is how recursively we can do this with interface Show<A> {
readonly show: (a: A) => string
}
const getShowNumber = (): Show<number> => ({
show: (x) => `${x}`
})
const getShowOption = <A>(S: Show<A>): Show<Option<A>> => ({
show: (ma) => (isNone(ma) ? 'none' : `some(${S.show(ma.value)})`)
})
const getShowList = <A>(S: Show<A>): Show<List<A>> => ({
show: (ma) => {
function _show(xs: List<A>): string {
return isNil(xs) ? 'nil' : xs.head + ',' + _show(xs.tail);
}
return 'list(' + _show(ma) + ')'
}
})
const data4 = some(cons(1, cons(2, cons(3, nil))));
const showOptionListNumber = getShowOption(getShowList(getShowNumber()))
const output4 = showOptionListNumber.show(data4)
console.log(output4) // some(list(1,2,3,nil)) |
Beta Was this translation helpful? Give feedback.
I believe you're describing
getShow
: https://gcanti.github.io/fp-ts/modules/Option.ts.html#getshow