-
-
Notifications
You must be signed in to change notification settings - Fork 504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What is the idiomatic way to do pattern matching in the fp-ts ecosystem #1892
Comments
There's no module in |
Would it make sense to have Record.match to do this? Would also like the options to |
Perhaps https://github.com/pfgray/ts-adt is more close to what you're looking for. |
Can you give an example? I'm finding very hard to use fp-ts from TS (mind you, I don't have zero FP experience, I come from F#). |
There's also @unsplash/sum-types if you can accept the constraint of no generics. |
I use ts-adt like @DenisFrezzato mentioned use it like: import { makeMatch } from "ts-adt/MakeADT";
interface ICounterActions {type: "increment" | "decrement"}
matchType = makeMatch("type");
const counterReducer = (state: number, action: ICounterActions) => {
return pipe(
action,
matchType({
increment: ()=> { state => state + 1 },
decrement: ()=> { state => state - 1 },
})
}; This will throw an error if have not implemented a type or implement a type that does not exist. I personally like this because if I make a change it forces me to handle the new type anywhere I match it. This video explains it well |
Holy shit that's ugly |
Hi I'm pretty new to fp concepts but I'm starting to work on some demos with the library
I want to do something like
is there a way of doing this with fo-ts alone? I browsed through the modules several times but I could be missing it because I don't know a term.
is there a recommended library?
I realize I could just use
switch
but it's not pipeable.Thanks in advance.
The text was updated successfully, but these errors were encountered: