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

implement ifElse

Open kutyel opened this issue 4 years ago • 1 comments

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!

kutyel avatar May 22 '20 12:05 kutyel

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

giogonzo avatar May 24 '20 08:05 giogonzo