Last active
May 5, 2020 14:17
-
-
Save zaetrik/c8f5b57a9afcefb77c3e75e36969af75 to your computer and use it in GitHub Desktop.
Functor Option
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 O from "fp-ts/lib/Option"; | |
const MyOptionFunctor = { | |
map: (option: O.Option<unknown>, f: (a: unknown) => unknown) => | |
O.isNone(option) ? O.none : O.some(f(option.value)), | |
// We check if 'option' is 'none' | |
// if so return 'none' | |
// else take out the value from 'option' and apply it to our function 'f' | |
// then wrap it in an 'Option' again | |
}; | |
// We define a pure function (a: A) => B | |
const double = (x: number) => x * 2; | |
MyOptionFunctor.map(O.some(4), double); // { _tag: 'Some', value: 8 } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment