giogonzo / fp-ts-ramda

Ramda functions reimplemented in fp-ts
https://giogonzo.github.io/fp-ts-ramda/
MIT License
138 stars 9 forks source link

implement ifElse #32

Open kutyel opened 4 years ago

kutyel commented 4 years ago

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!

giogonzo commented 4 years ago

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