Last active
October 18, 2019 18:08
-
-
Save Willmo36/e7b002cbdd01b2ef760ba8306a45d54a to your computer and use it in GitHub Desktop.
liftA2 fp-ts + Option example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as A from "fp-ts/lib/Applicative"; | |
import { URIS, Kind } from "fp-ts/lib/HKT"; | |
import { Option, option, some } from "fp-ts/lib/Option"; | |
const lift = <F extends URIS, A, B, C>( | |
F: A.Applicative1<F>, | |
fn: (a: A) => (b: B) => C | |
) => (fa: Kind<F, A>) => (fb: Kind<F, B>): Kind<F, C> => { | |
const fab = F.map(fa, fn); | |
const c = F.ap(fab, fb); | |
return c; | |
}; | |
const liftToOption = <A, B, C>(fn: (a: A) => (b: B) => C) => ( | |
optionA: Option<A> | |
) => (optionB: Option<B>): Option<C> => { | |
const optionAB = option.map(optionA, fn); | |
const optionC = option.ap(optionAB, optionB); | |
return optionC; | |
}; | |
const add = (a: number) => (b: number) => a + b; | |
const optionAdd = lift(option, add); | |
add(1)(2); | |
optionAdd(some(1))(some(2)); | |
liftToOption(add)(some(1))(some(2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment