fp-ts-ramda
fp-ts-ramda copied to clipboard
implement ifElse
Hi! now that I'm back to typescript, I'm willing to learn fp-ts
but I miss some functions from ramda too much 😭, how would ifElse
be implemented? Thanks!
If I guess the ifElse
signature correctly from the ramda docs, it should be something like (+ currying, ignored here):
function ifElse<A, R>(predicate: (a: A) => boolean, whenTrue: (a: A) => R, whenFalse: (a: A) => R, a: A): R
I believe it could also provide an overload similar to:
function ifElse<A, B, R>(predicate: (a: A) => a is B, whenTrue: (a: B) => R, whenFalse: (a: A) => R, a: A): R
@kutyel would you like to send a PR? I can provide more help if needed!
Manual currying should be implemented similar to any other function implemented in this repo.
For the actual logic of the function, I would go with something like (beware, not even tested in the editor!):
import { fold } from 'fp-ts/lib/boolean'
pipe(
a,
predicate,
fold(() => whenFalse(a), () => whenTrue(a))
)