Created
June 21, 2012 16:52
-
-
Save alexnask/2966969 to your computer and use it in GitHub Desktop.
Maybe and Either
This file contains hidden or 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
| MaybeState: enum { | |
| None, | |
| Some | |
| } | |
| Maybe: class <T> { | |
| _value: T | |
| value: T { | |
| get { | |
| if(state == MaybeState None) raise("No value!") | |
| _value | |
| } | |
| set(=_value) { | |
| state = MaybeState Some | |
| } | |
| } | |
| state: MaybeState = MaybeState None | |
| init: func ~none {} | |
| init: func ~some (=_value) { | |
| state = MaybeState Some | |
| } | |
| hasSome?: func -> Bool { state == MaybeState Some } | |
| } | |
| Either: class <T, V> { | |
| _left: Maybe<T> | |
| _right: Maybe<V> | |
| init: func ~left (l: T) { _left value = l } | |
| init: func ~right (r: V) { _right value = r } | |
| left?: func -> Bool { _left hasSome?() } | |
| right?: func -> Bool { _right hasSome?() } | |
| left: func -> T { _left value } | |
| right: func -> V { _right value } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment