Skip to content

Instantly share code, notes, and snippets.

@zaetrik
Last active May 5, 2020 14:17
Show Gist options
  • Save zaetrik/c8f5b57a9afcefb77c3e75e36969af75 to your computer and use it in GitHub Desktop.
Save zaetrik/c8f5b57a9afcefb77c3e75e36969af75 to your computer and use it in GitHub Desktop.
Functor Option
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