Operating on two Eithers

149 views Asked by At

Suppose that you have the following code:

import R from "ramda";
import S from "sanctuary";
import { Left, Right } from "sanctuary-either";

const add = R.curry((p1, p2) => p1 + p2);
const addOne = add(1);

const func1 = () => Right(2);
const func2 = () => Right(7);

Combining addOne with func1 or func2 is relatively easy:

const res = R.compose(
  S.map(addOne),
  func1
)(); 

but how can one call add using func1 and func2 as arguments?

p.s. I know that ramda offers an add function. Consider the example as an abstraction of a real world problem.

1

There are 1 answers

5
Bergi On BEST ANSWER

You are probably looking for the lift2 function:

const addEithers = S.lift2(add)

console.log(addEithers(func1())(func2()))

Alternatively, you can use ap:

S.ap(S.map(add)(func1()))(func2())