Last active
February 20, 2021 23:37
-
-
Save jahan-addison/33c9ad38a1ae7e5fe171b81ec4fbf539 to your computer and use it in GitHub Desktop.
This file contains 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
// License: Public domain | |
// see: https://creativecommons.org/publicdomain/zero/1.0/deed.en | |
interface Option<T> { | |
value: T | null; | |
} | |
/** | |
* First-class None subtype for a value that is Maybe.None. | |
*/ | |
export class None<T> implements Option<null> { | |
public value = null | |
} | |
/** | |
* First class Some<T> subtype for a value that is Maybe<T>Some(value). | |
*/ | |
export class Some<T> implements Option<T> { | |
public value: T; | |
public constructor(value: T) { | |
this.value = value; | |
} | |
} | |
/** | |
* Factory wrapper of Some, None, so that pattern matching is possible. | |
*/ | |
export default class Maybe { | |
public static Some<T>(value: T): Some<T> { | |
return new Some<T>(value); | |
} | |
public static None(): Maybe { | |
return new None() as Maybe; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment