fp-ts icon indicating copy to clipboard operation
fp-ts copied to clipboard

What is the idiomatic way to do pattern matching in the fp-ts ecosystem

Open CarpentersKeys opened this issue 1 year ago • 7 comments

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

interface ICounterActions {type: "increment" | "decrement"}
const counterReducer = (state: number, action: ICounterActions) => {
  return pipe(
    { state, ...action },
    match<ICounterActions>("type")({
      increment: state => state + 1,
      decrement: state => state - 1,
      a: () => {}, // would like compile error
    })
  );
};

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.

CarpentersKeys avatar Jul 24 '23 02:07 CarpentersKeys

There's no module in fp-ts for generic pattern matching, all data types have its own specialized match function, you can use something like ts-pattern

gcanti avatar Jul 24 '23 05:07 gcanti

Would it make sense to have Record.match to do this?

Would also like the options to match("key")([predicate, operation])(value)

CarpentersKeys avatar Jul 24 '23 13:07 CarpentersKeys

Perhaps https://github.com/pfgray/ts-adt is more close to what you're looking for.

DenisFrezzato avatar Jul 24 '23 13:07 DenisFrezzato

all data types have its own specialized match function, you can use something like ts-pattern

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#).

siwatanejo avatar Sep 12 '23 15:09 siwatanejo

There's also @unsplash/sum-types if you can accept the constraint of no generics.

samhh avatar Sep 13 '23 09:09 samhh

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

kingjordan avatar Mar 07 '24 15:03 kingjordan

Holy shit that's ugly

siwatanejo avatar Mar 07 '24 16:03 siwatanejo