How to eliminate ternary operators or if-else with fp-ts #1835
-
Hi,
Can someone help me on this? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
folding an option based on isComp is a pretty clean way to achieve this. This implementation for example: // helper function based on isComp, boolean -> ReadonlyArray<string> -> Option<string>
const firstMatchOrHead = (isComp: boolean) => (roa: ReadonlyArray<string>) =>
pipe(
isComp,
O.fromPredicate(identity),
O.fold(
() => RA.head(roa),
() => RA.findFirst((x) => x === "react")(roa)
)
);
// main function from above, Option<Param> -> string
const getX = (paramM: Option<Param>) =>
pipe(
paramM,
O.chain((p) => p.arr),
O.chain(firstMatchOrHead(isComp)),
O.getOrElse(constant("javascript"))
); |
Beta Was this translation helpful? Give feedback.
-
You can also achieve the same thing with
In general, if you find yourself doing Apologies for bad code formatting... I'm writing this on my phone |
Beta Was this translation helpful? Give feedback.
-
Hey Everyone,
|
Beta Was this translation helpful? Give feedback.
Hey Everyone,
I tried the below and it worked: