There are two Bool values, and three Status values, so there are 2×3=6 values for the product of Bool and Status. Now, what about the Sum type?
True
False
Waiting
Successful
Failed
We have 2+3=5 total values for the sum of Bool and Product.
Now, obviously, this math doesn't quite work out for types with infinite values, like strings or bigints or arrays, but that's where the analogy comes from.
If you extend this further, you can even figure out what an exponential type would be: the exponential type Bool^Status is a function that takes a Status as its argument and returns a Bool.
First I've heard of exponential type... to make sure I get it, you're saying there are 2^3 possible functions which take a status and return a bool? So if the enum inputs are success, wait, fail, you'd have
Fff
Tff
Ftf
Fft
Ttf
Tft
Ftt
Ttt
All as possible implementations of that function. Neat...
I know there are a bunch of small guides / articles written by the author of fp-ts, but is there a good book, guide, etc. for diving in for someone who is kind of familiar with functional concepts but not quite ready for going full haskell? A book on haskell?
ADTs like the one described here for remote data is a good start, but I haven’t yet found a good way to compose multiple pieces of data that is in potentially different states.
We use another class based concept on top at work where you can compose multiple pieces of remote data together, but it really only works well for the happy path.
The switch case is still a bit redundant and inelegant, particularly considering how out of place switch case syntax feels in a Javascript codebase in general. Early returns are the way to go.
The point of the switch-case is to mimic pattern matching and TS is smart enough tell you whether you handled all cases.
Would you prefer that it's a if-elseif-else based structure instead? I guess that would work too but I feel like it could be easier to write it in a way where you accidentally forget to handle some case (since your else / last return is a potential catch-all).
I personally don't mind switch-case because I'm so accustomed to it in ReasonML/ReScript already.
Although I don't mind switch statements, I was thinking similarly about using early returns instead. But this is where switch/match expressions like in Rust or C# would really shine; it's too bad JavaScript/Typescript doesn't support them though.
[+] [-] lalaithion|4 years ago|reply
Now, obviously, this math doesn't quite work out for types with infinite values, like strings or bigints or arrays, but that's where the analogy comes from.
If you extend this further, you can even figure out what an exponential type would be: the exponential type Bool^Status is a function that takes a Status as its argument and returns a Bool.
[+] [-] ronenlh|4 years ago|reply
In the switch statement it becomes a Sum value (as shown in the article).
In ReScript (shown as well, which uses OCaml under the hood) the syntax is that of a Variant, in which each element is a different nominal type.
[+] [-] adverbly|4 years ago|reply
Fff
Tff
Ftf
Fft
Ttf
Tft
Ftt
Ttt
All as possible implementations of that function. Neat...
[+] [-] rualca|4 years ago|reply
Indeed it would drive the point home if instead of simply mentioning product they referred to Cartesian product.
I guess sum types imply adding together two domain, but unless there's a subtlety then union would be clearer as well.
[+] [-] infogulch|4 years ago|reply
[+] [-] jimsimmons|4 years ago|reply
[+] [-] benrbray|4 years ago|reply
[1] https://github.com/gcanti/fp-ts
[+] [-] douglasisshiny|4 years ago|reply
[+] [-] myvoiceismypass|4 years ago|reply
[1] https://github.com/Effect-TS/core
[+] [-] g_delgado14|4 years ago|reply
https://www.youtube.com/watch?v=QyunIFfKLIU
[+] [-] inssein|4 years ago|reply
We use another class based concept on top at work where you can compose multiple pieces of remote data together, but it really only works well for the happy path.
[+] [-] ramesh31|4 years ago|reply
[+] [-] elnygren|4 years ago|reply
Would you prefer that it's a if-elseif-else based structure instead? I guess that would work too but I feel like it could be easier to write it in a way where you accidentally forget to handle some case (since your else / last return is a potential catch-all).
I personally don't mind switch-case because I'm so accustomed to it in ReasonML/ReScript already.
[+] [-] ducharmdev|4 years ago|reply
[+] [-] unknown|4 years ago|reply
[deleted]