Skip to content

Instantly share code, notes, and snippets.

@zaetrik
Created May 12, 2020 07:23
Show Gist options
  • Save zaetrik/4ec1251ea1014a65792fdf6e6fd3f23a to your computer and use it in GitHub Desktop.
Save zaetrik/4ec1251ea1014a65792fdf6e6fd3f23a to your computer and use it in GitHub Desktop.
Monads: Flatten
// Taken from https://dev.to/gcanti/getting-started-with-fp-ts-monad-6k
import { isNone } from 'fp-ts/lib/Option'
// flatten takes in a nested context like Option<Option<A>> and flattens/unwraps it to Option<A>
// We do this by returning the value from the nested context Option<Option<A>> which is Option<A>
const flatten = <A>(mma: Option<Option<A>>): Option<A> => (isNone(mma) ? none : mma.value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment