Skip to content

Instantly share code, notes, and snippets.

@roobie
Created December 1, 2015 13:44
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 roobie/9c2c9de14022a59d51bb to your computer and use it in GitHub Desktop.
Save roobie/9c2c9de14022a59d51bb to your computer and use it in GitHub Desktop.
a port of the `Result` type in Rust
'use strict';
const Result = function Result() {};
const Err = function Err(value) {
this.value = value;
};
const Ok = function Ok(value) {
this.value = value;
}
Result.match = function match(result, matcher, dynamicThis) {
return (matcher[result.constructor.name] || function () {})
.call(dynamicThis, (result || {}).value);
};
Result.err = function err(value) {
return new Err(value);
};
Result.ok = function ok(value) {
return new Ok(value);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment