Skip to content

Instantly share code, notes, and snippets.

@laduke
Created October 21, 2017 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laduke/49c10f362145c60dccdaa5e5010e4dcf to your computer and use it in GitHub Desktop.
Save laduke/49c10f362145c60dccdaa5e5010e4dcf to your computer and use it in GitHub Desktop.
// maybeToFuture :: a -> Maybe b -> Future a b
const maybeToFuture = S.curry2((x, maybe) =>
S.maybe(Future.reject(x), Future.of, maybe)
);
//also
/*
Aldwin Vlasblom @Avaq Aug 14 05:12
I generally have two strategies for dealing with a Future(Just(x)):
Using maybeToFuture. @davidchambers just posted an implementation. I use this when Nothing represents an actual error in the code (one that can only be recovered from by my generic error recovery procedure, which usually reports the error and crashes the process).
Using fromMaybe. Like you suggested, but limited to cases where the Nothing can be turned into a reasonable fallback value.
In the first case, I usually do the conversion as early as possible. In the second, I usually do the conversion as late as possible to avoid doing unnecessary computations on the default value (you can just pre-compute it and insert it later in the pipeline).
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment